Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit abab472

Browse files
author
whhone
committed
[CompositeAction and Action Builder]
Support composite action. Only mouse actions are supported in this diff. Example: $builder = new WebDriverActions($driver); $action = $builder->click($element1) ->doubleClick($element2) ->contextClick($element1); ->perform();
1 parent a48ed4c commit abab472

20 files changed

+750
-1
lines changed

lib/WebDriverAction.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/**
17+
* Interface representing a single user-interaction action.
18+
*/
19+
interface WebDriverAction {
20+
21+
public function perform();
22+
}

lib/WebDriverHasInputDevices.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/**
17+
* Interface implemented by each driver that allows access to the input devices.
18+
*/
19+
interface WebDriverHasInputDevices {
20+
21+
/**
22+
* return WebDriverKeyBoard
23+
*/
24+
public function getKeyboard();
25+
26+
/**
27+
* return WebDriverMouse
28+
*/
29+
public function getMouse();
30+
}

lib/WebDriverMouse.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/**
17+
* Interface representing basic mouse operations.
18+
*/
19+
interface WebDriverMouse {
20+
21+
public function click(WebDriverCoordinates $where);
22+
23+
public function contextClick(WebDriverCoordinates $where);
24+
25+
public function doubleClick(WebDriverCoordinates $where);
26+
27+
public function mouseDown(WebDriverCoordinates $where);
28+
29+
public function mouseMove(WebDriverCoordinates $where,
30+
$x_offset,
31+
$y_offset);
32+
33+
public function mouseUp(WebDriverCoordinates $where);
34+
}

lib/__init__.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,37 @@
1414
// limitations under the License.
1515

1616
require_once('WebDriver.php');
17+
require_once('WebDriverAction.php');
1718
require_once('WebDriverAlert.php');
1819
require_once('WebDriverBy.php');
1920
require_once('WebDriverDimension.php');
2021
require_once('WebDriverElement.php');
2122
require_once('WebDriverExceptions.php');
2223
require_once('WebDriverExpectedCondition.php');
24+
require_once('WebDriverHasInputDevices.php');
2325
require_once('WebDriverKeys.php');
2426
require_once('WebDriverNavigation.php');
27+
require_once('WebDriverMouse.php');
2528
require_once('WebDriverOptions.php');
2629
require_once('WebDriverPoint.php');
2730
require_once('WebDriverSelect.php');
2831
require_once('WebDriverTargetLocator.php');
2932
require_once('WebDriverTimeouts.php');
3033
require_once('WebDriverWait.php');
3134
require_once('WebDriverWindow.php');
35+
require_once('interactions/WebDriverActions.php');
36+
require_once('interactions/WebDriverCompositeAction.php');
37+
require_once('interactions/internal/WebDriverButtonReleaseAction.php');
38+
require_once('interactions/internal/WebDriverClickAction.php');
39+
require_once('interactions/internal/WebDriverClickAndHoldAction.php');
40+
require_once('interactions/internal/WebDriverContextClickAction.php');
41+
require_once('interactions/internal/WebDriverCoordinates.php');
42+
require_once('interactions/internal/WebDriverDoubleClickAction.php');
43+
require_once('interactions/internal/WebDriverMouseAction.php');
44+
require_once('interactions/internal/WebDriverMouseMoveAction.php');
45+
require_once('interactions/internal/WebDriverMoveToOffsetAction.php');
46+
require_once('internal/WebDriverLocatable.php');
47+
require_once('remote/RemoteMouse.php');
3248
require_once('remote/RemoteWebDriver.php');
3349
require_once('remote/RemoteWebElement.php');
3450
require_once('remote/WebDriverBrowserType.php');

