Failed Conditions
Push — master ( b9670c...4ba57f )
by Zac
16:20 queued 37s
created

AddEditTestTest::testEditTest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
1
<?php
2
3
namespace Overwatch\TestBundle\Tests\E2E;
4
5
use Facebook\WebDriver\WebDriverBy;
6
use Facebook\WebDriver\WebDriverKeys;
7
use Facebook\WebDriver\WebDriverSelect;
8
use Overwatch\TestBundle\DataFixtures\ORM\TestFixtures;
9
use Overwatch\UserBundle\Tests\Base\WebDriverTestCase;
10
11
/**
12
 * AddEditTestTest
13
 * Tests the add/edit group view
14
 */
15
class AddEditTestTest extends WebDriverTestCase
16
{
17
    public function setUp()
18
    {
19
        parent::setUp();
20
        
21
        $this->logInAsUser('user-1');
22
        $this->waitForLoadingAnimation();
23
    }
24
    
25
    public function testEditTest()
26
    {
27
        $this->webDriver->findElement(
28
            //View first test
29
            WebDriverBy::cssSelector('ul.groups > li:nth-child(1) li:nth-child(1) a:nth-child(3)')
30
        )->click();
31
        $this->waitForLoadingAnimation();
32
        
33
        $this->webDriver->findElement(
34
            //Edit test
35
            WebDriverBy::cssSelector('ul.results li:last-child a')
36
        )->click();
37
        $this->waitForLoadingAnimation();
38
        
39
        $this->checkTestField('name');
40
        $this->checkTestField('actual');
41
        $this->checkTestField('expectation');
42
        $this->checkTestField('expected');
43
        
44
        $this->getTestField('name')->clear();
45
        $this->getTestField('name')->sendKeys('UnUnTestium');
46
        $this->webDriver->findElement(
47
            WebDriverBy::cssSelector("button[data-ng-click='save()']")
48
        )->click();
49
        $this->waitForLoadingAnimation();
50
        $this->assertEquals('UnUnTestium', $this->getHeaderText());
51
    }
52
    
53
    public function testAddTest()
54
    {
55
        $this->webDriver->findElement(
56
            //Add test button
57
            WebDriverBy::cssSelector('ul.tests li:last-child a:nth-child(2)')
58
        )->click();
59
        
60
        $this->getTestField('name')->sendKeys('Github Status Resolves');
61
        $this->getTestField('actual')->sendKeys('status.github.com');
62
        (new WebDriverSelect($this->getTestField('expectation')))->selectByValue('string:toResolveTo');
63
        $this->getTestField('expected')->sendKeys('octostatus-production.github.com');
64
        $this->getTestField('expected')->sendKeys(WebDriverKeys::ENTER);
65
        
66
        $this->waitForLoadingAnimation();
67
        $this->assertCount(3, $this->getTestsForFirstGroup());
68
        $this->assertContains('Github Status Resolves', $this->getTestsForFirstGroup()[2]->getText());
69
    }
70
    
71
    private function checkTestField($field, $value = null)
72
    {
73
        $field = strtolower($field);
74
        
75
        if ($value === null) {
76
            $value = TestFixtures::$tests['test-1']->{'get' . ucfirst($field)}();
77
        }
78
        
79
        if ($field === 'expectation') {
80
            $select = new WebDriverSelect(
81
                $this->getTestField('expectation')
82
            );
83
            
84
            $this->assertEquals($value, $select->getFirstSelectedOption()->getText());
85
            return;
86
        }
87
        
88
        $this->assertEquals($value, $this->getTestField($field)->getAttribute('value'));
89
    }
90
    
91
    private function getTestField($field)
92
    {
93
        return $this->webDriver->findElement(
94
            WebDriverBy::cssSelector("*[data-ng-model='test.$field']")
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $field instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
95
        );
96
    }
97
    
98
    private function getTestsForFirstGroup()
99
    {
100
        $groups = $this->webDriver->findElements(
101
            WebDriverBy::cssSelector('.groups > li.ng-scope')
102
        );
103
                
104
        if (count($groups) < 1) {
105
            return [];
106
        }
107
        
108
        return $groups[0]->findElements(
109
            WebDriverBy::cssSelector('.tests li.ng-scope')
110
        );
111
    }
112
}
113