Skip to content

Commit

Permalink
java: Implementing action chains in marionette-style (that does not c…
Browse files Browse the repository at this point in the history
…onform to the standard yet)
  • Loading branch information
barancev committed May 13, 2015
1 parent da83f63 commit b4692ea
Show file tree
Hide file tree
Showing 20 changed files with 141 additions and 34 deletions.
3 changes: 2 additions & 1 deletion java/client/src/org/openqa/selenium/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ java_library(name = 'core',
'UnexpectedAlertBehaviour.java',
'WebDriver.java',
'WebElement.java',
'internal/HasIdentity.java',
'internal/Killable.java',
'internal/Locatable.java',
'internal/Lock.java',
Expand Down Expand Up @@ -135,4 +136,4 @@ java_binary(name = 'selenium-java',
'//java/client/src/org/openqa/selenium/safari:safari',
'//java/client/src/org/openqa/selenium/support:support',
],
)
)
1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/build.desc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ java_library(name = "webdriver-api",
"internal/FindsByName.java",
"internal/FindsByTagName.java",
"internal/FindsByXPath.java",
"internal/HasIdentity.java",
"internal/Killable.java",
"internal/Locatable.java",
"internal/Lock.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public class MarionetteConnection implements ExtensionConnection, NeedsLocalLogs
.put(DriverCommand.GET_ELEMENT_LOCATION, "getElementPosition")
.put(DriverCommand.GET_ALL_COOKIES, "getAllCookies")
.put(DriverCommand.QUIT, "deleteSession")
.put(DriverCommand.MOVE_TO, "move")
.put(DriverCommand.MOUSE_DOWN, "press")
.put(DriverCommand.MOUSE_UP, "release")
.put(DriverCommand.CLICK, "click")
.build();

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

} else {
response = new JsonToBeanConverter().convert(Response.class, rawResponse);
if (map.containsKey("error")) {
// ***************************************************************
// Marionette Compliance Issue: Error responses should, at a
// minimum, put the status property at the root of the object.
// In other words:
// {
// status: 7,
// value:
// {
// message: "Did not find element with id=foo",
// stackTrace: <stack trace goes here>
// }
// }
// ***************************************************************
response = new Response();
Object value = map.get("error");
if (value != null) {
if (value instanceof Map) {
Map<String, Object> errorMap = (Map<String, Object>) value;
if (errorMap != null) {
response.setStatus(Integer.parseInt(errorMap.get("status").toString()));
errorMap.remove("status");
response.setValue(errorMap);
}
} else {
response.setStatus(ErrorCodes.UNHANDLED_ERROR);
response.setValue(value + ": " + map.get("message"));
}
}
response.setValue(map.get("error"));

} else {
response = new JsonToBeanConverter().convert(Response.class, rawResponse);

// ***************************************************************
// Marionette Compliance Issue: Responses from findElements
// are returned with raw element IDs as the value.
Expand Down Expand Up @@ -280,7 +256,8 @@ private String serializeCommand(Command command) {
|| DriverCommand.MOUSE_DOWN.equals(commandName)
|| DriverCommand.MOUSE_UP.equals(commandName)
|| DriverCommand.MOVE_TO.equals(commandName)) {
String actionName = commandName;
String actionName = seleniumToMarionetteCommandMap.containsKey(commandName) ?
seleniumToMarionetteCommandMap.get(commandName) : commandName;
commandName = "actionChain";
List<Object> action = Lists.newArrayList();
action.add(actionName);
Expand Down
2 changes: 1 addition & 1 deletion java/client/src/org/openqa/selenium/interactions/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ java_library(name = 'interactions',
':core',
':exceptions',
'//java/client/src/org/openqa/selenium:core',
'//java/client/src/org/openqa/remote:remote',
],
deps = [
'//third_party/java/guava-libraries',
Expand Down Expand Up @@ -40,6 +41,5 @@ java_library(name = 'exceptions',
],
visibility = [
'//java/client/src/org/openqa/selenium:webdriver-api',
'//java/client/src/org/openqa/selenium/remote:remote',
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.Arrays;
import java.util.List;

/**
* Releases the left mouse button
*
Expand All @@ -40,4 +43,8 @@ public void perform() {
moveToLocation();
mouse.mouseUp(getActionLocation());
}

public List<Object> asList() {
return Arrays.<Object>asList("release");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.Arrays;
import java.util.List;

/**
* clicks an element.
*
Expand All @@ -33,4 +36,8 @@ public void perform() {
moveToLocation();
mouse.click(getActionLocation());
}

public List<Object> asList() {
return Arrays.<Object>asList("click", getTargetId(), Button.LEFT, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.Arrays;
import java.util.List;

/**
* Presses the left mouse button without releasing it.
*
Expand All @@ -38,4 +41,8 @@ public void perform() {
moveToLocation();
mouse.mouseDown(getActionLocation());
}

public List<Object> asList() {
return Arrays.<Object>asList("press", getTargetId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.Arrays;
import java.util.List;

/**
* Context-clicks an element
*
Expand All @@ -38,4 +41,7 @@ public void perform() {
mouse.contextClick(getActionLocation());
}

public List<Object> asList() {
return Arrays.<Object>asList("click", getTargetId(), Button.RIGHT, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.Arrays;
import java.util.List;

/**
* Double-clicks an element.
*
Expand All @@ -36,4 +39,8 @@ public void perform() {
moveToLocation();
mouse.doubleClick(getActionLocation());
}

public List<Object> asList() {
return Arrays.<Object>asList("click", getTargetId(), Button.LEFT, 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.openqa.selenium.interactions.internal.SingleKeyAction;
import org.openqa.selenium.internal.Locatable;

import java.util.Arrays;
import java.util.List;

/**
* Emulates key press only, without the release.
*
Expand All @@ -39,4 +42,8 @@ public void perform() {

keyboard.pressKey(key);
}

public List<Object> asList() {
return Arrays.<Object>asList("keyDown", key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.openqa.selenium.interactions.internal.SingleKeyAction;
import org.openqa.selenium.internal.Locatable;

import java.util.Arrays;
import java.util.List;

/**
* Emulates key release only, without the press.
*
Expand All @@ -39,4 +42,8 @@ public void perform() {

keyboard.releaseKey(key);
}

public List<Object> asList() {
return Arrays.<Object>asList("keyUp", key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.Arrays;
import java.util.List;

/**
* Moves the mouse to an element.
*
Expand All @@ -35,4 +38,8 @@ public MoveMouseAction(Mouse mouse, Locatable locationProvider) {
public void perform() {
mouse.mouseMove(getActionLocation());
}

public List<Object> asList() {
return Arrays.<Object>asList("move", getTargetId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.openqa.selenium.interactions.internal.MouseAction;
import org.openqa.selenium.internal.Locatable;

import java.util.Arrays;
import java.util.List;

/**
* Move the mouse to a location within the element provided. The coordinates provided specify the
* offset from the top-left corner of the element.
Expand All @@ -37,4 +40,8 @@ public MoveToOffsetAction(Mouse mouse, Locatable locationProvider, int x, int y)
public void perform() {
mouse.mouseMove(getActionLocation(), xOffset, yOffset);
}

public List<Object> asList() {
return Arrays.<Object>asList("move", getTargetId(), xOffset, yOffset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

package org.openqa.selenium.interactions;

import java.util.Arrays;
import java.util.List;

/**
* Takes a pause.
*
Expand All @@ -37,4 +40,8 @@ public void perform() {
} catch (InterruptedException e) {
}
}

public List<Object> asList() {
return Arrays.<Object>asList("wait", pause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package org.openqa.selenium.interactions.internal;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.HasIdentity;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.internal.WrapsElement;

/**
* Base class for all actions.
Expand All @@ -40,4 +43,22 @@ protected BaseAction(Locatable actionLocation) {
protected BaseAction() {
this.where = null;
}

protected String getTargetId() {
if (!(where instanceof WebElement)) {
return null;
}

WebElement target = (WebElement) where;

while (target instanceof WrapsElement) {
target = ((WrapsElement) target).getWrappedElement();
}

if (target instanceof HasIdentity) {
return ((HasIdentity) target).getId();
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
* Base class for all mouse-related actions.
*/
public class MouseAction extends BaseAction {
public enum Button {
LEFT(0),
MIDDLE(1),
RIGHT(2);

private final int b;
Button(int b) {
this.b = b;
}
}

protected final Mouse mouse;

protected MouseAction(Mouse mouse, Locatable locationProvider) {
Expand Down
24 changes: 24 additions & 0 deletions java/client/src/org/openqa/selenium/internal/HasIdentity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you 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.

package org.openqa.selenium.internal;

import org.openqa.selenium.interactions.internal.Coordinates;

public interface HasIdentity {
String getId();
}
1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/remote/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ java_library(name = 'remote',
'JsonException.java',
'JsonToBeanConverter.java',
'LocalFileDetector.java',
'RemoteActionChainExecutor.java',
'RemoteExecuteMethod.java',
'RemoteKeyboard.java',
'RemoteLogs.java',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.openqa.selenium.internal.FindsByName;
import org.openqa.selenium.internal.FindsByTagName;
import org.openqa.selenium.internal.FindsByXPath;
import org.openqa.selenium.internal.HasIdentity;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.internal.WrapsDriver;
import org.openqa.selenium.internal.WrapsElement;
Expand All @@ -46,7 +47,7 @@

public class RemoteWebElement implements WebElement, FindsByLinkText, FindsById, FindsByName,
FindsByTagName, FindsByClassName, FindsByCssSelector,
FindsByXPath, WrapsDriver, Locatable {
FindsByXPath, WrapsDriver, Locatable, HasIdentity {

private String foundBy;
protected String id;
Expand Down
1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/remote/build.desc
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ java_library(name = "remote",
"HttpCommandExecutor.java",
"HttpVerb.java",
"LocalFileDetector.java",
"RemoteActionChainExecutor.java",
"RemoteExecuteMethod.java",
"RemoteKeyboard.java",
"RemoteLogs.java",
Expand Down

0 comments on commit b4692ea

Please sign in to comment.