lib/interactions/WebDriverActions.php

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/**
17+
* WebDriver action builder. It implements the builder pattern.
18+
*/
19+
class WebDriverActions {
20+
21+
protected WebDriver $driver;
22+
protected WebDriverMouse $mouse;
23+
protected WebDriverCompositeAction $action;
24+
25+
// TODO: add keyboard actions
26+
// protected WebDriverKeyboard $keyboard;
27+
28+
public function __construct(WebDriver $driver) {
29+
$this->driver = $driver;
30+
$this->mouse = $driver->getMouse();
31+
$this->action = new WebDriverCompositeAction();
32+
}
33+
34+
/**
35+
* A convenience method for performing the actions without calling build().
36+
*/
37+
public function perform() {
38+
$this->action->perform();
39+
}
40+
41+
/**
42+
* Mouse click.
43+
* If $element is provided, move to the middle of the element first.
44+
*/
45+
public function click(WebDriverElement $element = null) {
46+
$this->action->addAction(
47+
new WebDriverClickAction($this->mouse, $element)
48+
);
49+
return $this;
50+
}
51+
52+
/**
53+
* Mouse click and hold.
54+
* If $element is provided, move to the middle of the element first.
55+
*/
56+
public function clickAndHold(WebDriverElement $element = null) {
57+
$this->action->addAction(
58+
new WebDriverClickAndHoldAction($this->mouse, $element)
59+
);
60+
return $this;
61+
}
62+
63+
/**
64+
* Context-click (right click).
65+
* If $element is provided, move to the middle of the element first.
66+
*/
67+
public function contextClick(WebDriverElement $element = null) {
68+
$this->action->addAction(
69+
new WebDriverContextClickAction($this->mouse, $element)
70+
);
71+
return $this;
72+
}
73+
74+
/**
75+
* Double click.
76+
* If $element is provided, move to the middle of the element first.
77+
*/
78+
public function doubleClick(WebDriverElement $element = null) {
79+
$this->action->addAction(
80+
new WebDriverDoubleClickAction($this->mouse, $element)
81+
);
82+
return $this;
83+
}
84+
85+
/**
86+
* Drag and drop from $source to $target.
87+
*/
88+
public function dragAndDrop(WebDriverElement $source,
89+
WebDriverElement $target) {
90+
$this->action->addAction(
91+
new WebDriverClickAndHoldAction($this->mouse, $source)
92+
);
93+
$this->action->addAction(
94+
new WebDriverMouseMoveAction($this->mouse, $target)
95+
);
96+
$this->action->addAction(
97+
new WebDriverButtonReleaseAction($this->mouse, $source)
98+
);
99+
return $this;
100+
}
101+
102+
/**
103+
* Drag $source and drop by offset ($x_offset, $y_offset).
104+
*/
105+
public function dragAndDropBy(WebDriverElement $source,
106+
$x_offset,
107+
$y_offset) {
108+
$this->action->addAction(
109+
new WebDriverClickAndHoldAction($this->mouse, $source)
110+
);
111+
$this->action->addAction(
112+
new WebDriverMoveToOffsetAction($this->mouse, null, $x_offset, $y_offset)
113+
);
114+
$this->action->addAction(
115+
new WebDriverButtonReleaseAction($this->mouse, null)
116+
);
117+
return $this;
118+
}
119+
120+
/**
121+
* Mouse move by offset.
122+
*/
123+
public function moveByOffset($x_offset, $y_offset) {
124+
$this->action->addAction(
125+
new WebDriverMoveToOffsetAction($this->mouse, null, $x_offset, $y_offset)
126+
);
127+
return $this;
128+
}
129+
130+
/**
131+
* Move to the middle of the given WebDriverElement. If offset are provided,
132+
* move the an offset from the top-left cornerof that element.
133+
*/
134+
public function moveToElement(WebDriverElement $element,
135+
$x_offset = null,
136+
$y_offset = null) {
137+
$this->action->addAction(new WebDriverMoveToOffsetAction(
138+
$this->mouse, $element, $x_offset, $y_offset
139+
));
140+
}
141+
142+
/**
143+
* Release the mouse button.
144+
* If $element is provided, move to the middle of the element first.
145+
*/
146+
public function release(WebDriverElement $element = null) {
147+
$this->action->addAction(
148+
new WebDriverButtonReleaseAction($this->mouse, $element)
149+
);
150+
return $this;
151+
}
152+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/**
17+
* An action for aggregating actions and triggering all of them afterwards.
18+
*/
19+
class WebDriverCompositeAction implements WebDriverAction {
20+
21+
private $actions = array();
22+
23+
/**
24+
* Add an WebDriverAction to the sequence.
25+
*
26+
* @return WebDriverCompositeAction The current instance.
27+
*/
28+
public function addAction(WebDriverAction $action) {
29+
$this->actions[] = $action;
30+
return $this;
31+
}
32+
33+
/**
34+
* Get the number of actions in the sequence.
35+
*
36+
* @return int The number of actions.
37+
*/
38+
public function getNumberOfActions() {
39+
return count($this->actions);
40+
}
41+
42+
/**
43+
* Perform the seqeunce of actions.
44+
*/
45+
public function perform() {
46+
foreach ($this->actions as $action) {
47+
$action->perform();
48+
}
49+
}
50+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
/**
17+
* Move to the location and then release the mouse key.
18+
*/
19+
class WebDriverButtonReleaseAction
20+
extends WebDriverMouseAction
21+
implements WebDriverAction {
22+
23+
public function perform() {
24+
$this->mouse->mouseUp($this->getActionLocation());
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
class WebDriverClickAction
17+
extends WebDriverMouseAction
18+
implements WebDriverAction {
19+
20+
public function perform() {
21+
$this->mouse->click($this->getActionLocation());
22+
}
23+
}

0 commit comments

Comments
 (0)