From 55b91457a61a997132cccb8e45bfed25156a91af Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Sun, 21 Jan 2018 10:07:41 +0100 Subject: [PATCH 1/9] Implement custom command codec --- .../AppiumW3CHttpCommandCodec.java | 87 +++++++++++++++++++ .../remote/AppiumCommandExecutor.java | 55 +++++++++++- 2 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java diff --git a/src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java b/src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java new file mode 100644 index 000000000..b25fdeb22 --- /dev/null +++ b/src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java @@ -0,0 +1,87 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * 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 io.appium.java_client; + +import org.openqa.selenium.remote.http.W3CHttpCommandCodec; + +import java.util.Map; + +import static org.openqa.selenium.remote.DriverCommand.ACCEPT_ALERT; +import static org.openqa.selenium.remote.DriverCommand.ACTIONS; +import static org.openqa.selenium.remote.DriverCommand.CLEAR_ACTIONS_STATE; +import static org.openqa.selenium.remote.DriverCommand.DISMISS_ALERT; +import static org.openqa.selenium.remote.DriverCommand.EXECUTE_ASYNC_SCRIPT; +import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT; +import static org.openqa.selenium.remote.DriverCommand.GET_ACTIVE_ELEMENT; +import static org.openqa.selenium.remote.DriverCommand.GET_ALERT_TEXT; +import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_HANDLE; +import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_POSITION; +import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_SIZE; +import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_LOCATION; +import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_RECT; +import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_SIZE; +import static org.openqa.selenium.remote.DriverCommand.GET_WINDOW_HANDLES; +import static org.openqa.selenium.remote.DriverCommand.MAXIMIZE_CURRENT_WINDOW; +import static org.openqa.selenium.remote.DriverCommand.SEND_KEYS_TO_ACTIVE_ELEMENT; +import static org.openqa.selenium.remote.DriverCommand.SEND_KEYS_TO_ELEMENT; +import static org.openqa.selenium.remote.DriverCommand.SET_ALERT_VALUE; +import static org.openqa.selenium.remote.DriverCommand.SET_CURRENT_WINDOW_POSITION; +import static org.openqa.selenium.remote.DriverCommand.SET_CURRENT_WINDOW_SIZE; +import static org.openqa.selenium.remote.DriverCommand.SET_TIMEOUT; + + +public class AppiumW3CHttpCommandCodec extends W3CHttpCommandCodec { + + public AppiumW3CHttpCommandCodec() { + alias(GET_ELEMENT_LOCATION, GET_ELEMENT_RECT); + alias(GET_ELEMENT_SIZE, GET_ELEMENT_RECT); + + defineCommand(EXECUTE_SCRIPT, post("/session/:sessionId/execute/sync")); + defineCommand(EXECUTE_ASYNC_SCRIPT, post("/session/:sessionId/execute/async")); + + defineCommand(MAXIMIZE_CURRENT_WINDOW, post("/session/:sessionId/window/maximize")); + defineCommand(GET_CURRENT_WINDOW_SIZE, get("/session/:sessionId/window/rect")); + defineCommand(SET_CURRENT_WINDOW_SIZE, post("/session/:sessionId/window/rect")); + alias(GET_CURRENT_WINDOW_POSITION, GET_CURRENT_WINDOW_SIZE); + alias(SET_CURRENT_WINDOW_POSITION, SET_CURRENT_WINDOW_SIZE); + defineCommand(GET_CURRENT_WINDOW_HANDLE, get("/session/:sessionId/window")); + defineCommand(GET_WINDOW_HANDLES, get("/session/:sessionId/window/handles")); + + defineCommand(ACCEPT_ALERT, post("/session/:sessionId/alert/accept")); + defineCommand(DISMISS_ALERT, post("/session/:sessionId/alert/dismiss")); + defineCommand(GET_ALERT_TEXT, get("/session/:sessionId/alert/text")); + defineCommand(SET_ALERT_VALUE, post("/session/:sessionId/alert/text")); + + defineCommand(GET_ACTIVE_ELEMENT, get("/session/:sessionId/element/active")); + + defineCommand(ACTIONS, post("/session/:sessionId/actions")); + defineCommand(CLEAR_ACTIONS_STATE, delete("/session/:sessionId/actions")); + } + + @Override + protected Map amendParameters(String name, Map parameters) { + switch (name) { + case SEND_KEYS_TO_ACTIVE_ELEMENT: + case SEND_KEYS_TO_ELEMENT: + case SET_ALERT_VALUE: + case SET_TIMEOUT: + return super.amendParameters(name, parameters); + default: + return parameters; + } + } +} diff --git a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java index b62e99e01..5c0b3a242 100644 --- a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java +++ b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java @@ -23,17 +23,22 @@ import com.google.common.base.Supplier; import com.google.common.base.Throwables; +import io.appium.java_client.AppiumW3CHttpCommandCodec; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.remote.Command; +import org.openqa.selenium.remote.CommandCodec; import org.openqa.selenium.remote.CommandInfo; import org.openqa.selenium.remote.DriverCommand; import org.openqa.selenium.remote.HttpCommandExecutor; import org.openqa.selenium.remote.Response; import org.openqa.selenium.remote.http.HttpClient; +import org.openqa.selenium.remote.http.HttpRequest; +import org.openqa.selenium.remote.http.W3CHttpCommandCodec; import org.openqa.selenium.remote.internal.ApacheHttpClient; import org.openqa.selenium.remote.service.DriverService; import java.io.IOException; +import java.lang.reflect.Field; import java.net.ConnectException; import java.net.URL; import java.util.Map; @@ -74,7 +79,42 @@ public AppiumCommandExecutor(Map additionalCommands, this(additionalCommands, service, new ApacheHttpClient.Factory()); } - @Override public Response execute(Command command) throws WebDriverException { + private B getPrivateFieldValue(String fieldName, Class fieldType) { + try { + final Field f = getClass().getSuperclass().getDeclaredField(fieldName); + f.setAccessible(true); + return fieldType.cast(f.get(this)); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new WebDriverException(e); + } + } + + private void setPrivateFieldValue(String fieldName, Object newValue) { + try { + final Field f = getClass().getSuperclass().getDeclaredField(fieldName); + f.setAccessible(true); + f.set(this, newValue); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new WebDriverException(e); + } + } + + private Map getAdditionalCommands() { + //noinspection unchecked + return getPrivateFieldValue("additionalCommands", Map.class); + } + + private CommandCodec getCommandCodec() { + //noinspection unchecked + return getPrivateFieldValue("commandCodec", CommandCodec.class); + } + + private void setCommandCodec(CommandCodec newCodec) { + setPrivateFieldValue("commandCodec", newCodec); + } + + @Override + public Response execute(Command command) throws WebDriverException { if (DriverCommand.NEW_SESSION.equals(command.getName())) { serviceOptional.ifPresent(driverService -> { try { @@ -85,8 +125,9 @@ public AppiumCommandExecutor(Map additionalCommands, }); } + Response response; try { - return super.execute(command); + response = super.execute(command); } catch (Throwable t) { Throwable rootCause = Throwables.getRootCause(t); if (rootCause instanceof ConnectException @@ -107,5 +148,15 @@ public AppiumCommandExecutor(Map additionalCommands, serviceOptional.ifPresent(DriverService::stop); } } + + if (DriverCommand.NEW_SESSION.equals(command.getName()) && + getCommandCodec() instanceof W3CHttpCommandCodec) { + setCommandCodec(new AppiumW3CHttpCommandCodec()); + for (Map.Entry entry : getAdditionalCommands().entrySet()) { + defineCommand(entry.getKey(), entry.getValue()); + } + } + + return response; } } \ No newline at end of file From 60343fa54200e67f16063ba6d183f52e8db43f8e Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Sun, 21 Jan 2018 10:18:52 +0100 Subject: [PATCH 2/9] Satisfy style checker --- .../io/appium/java_client/remote/AppiumCommandExecutor.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java index 5c0b3a242..128b8cc99 100644 --- a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java +++ b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java @@ -149,8 +149,8 @@ public Response execute(Command command) throws WebDriverException { } } - if (DriverCommand.NEW_SESSION.equals(command.getName()) && - getCommandCodec() instanceof W3CHttpCommandCodec) { + if (DriverCommand.NEW_SESSION.equals(command.getName()) + && getCommandCodec() instanceof W3CHttpCommandCodec) { setCommandCodec(new AppiumW3CHttpCommandCodec()); for (Map.Entry entry : getAdditionalCommands().entrySet()) { defineCommand(entry.getKey(), entry.getValue()); From 537dee70057d7c3a4f7f42605bcc41bda4e1511b Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 29 Jan 2018 07:59:21 +0100 Subject: [PATCH 3/9] Add docstriing --- .../appium/java_client/AppiumW3CHttpCommandCodec.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java b/src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java index b25fdeb22..f773c894c 100644 --- a/src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java +++ b/src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java @@ -31,9 +31,6 @@ import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_HANDLE; import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_POSITION; import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_SIZE; -import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_LOCATION; -import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_RECT; -import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_SIZE; import static org.openqa.selenium.remote.DriverCommand.GET_WINDOW_HANDLES; import static org.openqa.selenium.remote.DriverCommand.MAXIMIZE_CURRENT_WINDOW; import static org.openqa.selenium.remote.DriverCommand.SEND_KEYS_TO_ACTIVE_ELEMENT; @@ -46,10 +43,12 @@ public class AppiumW3CHttpCommandCodec extends W3CHttpCommandCodec { + /** + * This class overrides the built-in Selenium W3C commands codec, + * since the latter hardcodes many commands in Javascript, + * which does not work with Appium. + */ public AppiumW3CHttpCommandCodec() { - alias(GET_ELEMENT_LOCATION, GET_ELEMENT_RECT); - alias(GET_ELEMENT_SIZE, GET_ELEMENT_RECT); - defineCommand(EXECUTE_SCRIPT, post("/session/:sessionId/execute/sync")); defineCommand(EXECUTE_ASYNC_SCRIPT, post("/session/:sessionId/execute/async")); From 9105ebb4134a34be9f730c93c9125f5faaff43cb Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 29 Jan 2018 08:01:57 +0100 Subject: [PATCH 4/9] Change codec's pcakage --- .../io/appium/java_client/remote/AppiumCommandExecutor.java | 1 - .../java_client/{ => remote}/AppiumW3CHttpCommandCodec.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) rename src/main/java/io/appium/java_client/{ => remote}/AppiumW3CHttpCommandCodec.java (99%) diff --git a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java index 128b8cc99..2a1886918 100644 --- a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java +++ b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java @@ -23,7 +23,6 @@ import com.google.common.base.Supplier; import com.google.common.base.Throwables; -import io.appium.java_client.AppiumW3CHttpCommandCodec; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.remote.Command; import org.openqa.selenium.remote.CommandCodec; diff --git a/src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java b/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java similarity index 99% rename from src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java rename to src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java index f773c894c..8dab7e492 100644 --- a/src/main/java/io/appium/java_client/AppiumW3CHttpCommandCodec.java +++ b/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.appium.java_client; +package io.appium.java_client.remote; import org.openqa.selenium.remote.http.W3CHttpCommandCodec; From 9f6f734ecf645d5fa21beb6146133dfa42b41428 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 29 Jan 2018 09:04:53 +0100 Subject: [PATCH 5/9] Use more effective loop --- .../io/appium/java_client/remote/AppiumCommandExecutor.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java index 2a1886918..f8651dad6 100644 --- a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java +++ b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java @@ -151,9 +151,7 @@ public Response execute(Command command) throws WebDriverException { if (DriverCommand.NEW_SESSION.equals(command.getName()) && getCommandCodec() instanceof W3CHttpCommandCodec) { setCommandCodec(new AppiumW3CHttpCommandCodec()); - for (Map.Entry entry : getAdditionalCommands().entrySet()) { - defineCommand(entry.getKey(), entry.getValue()); - } + getAdditionalCommands().forEach(this::defineCommand); } return response; From ad2332c09f8a4f8af54826599660a6c54c47235e Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 29 Jan 2018 09:28:05 +0100 Subject: [PATCH 6/9] Add endpoints for getting source and attributes --- .../java_client/remote/AppiumW3CHttpCommandCodec.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java b/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java index 8dab7e492..ec952ac9f 100644 --- a/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java +++ b/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java @@ -31,6 +31,8 @@ import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_HANDLE; import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_POSITION; import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_SIZE; +import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_ATTRIBUTE; +import static org.openqa.selenium.remote.DriverCommand.GET_PAGE_SOURCE; import static org.openqa.selenium.remote.DriverCommand.GET_WINDOW_HANDLES; import static org.openqa.selenium.remote.DriverCommand.MAXIMIZE_CURRENT_WINDOW; import static org.openqa.selenium.remote.DriverCommand.SEND_KEYS_TO_ACTIVE_ELEMENT; @@ -47,8 +49,13 @@ public class AppiumW3CHttpCommandCodec extends W3CHttpCommandCodec { * This class overrides the built-in Selenium W3C commands codec, * since the latter hardcodes many commands in Javascript, * which does not work with Appium. + * Check https://www.w3.org/TR/webdriver/ to see all available W3C + * endpoints. */ public AppiumW3CHttpCommandCodec() { + defineCommand(GET_ELEMENT_ATTRIBUTE, get("/session/:sessionId/element/:id/attribute/:name")); + defineCommand(GET_PAGE_SOURCE, get("/session/:sessionId/source")); + defineCommand(EXECUTE_SCRIPT, post("/session/:sessionId/execute/sync")); defineCommand(EXECUTE_ASYNC_SCRIPT, post("/session/:sessionId/execute/async")); From 339fd9d012e2e5d471082fdc91e176d60e04b209 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 29 Jan 2018 22:59:44 +0100 Subject: [PATCH 7/9] Avoid unnecessary code duplication --- .../remote/AppiumW3CHttpCommandCodec.java | 57 +++++++------------ 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java b/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java index ec952ac9f..d88f59ac9 100644 --- a/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java +++ b/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java @@ -20,27 +20,17 @@ import java.util.Map; -import static org.openqa.selenium.remote.DriverCommand.ACCEPT_ALERT; -import static org.openqa.selenium.remote.DriverCommand.ACTIONS; -import static org.openqa.selenium.remote.DriverCommand.CLEAR_ACTIONS_STATE; -import static org.openqa.selenium.remote.DriverCommand.DISMISS_ALERT; -import static org.openqa.selenium.remote.DriverCommand.EXECUTE_ASYNC_SCRIPT; -import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT; -import static org.openqa.selenium.remote.DriverCommand.GET_ACTIVE_ELEMENT; -import static org.openqa.selenium.remote.DriverCommand.GET_ALERT_TEXT; -import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_HANDLE; -import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_POSITION; -import static org.openqa.selenium.remote.DriverCommand.GET_CURRENT_WINDOW_SIZE; import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_ATTRIBUTE; +import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_LOCATION; +import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW; +import static org.openqa.selenium.remote.DriverCommand.GET_ELEMENT_SIZE; import static org.openqa.selenium.remote.DriverCommand.GET_PAGE_SOURCE; -import static org.openqa.selenium.remote.DriverCommand.GET_WINDOW_HANDLES; -import static org.openqa.selenium.remote.DriverCommand.MAXIMIZE_CURRENT_WINDOW; +import static org.openqa.selenium.remote.DriverCommand.IS_ELEMENT_DISPLAYED; import static org.openqa.selenium.remote.DriverCommand.SEND_KEYS_TO_ACTIVE_ELEMENT; import static org.openqa.selenium.remote.DriverCommand.SEND_KEYS_TO_ELEMENT; import static org.openqa.selenium.remote.DriverCommand.SET_ALERT_VALUE; -import static org.openqa.selenium.remote.DriverCommand.SET_CURRENT_WINDOW_POSITION; -import static org.openqa.selenium.remote.DriverCommand.SET_CURRENT_WINDOW_SIZE; import static org.openqa.selenium.remote.DriverCommand.SET_TIMEOUT; +import static org.openqa.selenium.remote.DriverCommand.SUBMIT_ELEMENT; public class AppiumW3CHttpCommandCodec extends W3CHttpCommandCodec { @@ -55,31 +45,28 @@ public class AppiumW3CHttpCommandCodec extends W3CHttpCommandCodec { public AppiumW3CHttpCommandCodec() { defineCommand(GET_ELEMENT_ATTRIBUTE, get("/session/:sessionId/element/:id/attribute/:name")); defineCommand(GET_PAGE_SOURCE, get("/session/:sessionId/source")); + } - defineCommand(EXECUTE_SCRIPT, post("/session/:sessionId/execute/sync")); - defineCommand(EXECUTE_ASYNC_SCRIPT, post("/session/:sessionId/execute/async")); - - defineCommand(MAXIMIZE_CURRENT_WINDOW, post("/session/:sessionId/window/maximize")); - defineCommand(GET_CURRENT_WINDOW_SIZE, get("/session/:sessionId/window/rect")); - defineCommand(SET_CURRENT_WINDOW_SIZE, post("/session/:sessionId/window/rect")); - alias(GET_CURRENT_WINDOW_POSITION, GET_CURRENT_WINDOW_SIZE); - alias(SET_CURRENT_WINDOW_POSITION, SET_CURRENT_WINDOW_SIZE); - defineCommand(GET_CURRENT_WINDOW_HANDLE, get("/session/:sessionId/window")); - defineCommand(GET_WINDOW_HANDLES, get("/session/:sessionId/window/handles")); - - defineCommand(ACCEPT_ALERT, post("/session/:sessionId/alert/accept")); - defineCommand(DISMISS_ALERT, post("/session/:sessionId/alert/dismiss")); - defineCommand(GET_ALERT_TEXT, get("/session/:sessionId/alert/text")); - defineCommand(SET_ALERT_VALUE, post("/session/:sessionId/alert/text")); - - defineCommand(GET_ACTIVE_ELEMENT, get("/session/:sessionId/element/active")); - - defineCommand(ACTIONS, post("/session/:sessionId/actions")); - defineCommand(CLEAR_ACTIONS_STATE, delete("/session/:sessionId/actions")); + @Override + public void alias(String commandName, String isAnAliasFor) { + // This blocks parent constructor from undesirable aliases assigning + switch (commandName) { + case GET_ELEMENT_ATTRIBUTE: + case GET_ELEMENT_LOCATION: + case GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW: + case GET_ELEMENT_SIZE: + case IS_ELEMENT_DISPLAYED: + case SUBMIT_ELEMENT: + case GET_PAGE_SOURCE: + break; + default: + super.alias(commandName, isAnAliasFor); + } } @Override protected Map amendParameters(String name, Map parameters) { + // This blocks parent constructor from undesirable parameters amending switch (name) { case SEND_KEYS_TO_ACTIVE_ELEMENT: case SEND_KEYS_TO_ELEMENT: From e64f358ffee7a6a87491b5a45bfb8c8ceea4f9f7 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 30 Jan 2018 07:41:43 +0100 Subject: [PATCH 8/9] Try to fix codacy --- .../appium/java_client/remote/AppiumW3CHttpCommandCodec.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java b/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java index d88f59ac9..61df1ff47 100644 --- a/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java +++ b/src/main/java/io/appium/java_client/remote/AppiumW3CHttpCommandCodec.java @@ -58,9 +58,10 @@ public void alias(String commandName, String isAnAliasFor) { case IS_ELEMENT_DISPLAYED: case SUBMIT_ELEMENT: case GET_PAGE_SOURCE: - break; + return; default: super.alias(commandName, isAnAliasFor); + break; } } From 5da4998c0a35ddba5a12b8bac92dce1712cfb8cf Mon Sep 17 00:00:00 2001 From: Sergey Tikhomirov Date: Sun, 4 Feb 2018 00:23:23 +0300 Subject: [PATCH 9/9] Addition to the #817. JsonToMobileElementConverter: fixed --- .../JsonToMobileElementConverter.java | 43 ++++++------------- 1 file changed, 12 insertions(+), 31 deletions(-) diff --git a/src/main/java/io/appium/java_client/internal/JsonToMobileElementConverter.java b/src/main/java/io/appium/java_client/internal/JsonToMobileElementConverter.java index 180470773..d78b90c7d 100644 --- a/src/main/java/io/appium/java_client/internal/JsonToMobileElementConverter.java +++ b/src/main/java/io/appium/java_client/internal/JsonToMobileElementConverter.java @@ -18,9 +18,6 @@ import static io.appium.java_client.internal.ElementMap.getElementClass; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; - import io.appium.java_client.HasSessionDetails; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.remote.RemoteWebDriver; @@ -28,7 +25,6 @@ import org.openqa.selenium.remote.internal.JsonToWebElementConverter; import java.lang.reflect.Constructor; -import java.util.Collection; /** * Reconstitutes {@link org.openqa.selenium.WebElement}s from their JSON representation. Will recursively convert Lists @@ -54,45 +50,30 @@ public JsonToMobileElementConverter(RemoteWebDriver driver, HasSessionDetails ha this.automation = hasSessionDetails.getAutomationName(); } - /** - * This method converts a command result. - * - * @param result is the result of a command execution. - * @return the result - */ + @Override public Object apply(Object result) { - if (result instanceof Collection) { - Collection results = (Collection) result; - return Lists.newArrayList(Iterables.transform(results, this)); - } - - if (result instanceof RemoteWebElement) { - RemoteWebElement resultElement = RemoteWebElement.class.cast(result); - RemoteWebElement element = newMobileElement(); - element.setParent(driver); - element.setId(resultElement.getId()); - element.setFileDetector(driver.getFileDetector()); - return element; + Object toBeReturned = result; + if (toBeReturned instanceof RemoteWebElement) { + toBeReturned = newRemoteWebElement(); + ((RemoteWebElement) toBeReturned).setId(((RemoteWebElement) result).getId()); } - if (result instanceof Number) { - if (result instanceof Float || result instanceof Double) { - return ((Number) result).doubleValue(); - } - return ((Number) result).longValue(); - } - - return result; + return super.apply(toBeReturned); } - private RemoteWebElement newMobileElement() { + @Override + protected RemoteWebElement newRemoteWebElement() { Class target; target = getElementClass(platform, automation); + try { Constructor constructor = target.getDeclaredConstructor(); constructor.setAccessible(true); RemoteWebElement result = constructor.newInstance(); + result.setParent(driver); + result.setFileDetector(driver.getFileDetector()); + return result; } catch (Exception e) { throw new WebDriverException(e);