Skip to content

Commit

Permalink
[java] Refactoring code that builds command for RemoteWebDriver to co…
Browse files Browse the repository at this point in the history
…llect all magic string constants (command names and parameter names) into a single class DriverCommands
  • Loading branch information
barancev committed Dec 1, 2018
1 parent 3ee5a8e commit 4776510
Show file tree
Hide file tree
Showing 7 changed files with 246 additions and 79 deletions.
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 @@ -6,6 +6,7 @@ java_library(
srcs = [
'Augmentable.java',
'Command.java',
'CommandPayload.java',
'CommandExecutor.java',
'ErrorCodes.java',
'Response.java',
Expand Down
1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/remote/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("//java:version.bzl", "SE_VERSION")

TYPE_SOURCES = [
"Command.java",
"CommandPayload.java",
"ErrorCodes.java",
"Response.java",
"ScreenshotException.java",
Expand Down
24 changes: 13 additions & 11 deletions java/client/src/org/openqa/selenium/remote/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,36 @@
public class Command {

private final SessionId sessionId;
private final String name;
private final Map<String, ?> parameters;
private final CommandPayload payload;

This comment has been minimized.

Copy link
@kambleaa007

kambleaa007 Dec 3, 2018

Now CommandPayload class will hold both String name and Map<String,?> parameters
Can be used from payload


public Command(SessionId sessionId, String name) {
this(sessionId, name, new HashMap<>());
}

public Command(SessionId sessionId, String name, Map<String, ?> parameters) {
this(sessionId, new CommandPayload(name, parameters));

This comment has been minimized.

Copy link
@kambleaa007

kambleaa007 Dec 3, 2018

memory will get allocated from Old Same constructor

}

public Command(SessionId sessionId, CommandPayload payload) {
this.sessionId = sessionId;
this.name = name;
this.parameters = parameters == null ? new HashMap<>() : parameters;
this.payload = payload;
}

public SessionId getSessionId() {
return sessionId;
}

public String getName() {
return name;
return payload.getName();
}

public Map<String, ?> getParameters() {
return parameters;
return payload.getParameters();
}

@Override
public String toString() {
return "[" + sessionId + ", " + name + " " + parameters + "]";
return "[" + sessionId + ", " + getName() + " " + getParameters() + "]";
}

@Override
Expand All @@ -61,13 +63,13 @@ public boolean equals(Object o) {
}

Command that = (Command) o;
return Objects.equals(sessionId, that.sessionId) &&
Objects.equals(name, that.name) &&
Objects.equals(parameters, that.parameters);
return Objects.equals(this.sessionId, that.sessionId) &&
Objects.equals(this.getName(), that.getName()) &&
Objects.equals(this.getParameters(), that.getParameters());
}

@Override
public int hashCode() {
return Objects.hash(sessionId, name, parameters);
return Objects.hash(sessionId, getName(), getParameters());
}
}
40 changes: 40 additions & 0 deletions java/client/src/org/openqa/selenium/remote/CommandPayload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.remote;

import java.util.Map;

public class CommandPayload {

private final String name;
private final Map<String, ?> parameters;

public CommandPayload(String name, Map<String, ?> parameters) {

this.name = name;
this.parameters = parameters;
}

public String getName() {
return name;
}

public Map<String, ?> getParameters() {
return parameters;
}
}
136 changes: 136 additions & 0 deletions java/client/src/org/openqa/selenium/remote/DriverCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@

package org.openqa.selenium.remote;

import com.google.common.collect.ImmutableMap;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.interactions.Sequence;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* An empty interface defining constants for the standard commands defined in the WebDriver JSON
* wire protocol.
Expand All @@ -27,34 +41,75 @@ public interface DriverCommand {
String GET_ALL_SESSIONS = "getAllSessions";
String GET_CAPABILITIES = "getCapabilities";
String NEW_SESSION = "newSession";
static CommandPayload NEW_SESSION(Capabilities capabilities) {
return new CommandPayload(NEW_SESSION, ImmutableMap.of("desiredCapabilities", capabilities));
}

String STATUS = "status";

String CLOSE = "close";
String QUIT = "quit";

String GET = "get";
static CommandPayload GET(String url) {
return new CommandPayload(GET, ImmutableMap.of("url", url));
}
String GO_BACK = "goBack";
String GO_FORWARD = "goForward";
String REFRESH = "refresh";

String ADD_COOKIE = "addCookie";
static CommandPayload ADD_COOKIE(Cookie cookie) {
return new CommandPayload(ADD_COOKIE, ImmutableMap.of("cookie", cookie));
}
String GET_ALL_COOKIES = "getCookies";
String GET_COOKIE = "getCookie";
String DELETE_COOKIE = "deleteCookie";
static CommandPayload DELETE_COOKIE(String name) {
return new CommandPayload(DELETE_COOKIE, ImmutableMap.of("name", name));
}
String DELETE_ALL_COOKIES = "deleteAllCookies";

String FIND_ELEMENT = "findElement";
static CommandPayload FIND_ELEMENT(String strategy, String value) {
return new CommandPayload(FIND_ELEMENT, ImmutableMap.of("using", strategy, "value", value));
}
String FIND_ELEMENTS = "findElements";
static CommandPayload FIND_ELEMENTS(String strategy, String value) {
return new CommandPayload(FIND_ELEMENTS, ImmutableMap.of("using", strategy, "value", value));
}
String FIND_CHILD_ELEMENT = "findChildElement";
static CommandPayload FIND_CHILD_ELEMENT(String id, String strategy, String value) {
return new CommandPayload(FIND_CHILD_ELEMENT,
ImmutableMap.of("id", id, "using", strategy, "value", value));
}
String FIND_CHILD_ELEMENTS = "findChildElements";
static CommandPayload FIND_CHILD_ELEMENTS(String id, String strategy, String value) {
return new CommandPayload(FIND_CHILD_ELEMENTS,
ImmutableMap.of("id", id, "using", strategy, "value", value));
}

String CLEAR_ELEMENT = "clearElement";
static CommandPayload CLEAR_ELEMENT(String id) {
return new CommandPayload(CLEAR_ELEMENT, ImmutableMap.of("id", id));
}
String CLICK_ELEMENT = "clickElement";
static CommandPayload CLICK_ELEMENT(String id) {
return new CommandPayload(CLICK_ELEMENT, ImmutableMap.of("id", id));
}
String SEND_KEYS_TO_ELEMENT = "sendKeysToElement";
static CommandPayload SEND_KEYS_TO_ELEMENT(String id, CharSequence[] keysToSend) {
return new CommandPayload(SEND_KEYS_TO_ELEMENT, ImmutableMap.of("id", id, "value", keysToSend));
}
String SEND_KEYS_TO_ACTIVE_ELEMENT = "sendKeysToActiveElement";
String SUBMIT_ELEMENT = "submitElement";
static CommandPayload SUBMIT_ELEMENT(String id) {
return new CommandPayload(SUBMIT_ELEMENT, ImmutableMap.of("id", id));
}
String UPLOAD_FILE = "uploadFile";
static CommandPayload UPLOAD_FILE(String file) {
return new CommandPayload(UPLOAD_FILE, ImmutableMap.of("file", file));
}

String GET_CURRENT_WINDOW_HANDLE = "getCurrentWindowHandle";
String GET_WINDOW_HANDLES = "getWindowHandles";
Expand All @@ -63,8 +118,14 @@ public interface DriverCommand {
String GET_CONTEXT_HANDLES = "getContextHandles";

String SWITCH_TO_WINDOW = "switchToWindow";
static CommandPayload SWITCH_TO_WINDOW(String windowHandleOrName) {
return new CommandPayload(SWITCH_TO_WINDOW, ImmutableMap.of("handle", windowHandleOrName));
}
String SWITCH_TO_CONTEXT = "switchToContext";
String SWITCH_TO_FRAME = "switchToFrame";
static CommandPayload SWITCH_TO_FRAME(Object frame) {
return new CommandPayload(SWITCH_TO_FRAME, Collections.singletonMap("id", frame));
}
String SWITCH_TO_PARENT_FRAME = "switchToParentFrame";
String GET_ACTIVE_ELEMENT = "getActiveElement";

Expand All @@ -73,32 +134,89 @@ public interface DriverCommand {
String GET_TITLE = "getTitle";

String EXECUTE_SCRIPT = "executeScript";
static CommandPayload EXECUTE_SCRIPT(String script, List<Object> args) {
return new CommandPayload(EXECUTE_SCRIPT, ImmutableMap.of("script", script, "args", args));
}
String EXECUTE_ASYNC_SCRIPT = "executeAsyncScript";
static CommandPayload EXECUTE_ASYNC_SCRIPT(String script, List<Object> args) {
return new CommandPayload(EXECUTE_ASYNC_SCRIPT, ImmutableMap.of("script", script, "args", args));
}

String GET_ELEMENT_TEXT = "getElementText";
static CommandPayload GET_ELEMENT_TEXT(String id) {
return new CommandPayload(GET_ELEMENT_TEXT, ImmutableMap.of("id", id));
}
String GET_ELEMENT_TAG_NAME = "getElementTagName";
static CommandPayload GET_ELEMENT_TAG_NAME(String id) {
return new CommandPayload(GET_ELEMENT_TAG_NAME, ImmutableMap.of("id", id));
}
String IS_ELEMENT_SELECTED = "isElementSelected";
static CommandPayload IS_ELEMENT_SELECTED(String id) {
return new CommandPayload(IS_ELEMENT_SELECTED, ImmutableMap.of("id", id));
}
String IS_ELEMENT_ENABLED = "isElementEnabled";
static CommandPayload IS_ELEMENT_ENABLED(String id) {
return new CommandPayload(IS_ELEMENT_ENABLED, ImmutableMap.of("id", id));
}
String IS_ELEMENT_DISPLAYED = "isElementDisplayed";
static CommandPayload IS_ELEMENT_DISPLAYED(String id) {
return new CommandPayload(IS_ELEMENT_DISPLAYED, ImmutableMap.of("id", id));
}
String GET_ELEMENT_RECT = "getElementRect";
static CommandPayload GET_ELEMENT_RECT(String id) {
return new CommandPayload(GET_ELEMENT_RECT, ImmutableMap.of("id", id));
}
String GET_ELEMENT_LOCATION = "getElementLocation";
static CommandPayload GET_ELEMENT_LOCATION(String id) {
return new CommandPayload(GET_ELEMENT_LOCATION, ImmutableMap.of("id", id));
}
String GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW = "getElementLocationOnceScrolledIntoView";
static CommandPayload GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW(String id) {
return new CommandPayload(GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW, ImmutableMap.of("id", id));
}
String GET_ELEMENT_SIZE = "getElementSize";
static CommandPayload GET_ELEMENT_SIZE(String id) {
return new CommandPayload(GET_ELEMENT_SIZE, ImmutableMap.of("id", id));
}
String GET_ELEMENT_ATTRIBUTE = "getElementAttribute";
static CommandPayload GET_ELEMENT_ATTRIBUTE(String id, String name) {
return new CommandPayload(GET_ELEMENT_ATTRIBUTE, ImmutableMap.of("id", id, "name", name));
}
String GET_ELEMENT_PROPERTY = "getElementProperty";
String GET_ELEMENT_VALUE_OF_CSS_PROPERTY = "getElementValueOfCssProperty";
static CommandPayload GET_ELEMENT_VALUE_OF_CSS_PROPERTY(String id, String name) {
return new CommandPayload(GET_ELEMENT_VALUE_OF_CSS_PROPERTY, ImmutableMap.of("id", id, "propertyName", name));
}
String ELEMENT_EQUALS = "elementEquals";

String SCREENSHOT = "screenshot";
String ELEMENT_SCREENSHOT = "elementScreenshot";
static CommandPayload ELEMENT_SCREENSHOT(String id) {
return new CommandPayload(ELEMENT_SCREENSHOT, ImmutableMap.of("id", id));
}

String ACCEPT_ALERT = "acceptAlert";
String DISMISS_ALERT = "dismissAlert";
String GET_ALERT_TEXT = "getAlertText";
String SET_ALERT_VALUE = "setAlertValue";
static CommandPayload SET_ALERT_VALUE(String keysToSend) {
return new CommandPayload(SET_ALERT_VALUE, ImmutableMap.of("text", keysToSend));
}
String SET_ALERT_CREDENTIALS = "setAlertCredentials";

String SET_TIMEOUT = "setTimeout";
static CommandPayload SET_IMPLICIT_WAIT_TIMEOUT(long time, TimeUnit unit) {
return new CommandPayload(
SET_TIMEOUT, ImmutableMap.of("implicit", TimeUnit.MILLISECONDS.convert(time, unit)));
}
static CommandPayload SET_SCRIPT_TIMEOUT(long time, TimeUnit unit) {
return new CommandPayload(
SET_TIMEOUT, ImmutableMap.of("script", TimeUnit.MILLISECONDS.convert(time, unit)));
}
static CommandPayload SET_PAGE_LOAD_TIMEOUT(long time, TimeUnit unit) {
return new CommandPayload(
SET_TIMEOUT, ImmutableMap.of("pageLoad", TimeUnit.MILLISECONDS.convert(time, unit)));
}
String IMPLICITLY_WAIT = "implicitlyWait";
String SET_SCRIPT_TIMEOUT = "setScriptTimeout";

Expand Down Expand Up @@ -131,6 +249,9 @@ public interface DriverCommand {

// W3C Actions APIs
String ACTIONS = "actions";
static CommandPayload ACTIONS(Collection<Sequence> actions) {
return new CommandPayload(ACTIONS, ImmutableMap.of("actions", actions));
}
String CLEAR_ACTIONS_STATE = "clearActionState";

// These belong to the Advanced user interactions - an element is
Expand All @@ -148,6 +269,9 @@ public interface DriverCommand {
String IME_IS_ACTIVATED = "imeIsActivated";
String IME_DEACTIVATE = "imeDeactivate";
String IME_ACTIVATE_ENGINE = "imeActivateEngine";
static CommandPayload IME_ACTIVATE_ENGINE(String engine) {
return new CommandPayload(SET_ALERT_VALUE, ImmutableMap.of("engine", engine));
}

// These belong to the Advanced Touch API
String TOUCH_SINGLE_TAP = "touchSingleTap";
Expand All @@ -161,10 +285,22 @@ public interface DriverCommand {

// Window API
String SET_CURRENT_WINDOW_POSITION = "setWindowPosition";
static CommandPayload SET_CURRENT_WINDOW_POSITION(Point targetPosition) {
return new CommandPayload(
SET_CURRENT_WINDOW_POSITION, ImmutableMap.of("x", targetPosition.x, "y", targetPosition.y));
}
String GET_CURRENT_WINDOW_POSITION = "getWindowPosition";
static CommandPayload GET_CURRENT_WINDOW_POSITION() {
return new CommandPayload(
GET_CURRENT_WINDOW_POSITION, ImmutableMap.of("windowHandle", "current"));
}

// W3C compatible Window API
String SET_CURRENT_WINDOW_SIZE = "setCurrentWindowSize";
static CommandPayload SET_CURRENT_WINDOW_SIZE(Dimension targetSize) {
return new CommandPayload(
SET_CURRENT_WINDOW_SIZE, ImmutableMap.of("width", targetSize.width, "height", targetSize.height));
}
String GET_CURRENT_WINDOW_SIZE = "getCurrentWindowSize";
String MAXIMIZE_CURRENT_WINDOW = "maximizeCurrentWindow";
String FULLSCREEN_CURRENT_WINDOW = "fullscreenCurrentWindow";
Expand Down

1 comment on commit 4776510

@kambleaa007
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonderful example of object oriented concepts

Please sign in to comment.