Failed Conditions
Pull Request — master (#142)
by Zac
12:55
created

WebDriverTestCase::waitForAjax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Overwatch\UserBundle\Tests\Base;
4
5
use Facebook\WebDriver\Exception\TimeOutException;
6
use Facebook\WebDriver\Remote\RemoteWebDriver;
7
use Facebook\WebDriver\Remote\WebDriverCapabilityType;
8
use Facebook\WebDriver\WebDriverBy;
9
use Facebook\WebDriver\WebDriverExpectedCondition;
10
use Facebook\WebDriver\WebDriverKeys;
11
use Overwatch\UserBundle\DataFixtures\ORM\UserFixtures;
12
13
/**
14
 * WebDriverTestCase
15
 * Extends DatabseAwareTestCase to add WebDriver logic.
16
 */
17
class WebDriverTestCase extends DatabaseAwareTestCase
18
{
19
    /**
20
     * @var RemoteWebDriver
21
     */
22
    protected $webDriver = null;
23
    
24
    public function setUp()
25
    {
26
        parent::setUp();
27
        
28
        $capabilities = [WebDriverCapabilityType::BROWSER_NAME => 'firefox'];
29
        $this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
30
    }
31
    
32
    public function runBare()
33
    {
34
        try {
35
            parent::runBare();
36
        } catch (TimeOutException $e) {
37
            //If there's a timeout the first time, retry, but don't catch
38
            //anything the second time around (i.e. there's only 1 retry attempt)
39
            echo '!';
40
            parent::runBare();
41
        }
42
    }
43
    
44
    public function logInAsUser($userReference)
45
    {
46
        $user = UserFixtures::$users[$userReference];
47
        $this->webDriver->get('http://127.0.0.1:8000');
48
        $this->webDriver->findElement(WebDriverBy::id('username'))->clear();
49
        $this->webDriver->findElement(WebDriverBy::id('username'))->click();
50
        $this->webDriver->getKeyboard()->sendKeys($user->getEmail());
51
        $this->webDriver->findElement(WebDriverBy::id('password'))->click();
52
        $this->webDriver->getKeyboard()->sendKeys('p4ssw0rd');
53
        $this->webDriver->getKeyboard()->pressKey(WebDriverKeys::ENTER);
54
    }
55
    
56
    public function waitForUserInput()
57
    {
58
        if (trim(fgets(fopen('php://stdin', 'r'))) != chr(13)) {
59
            return;
60
        }
61
    }
62
    
63
    public function waitForLoadingAnimation()
64
    {
65
        $this->webDriver->wait(30, 500)->until(
66
            WebDriverExpectedCondition::invisibilityOfElementLocated(WebDriverBy::id('loading'))
67
        );
68
    }
69
    
70
    public function waitForAlert()
71
    {
72
        $this->webDriver->wait()->until(
73
            WebDriverExpectedCondition::alertIsPresent()
74
        );
75
    }
76
    
77
    public function waitForAjax()
78
    {
79
        $this->webDriver->wait(5, 100)->until(function($this) {
80
            $pendingApiCalls = $this->webDriver->executeScript('angular.element(document.body).injector().get(\'$http\').pendingRequests.length');
81
            var_dump($pendingApiCalls);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($pendingApiCalls); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
82
            return ($pendingApiCalls === 0);
83
        });
84
    }
85
    
86
    public function getHeaderText()
87
    {
88
        return $this->webDriver->findElement(
89
            WebDriverBy::cssSelector('#page h1')
90
        )->getText();
91
    }
92
    
93
    public function tearDown()
94
    {
95
        if ($this->webDriver !== null) {
96
            if (in_array($this->getStatus(), [\PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE, \PHPUnit_Runner_BaseTestRunner::STATUS_ERROR])) {
97
                echo PHP_EOL . PHP_EOL . PHP_EOL;
98
                echo 'data:image/png;base64,' . base64_encode($this->webDriver->takeScreenshot());
99
                echo PHP_EOL . PHP_EOL . PHP_EOL;
100
            }
101
        }
102
        
103
        parent::tearDown();
104
        
105
        if ($this->webDriver !== null) {
106
            $this->webDriver->close();
107
        }
108
    }
109
}
110