-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
FieldTrait.php
173 lines (155 loc) · 5.06 KB
/
FieldTrait.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
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
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\ElementNotFoundException;
/**
* Trait Field.
*
* Field-related steps.
*
* @package DrevOps\BehatSteps
*/
trait FieldTrait {
/**
* Assert that field exists on the page using id,name,label or value.
*
* @code
* Then I see field "Body"
* Then I see field "field_body"
* @endcode
*
* @Then I see field :name
*/
public function fieldAssertExists(string $field_name): NodeElement {
$page = $this->getSession()->getPage();
$field = $page->findField($field_name);
// Try to resolve by ID.
$field = $field ? $field : $page->findById($field_name);
if ($field === NULL) {
$exception = new ElementNotFoundException($this->getSession()
->getDriver(), 'form field', 'id|name|label|value', $field_name);
throw new \Exception($exception->getMessage());
}
return $field;
}
/**
* Assert that field does not exist on the page using id,name,label or value.
*
* @code
* Then I don't see field "Body"
* Then I don't see field "field_body"
* @endcode
*
* @Then I don't see field :name
*/
public function fieldAssertNotExists(string $field_name): void {
$page = $this->getSession()->getPage();
$field = $page->findField($field_name);
// Try to resolve by ID.
$field = $field ? $field : $page->findById($field_name);
if ($field !== NULL) {
throw new \Exception(sprintf('A field "%s" appears on this page, but it should not.', $field_name));
}
}
/**
* Assert whether the field exists on the page using id,name,label or value.
*
* @code
* Then field "Body" "exists" on the page
* Then field "field_body" "exists" on the page
* Then field "Tags" "does not exist" on the page
* Then field "field_tags" "does not exist" on the page
* @endcode
*
* @Then field :name :exists on the page
*/
public function fieldAssertExistence(string $field_name, string $exists): void {
if ($exists === 'exists') {
$this->fieldAssertExists($field_name);
}
else {
$this->fieldAssertNotExists($field_name);
}
}
/**
* Assert whether the field has a state.
*
* @code
* Then field "Body" is "disabled" on the page
* Then field "field_body" is "disabled" on the page
* Then field "Tags" is "enabled" on the page
* Then field "field_tags" is "not enabled" on the page
* @endcode
*
* @Then field :name is :disabled on the page
*/
public function fieldAssertState(string $field_name, string $disabled): void {
$field = $this->fieldAssertExists($field_name);
if ($disabled === 'disabled' && !$field->hasAttribute('disabled')) {
throw new \Exception(sprintf('A field "%s" should be disabled, but it is not.', $field_name));
}
elseif ($disabled !== 'disabled' && $field->hasAttribute('disabled')) {
throw new \Exception(sprintf('A field "%s" should not be disabled, but it is.', $field_name));
}
}
/**
* Assert whether the field exists on the page and has a state.
*
* @code
* Then field "Body" should be "present" on the page and have state "enabled"
* Then field "Tags" should be "absent" on the page and have state "n/a"
* @endcode
*
* @Then field :name should be :presence on the page and have state :state
*/
public function fieldAssertExistsState(string $field_name, string $presence, string $state = 'enabled'): void {
if ($presence === 'present') {
$this->fieldAssertExists($field_name);
$this->fieldAssertState($field_name, $state);
}
else {
$this->fieldAssertNotExists($field_name);
}
}
/**
* Fills value for color field.
*
* @When /^(?:|I )fill color in "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/
* @When /^(?:|I )fill color in "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/
*/
public function fillColorField(string $field, string $value = NULL): mixed {
$js = <<<JS
(function() {
var element = document.querySelector('$field');
if (!element) {
throw new Error('Element not found: $field');
}
element.value = '$value';
var event = new Event('change', { bubbles: true });
element.dispatchEvent(event);
})();
JS;
return $this->getSession()->evaluateScript($js);
}
/**
* Asserts that a color field has a value.
*
* @Then /^color field "(?P<field>(?:[^"]|\\")*)" value is "(?P<value>(?:[^"]|\\")*)"$/
*/
public function assertColorFieldHasValue(string $field, string $value): void {
$js = <<<JS
(function() {
var element = document.querySelector('$field');
if (!element) {
throw new Error('Element not found: $field');
}
return element.value;
})();
JS;
$actual = $this->getSession()->evaluateScript($js);
if ($actual != $value) {
throw new \Exception(sprintf('Color field "%s" expected a value "%s" but has a value "%s".', $field, $value, $actual));
}
}
}