-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathInteractsWithElements.php
More file actions
232 lines (186 loc) · 5.04 KB
/
InteractsWithElements.php
File metadata and controls
232 lines (186 loc) · 5.04 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
declare(strict_types=1);
namespace Pest\Browser\Api\Concerns;
use Pest\Browser\Api\Webpage;
/**
* @mixin Webpage
*/
trait InteractsWithElements
{
/**
* Click an element using smart selector detection (id, class, data-test, submit, text, etc).
*/
public function click(string $selector): self
{
$this->guessLocator($selector)->click();
return $this;
}
/**
* Get the text of the element matching the given selector.
*/
public function text(string $selector): ?string
{
return $this->guessLocator($selector)->textContent();
}
/**
* Get the given attribute from the element matching the given selector.
*/
public function attribute(string $selector, string $attribute): ?string
{
$locator = $this->guessLocator($selector);
return $locator->getAttribute($attribute) ?? null;
}
/**
* Send the given keys to the element matching the given selector.
*
* @param array<int, string> $keys
*/
public function keys(string $selector, array|string $keys): self
{
$keys = is_array($keys) ? $keys : [$keys];
$locator = $this->guessLocator($selector);
foreach ($keys as $key) {
$locator->press($key);
}
return $this;
}
/**
* Type the given value in the given field.
*/
public function type(string $field, string $value): self
{
$this->guessLocator($field)->fill($value);
return $this;
}
/**
* Type the given value slowly in the given field.
*/
public function typeSlowly(string $field, string $value, int $delay = 100): self
{
$options = ['delay' => $delay];
$this->guessLocator($field)->type($value, $options);
return $this;
}
/**
* Fills the given value in the given field.
*/
public function fill(string $field, string $value): self
{
return $this->type($field, $value);
}
/**
* Hovers over the element matching the given selector.
*/
public function hover(string $selector): self
{
$this->guessLocator($selector)->hover();
return $this;
}
/**
* Right-click the element matching the given selector.
*/
public function rightClick(string $text): Webpage
{
$this->guessLocator($text)->click([
'button' => 'right',
]);
return $this;
}
/**
* Select the given value in the given field.
*
* @param array<int, string|int>|string|int $option
*/
public function select(string $field, array|string|int $option): self
{
$this->guessLocator($field)->selectOption($option);
return $this;
}
/**
* Type the given value in the given field without clearing it.
*/
public function append(string $field, string $value): self
{
$locator = $this->guessLocator($field);
$currentValue = $locator->inputValue();
$locator->fill($currentValue.$value);
return $this;
}
/**
* Clear the given field.
*/
public function clear(string $field): self
{
$this->guessLocator($field)->clear();
return $this;
}
/**
* Select the given value of a radio button field.
*/
public function radio(string $field, string $value): self
{
$this->guessLocator($field, $value)->click();
return $this;
}
/**
* Check the given checkbox.
*/
public function check(string $field, ?string $value = null): self
{
$this->guessLocator($field, $value)->check();
return $this;
}
/**
* Uncheck the given checkbox.
*/
public function uncheck(string $field, ?string $value = null): self
{
$this->guessLocator($field, $value)->uncheck();
return $this;
}
/**
* Attach the given file to the field.
*/
public function attach(string $field, string $path): self
{
$this->guessLocator($field)->setInputFiles($path);
return $this;
}
/**
* Press the button with the given text or name.
*/
public function press(string $button): self
{
return $this->click($button);
}
/**
* Press the button with the given text or name.
*/
public function pressAndWaitFor(string $button, int|float $seconds = 1): self
{
return $this->press($button)->wait($seconds);
}
/**
* Drag an element to another element using selectors.
*/
public function drag(string $from, string $to): self
{
$fromLocator = $this->guessLocator($from);
$toLocator = $this->guessLocator($to);
$fromLocator->dragTo($toLocator);
return $this;
}
/**
* Hold down key while running callback
*/
public function withKeyDown(string $key, callable $callback): self
{
$this->page->keyDown($key);
try {
$callback($this);
} finally {
$this->page->keyUp($key);
}
return $this;
}
}