Repository navigation
Expand file tree
/
Copy pathLocator.php
More file actions
204 lines (193 loc) · 5.27 KB
/
Copy pathLocator.php
File metadata and controls
204 lines (193 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace Codeception\Util;
use Symfony\Component\CssSelector\CssSelectorConverter;
use Symfony\Component\CssSelector\Exception\ParseException;
use Symfony\Component\CssSelector\XPath\Translator;
/**
* Set of useful functions for using CSS and XPath locators.
* Please check them before writing complex functional or acceptance tests.
*
*/
class Locator
{
/**
* Applies OR operator to any number of CSS or XPath selectors.
* You can mix up CSS and XPath selectors here.
*
* ```php
* <?php
* use \Codeception\Util\Locator;
*
* $I->see('Title', Locator::combine('h1','h2','h3'));
* ?>
* ```
*
* This will search for `Title` text in either `h1`, `h2`, or `h3` tag. You can also combine CSS selector with XPath locator:
*
* ```php
* <?php
* use \Codeception\Util\Locator;
*
* $I->fillField(Locator::combine('form input[type=text]','//form/textarea[2]'), 'qwerty');
* ?>
* ```
*
* As a result the Locator will produce a mixed XPath value that will be used in fillField action.
*
* @static
* @param $selector1
* @param $selector2
* @throws \Exception
* @return string
*/
public static function combine($selector1, $selector2)
{
$selectors = func_get_args();
foreach ($selectors as $k => $v) {
$selectors[$k] = self::toXPath($v);
if (!$selectors[$k]) {
throw new \Exception("$v is invalid CSS or XPath");
}
}
return implode(' | ', $selectors);
}
/**
* Matches the *a* element with given URL
*
* ```php
* <?php
* use \Codeception\Util\Locator;
*
* $I->see('Log In', Locator::href('/login.php'));
* ?>
* ```
*
* @static
* @param $url
* @return string
*/
public static function href($url)
{
return sprintf('//a[@href=normalize-space(%s)]', Translator::getXpathLiteral($url));
}
/**
* Matches the element with given tab index
*
* Do you often use the `TAB` key to navigate through the web page? How do your site respond to this navigation?
* You could try to match elements by their tab position using `tabIndex` method of `Locator` class.
* ```php
* <?php
* use \Codeception\Util\Locator;
*
* $I->fillField(Locator::tabIndex(1), 'davert');
* $I->fillField(Locator::tabIndex(2) , 'qwerty');
* $I->click('Login');
* ?>
* ```
*
* @static
* @param $index
* @return string
*/
public static function tabIndex($index)
{
return sprintf('//*[@tabindex = normalize-space(%d)]', $index);
}
/**
* Matches option by text
*
* @param $value
*
* @return string
*/
public static function option($value)
{
return sprintf('//option[.=normalize-space("%s")]', $value);
}
protected static function toXPath($selector)
{
try {
$xpath = (new CssSelectorConverter())->toXPath($selector);
return $xpath;
} catch (ParseException $e) {
if (self::isXPath($selector)) {
return $selector;
}
}
return null;
}
/**
* Finds element by it's attribute(s)
*
* @static
*
* @param $element
* @param $attributes
*
* @return string
*/
public static function find($element, array $attributes)
{
$operands = [];
foreach ($attributes as $attribute => $value) {
if (is_int($attribute)) {
$operands[] = '@' . $value;
} else {
$operands[] = '@' . $attribute . ' = ' . Translator::getXpathLiteral($value);
}
}
return sprintf('//%s[%s]', $element, implode(' and ', $operands));
}
/**
* @param $selector
* @return bool
*/
public static function isCSS($selector)
{
try {
(new CssSelectorConverter())->toXPath($selector);
} catch (ParseException $e) {
return false;
}
return true;
}
/**
* Checks that locator is an XPath
*
* @param $locator
* @return bool
*/
public static function isXPath($locator)
{
$document = new \DOMDocument('1.0', 'UTF-8');
$xpath = new \DOMXPath($document);
return @$xpath->evaluate($locator, $document) !== false;
}
/**
* Checks that string and CSS selector for element by ID
*
*/
public static function isID($id)
{
return (bool)preg_match('~^#[\w\.\-\[\]\=\^\~\:]+$~', $id);
}
public static function humanReadableString($selector)
{
if (is_string($selector)) {
return "'$selector'";
}
if (is_array($selector)) {
$type = strtolower(key($selector));
$locator = $selector[$type];
return "$type '$locator'";
}
if (class_exists('\Facebook\WebDriver\WebDriverBy')) {
if ($selector instanceof \Facebook\WebDriver\WebDriverBy) {
$type = $selector->getMechanism();
$locator = $selector->getValue();
return "$type '$locator'";
}
}
throw new \InvalidArgumentException("Unrecognized selector");
}
}