Skip to content

Commit b4692ea

Browse files
committed
java: Implementing action chains in marionette-style (that does not conform to the standard yet)
1 parent da83f63 commit b4692ea

20 files changed

+141
-34
lines changed

Diff for: java/client/src/org/openqa/selenium/BUCK

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ java_library(name = 'core',
4040
'UnexpectedAlertBehaviour.java',
4141
'WebDriver.java',
4242
'WebElement.java',
43+
'internal/HasIdentity.java',
4344
'internal/Killable.java',
4445
'internal/Locatable.java',
4546
'internal/Lock.java',
@@ -135,4 +136,4 @@ java_binary(name = 'selenium-java',
135136
'//java/client/src/org/openqa/selenium/safari:safari',
136137
'//java/client/src/org/openqa/selenium/support:support',
137138
],
138-
)
139+
)

Diff for: java/client/src/org/openqa/selenium/build.desc

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ java_library(name = "webdriver-api",
7070
"internal/FindsByName.java",
7171
"internal/FindsByTagName.java",
7272
"internal/FindsByXPath.java",
73+
"internal/HasIdentity.java",
7374
"internal/Killable.java",
7475
"internal/Locatable.java",
7576
"internal/Lock.java",

Diff for: java/client/src/org/openqa/selenium/firefox/internal/MarionetteConnection.java

+8-31
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public class MarionetteConnection implements ExtensionConnection, NeedsLocalLogs
7676
.put(DriverCommand.GET_ELEMENT_LOCATION, "getElementPosition")
7777
.put(DriverCommand.GET_ALL_COOKIES, "getAllCookies")
7878
.put(DriverCommand.QUIT, "deleteSession")
79+
.put(DriverCommand.MOVE_TO, "move")
80+
.put(DriverCommand.MOUSE_DOWN, "press")
81+
.put(DriverCommand.MOUSE_UP, "release")
82+
.put(DriverCommand.CLICK, "click")
7983
.build();
8084

8185
private Socket socket;
@@ -195,39 +199,11 @@ public Response execute(Command command) throws IOException {
195199
response.setValue(map.get("value"));
196200

197201
} else {
202+
response = new JsonToBeanConverter().convert(Response.class, rawResponse);
198203
if (map.containsKey("error")) {
199-
// ***************************************************************
200-
// Marionette Compliance Issue: Error responses should, at a
201-
// minimum, put the status property at the root of the object.
202-
// In other words:
203-
// {
204-
// status: 7,
205-
// value:
206-
// {
207-
// message: "Did not find element with id=foo",
208-
// stackTrace: <stack trace goes here>
209-
// }
210-
// }
211-
// ***************************************************************
212-
response = new Response();
213-
Object value = map.get("error");
214-
if (value != null) {
215-
if (value instanceof Map) {
216-
Map<String, Object> errorMap = (Map<String, Object>) value;
217-
if (errorMap != null) {
218-
response.setStatus(Integer.parseInt(errorMap.get("status").toString()));
219-
errorMap.remove("status");
220-
response.setValue(errorMap);
221-
}
222-
} else {
223-
response.setStatus(ErrorCodes.UNHANDLED_ERROR);
224-
response.setValue(value + ": " + map.get("message"));
225-
}
226-
}
204+
response.setValue(map.get("error"));
227205

228206
} else {
229-
response = new JsonToBeanConverter().convert(Response.class, rawResponse);
230-
231207
// ***************************************************************
232208
// Marionette Compliance Issue: Responses from findElements
233209
// are returned with raw element IDs as the value.
@@ -280,7 +256,8 @@ private String serializeCommand(Command command) {
280256
|| DriverCommand.MOUSE_DOWN.equals(commandName)
281257
|| DriverCommand.MOUSE_UP.equals(commandName)
282258
|| DriverCommand.MOVE_TO.equals(commandName)) {
283-
String actionName = commandName;
259+
String actionName = seleniumToMarionetteCommandMap.containsKey(commandName) ?
260+
seleniumToMarionetteCommandMap.get(commandName) : commandName;
284261
commandName = "actionChain";
285262
List<Object> action = Lists.newArrayList();
286263
action.add(actionName);

Diff for: java/client/src/org/openqa/selenium/interactions/BUCK

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ java_library(name = 'interactions',
66
':core',
77
':exceptions',
88
'//java/client/src/org/openqa/selenium:core',
9+
'//java/client/src/org/openqa/remote:remote',
910
],
1011
deps = [
1112
'//third_party/java/guava-libraries',
@@ -40,6 +41,5 @@ java_library(name = 'exceptions',
4041
],
4142
visibility = [
4243
'//java/client/src/org/openqa/selenium:webdriver-api',
43-
'//java/client/src/org/openqa/selenium/remote:remote',
4444
],
4545
)

Diff for: java/client/src/org/openqa/selenium/interactions/ButtonReleaseAction.java

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.openqa.selenium.interactions.internal.MouseAction;
2121
import org.openqa.selenium.internal.Locatable;
2222

23+
import java.util.Arrays;
24+
import java.util.List;
25+
2326
/**
2427
* Releases the left mouse button
2528
*
@@ -40,4 +43,8 @@ public void perform() {
4043
moveToLocation();
4144
mouse.mouseUp(getActionLocation());
4245
}
46+
47+
public List<Object> asList() {
48+
return Arrays.<Object>asList("release");
49+
}
4350
}

Diff for: java/client/src/org/openqa/selenium/interactions/ClickAction.java

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.openqa.selenium.interactions.internal.MouseAction;
2121
import org.openqa.selenium.internal.Locatable;
2222

23+
import java.util.Arrays;
24+
import java.util.List;
25+
2326
/**
2427
* clicks an element.
2528
*
@@ -33,4 +36,8 @@ public void perform() {
3336
moveToLocation();
3437
mouse.click(getActionLocation());
3538
}
39+
40+
public List<Object> asList() {
41+
return Arrays.<Object>asList("click", getTargetId(), Button.LEFT, 1);
42+
}
3643
}

Diff for: java/client/src/org/openqa/selenium/interactions/ClickAndHoldAction.java

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.openqa.selenium.interactions.internal.MouseAction;
2121
import org.openqa.selenium.internal.Locatable;
2222

23+
import java.util.Arrays;
24+
import java.util.List;
25+
2326
/**
2427
* Presses the left mouse button without releasing it.
2528
*
@@ -38,4 +41,8 @@ public void perform() {
3841
moveToLocation();
3942
mouse.mouseDown(getActionLocation());
4043
}
44+
45+
public List<Object> asList() {
46+
return Arrays.<Object>asList("press", getTargetId());
47+
}
4148
}

Diff for: java/client/src/org/openqa/selenium/interactions/ContextClickAction.java

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.openqa.selenium.interactions.internal.MouseAction;
2121
import org.openqa.selenium.internal.Locatable;
2222

23+
import java.util.Arrays;
24+
import java.util.List;
25+
2326
/**
2427
* Context-clicks an element
2528
*
@@ -38,4 +41,7 @@ public void perform() {
3841
mouse.contextClick(getActionLocation());
3942
}
4043

44+
public List<Object> asList() {
45+
return Arrays.<Object>asList("click", getTargetId(), Button.RIGHT, 1);
46+
}
4147
}

Diff for: java/client/src/org/openqa/selenium/interactions/DoubleClickAction.java

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.openqa.selenium.interactions.internal.MouseAction;
2121
import org.openqa.selenium.internal.Locatable;
2222

23+
import java.util.Arrays;
24+
import java.util.List;
25+
2326
/**
2427
* Double-clicks an element.
2528
*
@@ -36,4 +39,8 @@ public void perform() {
3639
moveToLocation();
3740
mouse.doubleClick(getActionLocation());
3841
}
42+
43+
public List<Object> asList() {
44+
return Arrays.<Object>asList("click", getTargetId(), Button.LEFT, 2);
45+
}
3946
}

Diff for: java/client/src/org/openqa/selenium/interactions/KeyDownAction.java

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import org.openqa.selenium.interactions.internal.SingleKeyAction;
2222
import org.openqa.selenium.internal.Locatable;
2323

24+
import java.util.Arrays;
25+
import java.util.List;
26+
2427
/**
2528
* Emulates key press only, without the release.
2629
*
@@ -39,4 +42,8 @@ public void perform() {
3942

4043
keyboard.pressKey(key);
4144
}
45+
46+
public List<Object> asList() {
47+
return Arrays.<Object>asList("keyDown", key);
48+
}
4249
}

Diff for: java/client/src/org/openqa/selenium/interactions/KeyUpAction.java

+7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import org.openqa.selenium.interactions.internal.SingleKeyAction;
2222
import org.openqa.selenium.internal.Locatable;
2323

24+
import java.util.Arrays;
25+
import java.util.List;
26+
2427
/**
2528
* Emulates key release only, without the press.
2629
*
@@ -39,4 +42,8 @@ public void perform() {
3942

4043
keyboard.releaseKey(key);
4144
}
45+
46+
public List<Object> asList() {
47+
return Arrays.<Object>asList("keyUp", key);
48+
}
4249
}

Diff for: java/client/src/org/openqa/selenium/interactions/MoveMouseAction.java

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.openqa.selenium.interactions.internal.MouseAction;
2121
import org.openqa.selenium.internal.Locatable;
2222

23+
import java.util.Arrays;
24+
import java.util.List;
25+
2326
/**
2427
* Moves the mouse to an element.
2528
*
@@ -35,4 +38,8 @@ public MoveMouseAction(Mouse mouse, Locatable locationProvider) {
3538
public void perform() {
3639
mouse.mouseMove(getActionLocation());
3740
}
41+
42+
public List<Object> asList() {
43+
return Arrays.<Object>asList("move", getTargetId());
44+
}
3845
}

Diff for: java/client/src/org/openqa/selenium/interactions/MoveToOffsetAction.java

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import org.openqa.selenium.interactions.internal.MouseAction;
2121
import org.openqa.selenium.internal.Locatable;
2222

23+
import java.util.Arrays;
24+
import java.util.List;
25+
2326
/**
2427
* Move the mouse to a location within the element provided. The coordinates provided specify the
2528
* offset from the top-left corner of the element.
@@ -37,4 +40,8 @@ public MoveToOffsetAction(Mouse mouse, Locatable locationProvider, int x, int y)
3740
public void perform() {
3841
mouse.mouseMove(getActionLocation(), xOffset, yOffset);
3942
}
43+
44+
public List<Object> asList() {
45+
return Arrays.<Object>asList("move", getTargetId(), xOffset, yOffset);
46+
}
4047
}

Diff for: java/client/src/org/openqa/selenium/interactions/PauseAction.java

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.openqa.selenium.interactions;
1919

20+
import java.util.Arrays;
21+
import java.util.List;
22+
2023
/**
2124
* Takes a pause.
2225
*
@@ -37,4 +40,8 @@ public void perform() {
3740
} catch (InterruptedException e) {
3841
}
3942
}
43+
44+
public List<Object> asList() {
45+
return Arrays.<Object>asList("wait", pause);
46+
}
4047
}

Diff for: java/client/src/org/openqa/selenium/interactions/internal/BaseAction.java

+21
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
package org.openqa.selenium.interactions.internal;
1919

20+
import org.openqa.selenium.WebElement;
21+
import org.openqa.selenium.internal.HasIdentity;
2022
import org.openqa.selenium.internal.Locatable;
23+
import org.openqa.selenium.internal.WrapsElement;
2124

2225
/**
2326
* Base class for all actions.
@@ -40,4 +43,22 @@ protected BaseAction(Locatable actionLocation) {
4043
protected BaseAction() {
4144
this.where = null;
4245
}
46+
47+
protected String getTargetId() {
48+
if (!(where instanceof WebElement)) {
49+
return null;
50+
}
51+
52+
WebElement target = (WebElement) where;
53+
54+
while (target instanceof WrapsElement) {
55+
target = ((WrapsElement) target).getWrappedElement();
56+
}
57+
58+
if (target instanceof HasIdentity) {
59+
return ((HasIdentity) target).getId();
60+
}
61+
62+
return null;
63+
}
4364
}

Diff for: java/client/src/org/openqa/selenium/interactions/internal/MouseAction.java

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
* Base class for all mouse-related actions.
2626
*/
2727
public class MouseAction extends BaseAction {
28+
public enum Button {
29+
LEFT(0),
30+
MIDDLE(1),
31+
RIGHT(2);
32+
33+
private final int b;
34+
Button(int b) {
35+
this.b = b;
36+
}
37+
}
38+
2839
protected final Mouse mouse;
2940

3041
protected MouseAction(Mouse mouse, Locatable locationProvider) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.internal;
19+
20+
import org.openqa.selenium.interactions.internal.Coordinates;
21+
22+
public interface HasIdentity {
23+
String getId();
24+
}

Diff for: java/client/src/org/openqa/selenium/remote/BUCK

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ java_library(name = 'remote',
4646
'JsonException.java',
4747
'JsonToBeanConverter.java',
4848
'LocalFileDetector.java',
49+
'RemoteActionChainExecutor.java',
4950
'RemoteExecuteMethod.java',
5051
'RemoteKeyboard.java',
5152
'RemoteLogs.java',

Diff for: java/client/src/org/openqa/selenium/remote/RemoteWebElement.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.openqa.selenium.internal.FindsByName;
3535
import org.openqa.selenium.internal.FindsByTagName;
3636
import org.openqa.selenium.internal.FindsByXPath;
37+
import org.openqa.selenium.internal.HasIdentity;
3738
import org.openqa.selenium.internal.Locatable;
3839
import org.openqa.selenium.internal.WrapsDriver;
3940
import org.openqa.selenium.internal.WrapsElement;
@@ -46,7 +47,7 @@
4647

4748
public class RemoteWebElement implements WebElement, FindsByLinkText, FindsById, FindsByName,
4849
FindsByTagName, FindsByClassName, FindsByCssSelector,
49-
FindsByXPath, WrapsDriver, Locatable {
50+
FindsByXPath, WrapsDriver, Locatable, HasIdentity {
5051

5152
private String foundBy;
5253
protected String id;

Diff for: java/client/src/org/openqa/selenium/remote/build.desc

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ java_library(name = "remote",
7777
"HttpCommandExecutor.java",
7878
"HttpVerb.java",
7979
"LocalFileDetector.java",
80+
"RemoteActionChainExecutor.java",
8081
"RemoteExecuteMethod.java",
8182
"RemoteKeyboard.java",
8283
"RemoteLogs.java",

0 commit comments

Comments
 (0)