1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NuvoleWeb\Drupal\DrupalExtension\Context; |
4
|
|
|
|
5
|
|
|
use Behat\Mink\Element\NodeElement; |
6
|
|
|
use Behat\Mink\Exception\ExpectationException; |
7
|
|
|
use Behat\Mink\Exception\UnsupportedDriverActionException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class VisibilityContext. |
11
|
|
|
* |
12
|
|
|
* @package NuvoleWeb\Drupal\DrupalExtension\Context |
13
|
|
|
*/ |
14
|
|
|
class VisibilityContext extends RawMinkContext { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Assert presence of given element on the page. |
18
|
|
|
* |
19
|
|
|
* @Then the element :tag with text :text should be visible |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public function assertElementVisibility($tag, $text) { |
|
|
|
|
22
|
|
|
$element = $this->getSession()->getPage(); |
23
|
|
|
/** @var \Behat\Mink\Element\NodeElement[] $nodes */ |
24
|
|
|
$nodes = $element->findAll('css', $tag); |
25
|
|
|
foreach ($nodes as $node) { |
26
|
|
|
if ($node->getText() === $text) { |
27
|
|
|
$this->assertElementVisible($text, $node); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Assert absence of given element on the page. |
34
|
|
|
* |
35
|
|
|
* @Then the element :tag with text :text should not be visible |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function assertElementNonVisibility($tag, $text) { |
|
|
|
|
38
|
|
|
$element = $this->getSession()->getPage(); |
39
|
|
|
/** @var \Behat\Mink\Element\NodeElement[] $nodes */ |
40
|
|
|
$nodes = $element->findAll('css', $tag); |
41
|
|
|
foreach ($nodes as $node) { |
42
|
|
|
if ($node->getText() === $text) { |
43
|
|
|
$this->assertElementNotVisible($text, $node); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Assert presence of given field on the page. |
50
|
|
|
* |
51
|
|
|
* @Then I should see the field :field |
52
|
|
|
*/ |
53
|
|
|
public function iShouldSeeTheField($field) { |
54
|
|
|
$element = $this->getSession()->getPage(); |
55
|
|
|
$result = $element->findField($field); |
56
|
|
|
$this->assertElementVisible($field, $result); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Assert absence of given field. |
61
|
|
|
* |
62
|
|
|
* @Then I should not see the field :field |
63
|
|
|
*/ |
64
|
|
|
public function iShouldNotSeeTheField($field) { |
65
|
|
|
$element = $this->getSession()->getPage(); |
66
|
|
|
$this->assertElementNotVisible($field, $element->findField($field)); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Assert visibility of an element. |
71
|
|
|
* |
72
|
|
|
* @param string $element |
73
|
|
|
* Element selector or a string that describes it. |
74
|
|
|
* @param \Behat\Mink\Element\NodeElement $node |
75
|
|
|
* Node representing the element above, if any. |
76
|
|
|
* |
77
|
|
|
* @throws \Behat\Mink\Exception\ExpectationException |
78
|
|
|
* Throws exception if element not found. |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
protected function assertElementVisible($element, NodeElement $node) { |
|
|
|
|
81
|
|
|
try { |
82
|
|
|
if ($node && !$node->isVisible()) { |
83
|
|
|
throw new ExpectationException(sprintf("The element '%s' is not present on the page %s", $element, $this->getSession()->getCurrentUrl()), $this->getSession()); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
catch (UnsupportedDriverActionException $e) { |
87
|
|
|
// We catch the UnsupportedDriverActionException exception in case |
88
|
|
|
// this step is not being performed by a driver that supports javascript. |
89
|
|
|
// All other exceptions are valid. |
90
|
|
|
if (empty($node)) { |
91
|
|
|
throw new ExpectationException(sprintf("The element '%s' is not present on the page %s", $element, $this->getSession()->getCurrentUrl()), $this->getSession()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Assert non visibility of an element. |
98
|
|
|
* |
99
|
|
|
* @param string $element |
100
|
|
|
* Element selector or a string that describes it. |
101
|
|
|
* @param \Behat\Mink\Element\NodeElement $node |
102
|
|
|
* Node representing the element above, if any. |
103
|
|
|
* |
104
|
|
|
* @throws \Behat\Mink\Exception\ExpectationException |
105
|
|
|
* Throws exception if element is found. |
106
|
|
|
*/ |
107
|
|
View Code Duplication |
protected function assertElementNotVisible($element, NodeElement $node) { |
|
|
|
|
108
|
|
|
try { |
109
|
|
|
if ($node && $node->isVisible()) { |
110
|
|
|
throw new ExpectationException(sprintf("The field '%s' was present on the page %s and was not supposed to be", $element, $this->getSession()->getCurrentUrl()), $this->getSession()); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
catch (UnsupportedDriverActionException $e) { |
114
|
|
|
// We catch the UnsupportedDriverActionException exception in case |
115
|
|
|
// this step is not being performed by a driver that supports javascript. |
116
|
|
|
// All other exceptions are valid. |
117
|
|
|
if ($node) { |
118
|
|
|
throw new ExpectationException(sprintf("The field '%s' was present on the page %s and was not supposed to be", $element, $this->getSession()->getCurrentUrl()), $this->getSession()); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.