/
WebDriverActions.php
251 lines (233 loc) · 7.32 KB
/
WebDriverActions.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
// Copyright 2004-present Facebook. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Facebook\WebDriver\Interactions;
use Facebook\WebDriver\Interactions\Internal\WebDriverClickAndHoldAction;
use Facebook\WebDriver\Interactions\Internal\WebDriverMoveToOffsetAction;
use Facebook\WebDriver\Interactions\Internal\WebDriverButtonReleaseAction;
use Facebook\WebDriver\Interactions\Internal\WebDriverDoubleClickAction;
use Facebook\WebDriver\Interactions\Internal\WebDriverSendKeysAction;
use Facebook\WebDriver\Interactions\Internal\WebDriverClickAction;
use Facebook\WebDriver\Interactions\Internal\WebDriverKeyDownAction;
use Facebook\WebDriver\Interactions\Internal\WebDriverContextClickAction;
use Facebook\WebDriver\Interactions\Internal\WebDriverKeyUpAction;
use Facebook\WebDriver\Interactions\Internal\WebDriverMouseMoveAction;
use Facebook\WebDriver\WebDriverElement;
use Facebook\WebDriver\WebDriver;
/**
* WebDriver action builder. It implements the builder pattern.
*/
class WebDriverActions {
protected $driver;
protected $keyboard;
protected $mouse;
protected $action;
public function __construct(WebDriver $driver) {
$this->driver = $driver;
$this->keyboard = $driver->getKeyboard();
$this->mouse = $driver->getMouse();
$this->action = new WebDriverCompositeAction();
}
/**
* A convenience method for performing the actions without calling build().
* @return void
*/
public function perform() {
$this->action->perform();
}
/**
* Mouse click.
* If $element is provided, move to the middle of the element first.
*
* @param WebDriverElement $element
* @return WebDriverActions
*/
public function click(WebDriverElement $element = null) {
$this->action->addAction(
new WebDriverClickAction($this->mouse, $element)
);
return $this;
}
/**
* Mouse click and hold.
* If $element is provided, move to the middle of the element first.
*
* @param WebDriverElement $element
* @return WebDriverActions
*/
public function clickAndHold(WebDriverElement $element = null) {
$this->action->addAction(
new WebDriverClickAndHoldAction($this->mouse, $element)
);
return $this;
}
/**
* Context-click (right click).
* If $element is provided, move to the middle of the element first.
*
* @param WebDriverElement $element
* @return WebDriverActions
*/
public function contextClick(WebDriverElement $element = null) {
$this->action->addAction(
new WebDriverContextClickAction($this->mouse, $element)
);
return $this;
}
/**
* Double click.
* If $element is provided, move to the middle of the element first.
*
* @param WebDriverElement $element
* @return WebDriverActions
*/
public function doubleClick(WebDriverElement $element = null) {
$this->action->addAction(
new WebDriverDoubleClickAction($this->mouse, $element)
);
return $this;
}
/**
* Drag and drop from $source to $target.
*
* @param WebDriverElement $source
* @param WebDriverElement $target
* @return WebDriverActions
*/
public function dragAndDrop(WebDriverElement $source,
WebDriverElement $target) {
$this->action->addAction(
new WebDriverClickAndHoldAction($this->mouse, $source)
);
$this->action->addAction(
new WebDriverMouseMoveAction($this->mouse, $target)
);
$this->action->addAction(
new WebDriverButtonReleaseAction($this->mouse, $target)
);
return $this;
}
/**
* Drag $source and drop by offset ($x_offset, $y_offset).
*
* @param WebDriverElement $source
* @param int $x_offset
* @param int $y_offset
* @return WebDriverActions
*/
public function dragAndDropBy(WebDriverElement $source,
$x_offset,
$y_offset) {
$this->action->addAction(
new WebDriverClickAndHoldAction($this->mouse, $source)
);
$this->action->addAction(
new WebDriverMoveToOffsetAction($this->mouse, null, $x_offset, $y_offset)
);
$this->action->addAction(
new WebDriverButtonReleaseAction($this->mouse, null)
);
return $this;
}
/**
* Mouse move by offset.
*
* @param int $x_offset
* @param int $y_offset
* @return WebDriverActions
*/
public function moveByOffset($x_offset, $y_offset) {
$this->action->addAction(
new WebDriverMoveToOffsetAction($this->mouse, null, $x_offset, $y_offset)
);
return $this;
}
/**
* Move to the middle of the given WebDriverElement. If offset are provided,
* move the an offset from the top-left cornerof that element.
*
* @param WebDriverElement $element
* @param int $x_offset
* @param int $y_offset
* @return WebDriverActions
*/
public function moveToElement(WebDriverElement $element,
$x_offset = null,
$y_offset = null) {
$this->action->addAction(new WebDriverMoveToOffsetAction(
$this->mouse, $element, $x_offset, $y_offset
));
return $this;
}
/**
* Release the mouse button.
* If $element is provided, move to the middle of the element first.
*
* @param WebDriverElement $element
* @return WebDriverActions
*/
public function release(WebDriverElement $element = null) {
$this->action->addAction(
new WebDriverButtonReleaseAction($this->mouse, $element)
);
return $this;
}
/**
* Press a key on keyboard.
* If $element is provided, focus on that element first.
*
* @see WebDriverKeys for special keys like CONTROL, ALT, etc.
* @param WebDriverElement $element
* @param string $key
* @return WebDriverActions
*/
public function keyDown(WebDriverElement $element = null, $key = null) {
$this->action->addAction(
new WebDriverKeyDownAction($this->keyboard, $this->mouse, $element, $key)
);
return $this;
}
/**
* Release a key on keyboard.
* If $element is provided, focus on that element first.
*
* @see WebDriverKeys for special keys like CONTROL, ALT, etc.
* @param WebDriverElement $element
* @param string $key
* @return WebDriverActions
*/
public function keyUp(WebDriverElement $element = null, $key = null) {
$this->action->addAction(
new WebDriverKeyUpAction($this->keyboard, $this->mouse, $element, $key)
);
return $this;
}
/**
* Send keys by keyboard.
* If $element is provided, focus on that element first.
*
* @see WebDriverKeys for special keys like CONTROL, ALT, etc.
* @param WebDriverElement $element
* @param string $keys
* @return WebDriverActions
*/
public function sendKeys(WebDriverElement $element = null, $keys = null) {
$this->action->addAction(
new WebDriverSendKeysAction(
$this->keyboard, $this->mouse, $element, $keys
)
);
return $this;
}
}