1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Sergii Bondarenko, <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
namespace Drupal\TqExtension\Context; |
6
|
|
|
|
7
|
|
|
// Contexts. |
8
|
|
|
use Drupal\DrupalExtension\Context\RawDrupalContext; |
9
|
|
|
// Exceptions. |
10
|
|
|
use WebDriver\Exception\NoSuchElement; |
11
|
|
|
// Helpers. |
12
|
|
|
use Behat\Mink\Element\NodeElement; |
13
|
|
|
// Utils. |
14
|
|
|
use Drupal\TqExtension\Utils\XPath; |
15
|
|
|
|
16
|
|
|
class RawPageContext extends RawDrupalContext |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var NodeElement |
20
|
|
|
*/ |
21
|
|
|
private static $workingElement; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return NodeElement |
25
|
|
|
*/ |
26
|
|
|
public function getWorkingElement() |
27
|
|
|
{ |
28
|
|
|
if (null === self::$workingElement) { |
29
|
|
|
$this->setWorkingElement($this->getBodyElement()); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return self::$workingElement; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param NodeElement $element |
37
|
|
|
*/ |
38
|
|
|
public function setWorkingElement(NodeElement $element) |
39
|
|
|
{ |
40
|
|
|
self::$workingElement = $element; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function unsetWorkingElement() |
44
|
|
|
{ |
45
|
|
|
self::$workingElement = null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $selector |
50
|
|
|
* |
51
|
|
|
* @return NodeElement |
52
|
|
|
*/ |
53
|
|
|
public function findByCss($selector) |
54
|
|
|
{ |
55
|
|
|
return $this->getWorkingElement() |
56
|
|
|
->find(empty($this->getDrupalParameter('region_map')[$selector]) ? 'css' : 'region', $selector); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $selector |
61
|
|
|
* |
62
|
|
|
* @return NodeElement|null |
63
|
|
|
*/ |
64
|
|
|
public function findField($selector) |
65
|
|
|
{ |
66
|
|
|
$selector = ltrim($selector, '#'); |
67
|
|
|
$element = $this->getWorkingElement(); |
68
|
|
|
|
69
|
|
|
foreach ($this->findLabels($selector) as $forAttribute => $label) { |
70
|
|
|
// We trying to find an ID with "-upload" suffix, because some |
71
|
|
|
// image inputs in Drupal are suffixed by it. |
72
|
|
|
foreach ([$forAttribute, "$forAttribute-upload"] as $elementID) { |
73
|
|
|
$field = $element->findById($elementID); |
74
|
|
|
|
75
|
|
|
if (null !== $field) { |
76
|
|
|
return $field; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $element->findField($selector); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $selector |
86
|
|
|
* |
87
|
|
|
* @return NodeElement |
88
|
|
|
*/ |
89
|
|
|
public function findButton($selector) |
90
|
|
|
{ |
91
|
|
|
$element = $this->getWorkingElement(); |
92
|
|
|
|
93
|
|
|
// Search inside of: "id", "name", "title", "alt" and "value" attributes. |
94
|
|
|
return $element->findButton($selector) |
95
|
|
|
?: (new XPath\InaccurateText('//button', $element))->text($selector)->find(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $text |
100
|
|
|
* |
101
|
|
|
* @return NodeElement|null |
102
|
|
|
*/ |
103
|
|
|
public function findByText($text) |
104
|
|
|
{ |
105
|
|
|
return (new XPath\InaccurateText('//*', $this->getWorkingElement()))->text($text)->find(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param string $locator |
110
|
|
|
* Element locator. Can be inaccurate text, inaccurate field label, CSS selector or region name. |
111
|
|
|
* |
112
|
|
|
* @throws NoSuchElement |
113
|
|
|
* |
114
|
|
|
* @return NodeElement |
115
|
|
|
*/ |
116
|
|
|
public function findElement($locator) |
117
|
|
|
{ |
118
|
|
|
return $this->findByCss($locator) |
119
|
|
|
?: $this->findField($locator) |
120
|
|
|
?: $this->findButton($locator) |
121
|
|
|
?: $this->findByText($locator); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Find all field labels by text. |
126
|
|
|
* |
127
|
|
|
* @param string $text |
128
|
|
|
* Label text. |
129
|
|
|
* |
130
|
|
|
* @return NodeElement[] |
131
|
|
|
*/ |
132
|
|
|
public function findLabels($text) |
133
|
|
|
{ |
134
|
|
|
$xpath = new XPath\InaccurateText('//label[@for]', $this->getWorkingElement()); |
135
|
|
|
$labels = []; |
136
|
|
|
|
137
|
|
|
foreach ($xpath->text($text)->findAll() as $label) { |
138
|
|
|
$labels[$label->getAttribute('for')] = $label; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $labels; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return NodeElement |
146
|
|
|
*/ |
147
|
|
|
public function getBodyElement() |
148
|
|
|
{ |
149
|
|
|
return $this->getSession()->getPage()->find('css', 'body'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $selector |
154
|
|
|
* Element selector. |
155
|
|
|
* @param mixed $element |
156
|
|
|
* Existing element or null. |
157
|
|
|
* |
158
|
|
|
* @throws NoSuchElement |
159
|
|
|
*/ |
160
|
|
|
public function throwNoSuchElementException($selector, $element) |
161
|
|
|
{ |
162
|
|
|
if (null === $element) { |
163
|
|
|
throw new NoSuchElement(sprintf('Cannot find an element by "%s" selector.', $selector)); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $locator |
169
|
|
|
* @param string $selector |
170
|
|
|
* |
171
|
|
|
* @throws \RuntimeException |
172
|
|
|
* @throws NoSuchElement |
173
|
|
|
* |
174
|
|
|
* @return NodeElement |
175
|
|
|
*/ |
176
|
|
|
public function element($locator, $selector) |
177
|
|
|
{ |
178
|
|
|
$map = [ |
179
|
|
|
'button' => 'Button', |
180
|
|
|
'field' => 'Field', |
181
|
|
|
'text' => 'ByText', |
182
|
|
|
'css' => 'ByCss', |
183
|
|
|
'*' => 'Element', |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
if (!isset($map[$locator])) { |
187
|
|
|
throw new \RuntimeException(sprintf('Locator "%s" was not specified.')); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$selector = t($selector); |
191
|
|
|
$element = $this->{'find' . $map[$locator]}($selector); |
192
|
|
|
$this->throwNoSuchElementException($selector, $element); |
193
|
|
|
|
194
|
|
|
return $element; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: