-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ElementTrait.php
122 lines (103 loc) · 3.74 KB
/
ElementTrait.php
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
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Mink\Exception\ElementNotFoundException;
/**
* Trait Element.
*
* Steps to work with HTML element.
*
* @package DrevOps\BehatSteps
*/
trait ElementTrait {
/**
* Assert that an element with selector and attribute with a value exists.
*
* @Then I( should) see the :selector element with the :attribute attribute set to :value
*/
public function elementAssertAttributeHasValue(string $selector, string $attribute, mixed $value): void {
$page = $this->getSession()->getPage();
$elements = $page->findAll('css', $selector);
if (empty($elements)) {
throw new \Exception(sprintf('The "%s" element was not found on the page.', $selector));
}
$attr_found = FALSE;
$attr_value_found = FALSE;
foreach ($elements as $element) {
$attr = $element->getAttribute($attribute);
if (!empty($attr)) {
$attr_found = TRUE;
if (str_contains((string) $attr, strval($value))) {
$attr_value_found = TRUE;
break;
}
}
}
if (!$attr_value_found) {
if (!$attr_found) {
throw new \Exception(sprintf('The "%s" attribute was not found on the element "%s".', $attribute, $selector));
}
else {
throw new \Exception(sprintf('The "%s" attribute was found on the element "%s", but does not contain a value "%s".', $attribute, $selector, $value));
}
}
}
/**
* Assert element with wildcard pattern exists.
*
* Assert an element with selector and attribute with a value exists,
* matching a wildcard pattern.
*
* @Then I( should) see the :selector element with a(n) :attribute attribute containing :value
*/
public function elementAssertAttributeContains(string $selector, string $attribute, string $pattern): void {
$page = $this->getSession()->getPage();
$elements = $page->findAll('css', $selector);
if (empty($elements)) {
throw new \Exception(sprintf('The "%s" element was not found on the page.', $selector));
}
$attr_found = FALSE;
$attr_pattern_matched = FALSE;
foreach ($elements as $element) {
$attr = (string) $element->getAttribute($attribute);
if (!empty($attr)) {
$attr_found = TRUE;
// Convert wildcard pattern to regex.
$regex_pattern = '/' . str_replace(['*', '?'], ['.*', '.'], preg_quote($pattern, '/')) . '/';
if (preg_match($regex_pattern, $attr)) {
$attr_pattern_matched = TRUE;
break;
}
}
}
if (!$attr_found) {
throw new \Exception(sprintf('The "%s" attribute was not found on the element "%s".', $attribute, $selector));
}
if (!$attr_pattern_matched) {
throw new \Exception(sprintf('No element with "%s" attribute matching the pattern "%s" found.', $attribute, $pattern));
}
}
/**
* Assert that an element with selector contains text.
*
* @Then I should see an element :selector using :type contains :text text
*/
public function iShouldSeeAnElementUsingType(string $selector, string $type, string $text): void {
if ($type === 'css') {
$element = $this->getSession()->getPage()->find('css', $selector);
}
elseif ($type === 'xpath') {
$element = $this->getSession()->getPage()->find('xpath', $selector);
}
else {
throw new \Exception('Selector type must be "css" or "xpath".');
}
if (!$element) {
$exception = new ElementNotFoundException($this->getSession()->getDriver(), NULL, $type, $selector);
throw new \Exception($exception->getMessage());
}
if (!str_contains($element->getText(), $text)) {
throw new \Exception(sprintf('The text "%s" was not found in the element "%s" using %s.', $text, $selector, $type));
}
}
}