WebDriverTestCase   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 84
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A runBare() 0 11 2
A logInAsUser() 0 11 1
A waitForUserInput() 0 6 2
A waitForLoadingAnimation() 0 6 1
A waitForAlert() 0 6 1
A getHeaderText() 0 6 1
A tearDown() 0 16 4
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 getHeaderText()
78
    {
79
        return $this->webDriver->findElement(
80
            WebDriverBy::cssSelector('.intro h1')
81
        )->getText();
82
    }
83
84
    public function tearDown()
85
    {
86
        if ($this->webDriver !== null) {
87
            if (in_array($this->getStatus(), [\PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE, \PHPUnit_Runner_BaseTestRunner::STATUS_ERROR])) {
88
                echo PHP_EOL . PHP_EOL . PHP_EOL;
89
                echo 'data:image/png;base64,' . base64_encode($this->webDriver->takeScreenshot());
90
                echo PHP_EOL . PHP_EOL . PHP_EOL;
91
            }
92
        }
93
94
        parent::tearDown();
95
96
        if ($this->webDriver !== null) {
97
            $this->webDriver->close();
98
        }
99
    }
100
}
101