Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 27 additions & 139 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,19 @@

package io.appium.java_client;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.appium.java_client.remote.MobileCapabilityType.PLATFORM_NAME;
import static org.apache.commons.lang3.StringUtils.containsIgnoreCase;
import static org.apache.commons.lang3.StringUtils.isBlank;

import com.google.common.collect.ImmutableMap;

import io.appium.java_client.internal.CapabilityHelpers;
import io.appium.java_client.internal.JsonToMobileElementConverter;
import io.appium.java_client.remote.AppiumCommandExecutor;
import io.appium.java_client.remote.AppiumNewSessionCommandPayload;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.DeviceRotation;
import org.openqa.selenium.ImmutableCapabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.SessionNotCreatedException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.html5.Location;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.ErrorHandler;
import org.openqa.selenium.remote.ExecuteMethod;
Expand All @@ -55,29 +42,26 @@
import java.lang.reflect.Field;
import java.net.URL;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
* Default Appium driver implementation.
*
* @param <T> the required type of class which implement {@link WebElement}.
* Instances of the defined type will be returned via findElement* and findElements*
* Warning (!!!). Allowed types:
* {@link WebElement}, {@link org.openqa.selenium.remote.RemoteWebElement},
* {@link MobileElement} and its subclasses that designed
* specifically for each target mobile OS (still Android and iOS)
*/
@SuppressWarnings("unchecked")
public class AppiumDriver<T extends WebElement>
extends DefaultGenericMobileDriver<T> implements ComparesImages, ExecutesDriverScript, LogsEvents, HasSettings {
public class AppiumDriver extends RemoteWebDriver implements
WebDriver,
ExecutesMethod,
ComparesImages,
ExecutesDriverScript,
LogsEvents,
CanSetElementValue,
HasBrowserCheck,
HasSettings {

private static final ErrorHandler errorHandler = new ErrorHandler(new ErrorCodesMobile(), true);
// frequently used command parameters
private final URL remoteAddress;
private final RemoteLocationContext locationContext;
protected final RemoteLocationContext locationContext;
private final ExecuteMethod executeMethod;

/**
Expand All @@ -94,7 +78,6 @@ public AppiumDriver(HttpCommandExecutor executor, Capabilities capabilities) {
locationContext = new RemoteLocationContext(executeMethod);
super.setErrorHandler(errorHandler);
this.remoteAddress = executor.getAddressOfRemoteServer();
this.setElementConverter(new JsonToMobileElementConverter(this));
}

public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) {
Expand Down Expand Up @@ -147,95 +130,31 @@ public AppiumDriver(Capabilities desiredCapabilities) {
*/
protected static Capabilities updateDefaultPlatformName(Capabilities originalCapabilities,
String defaultName) {
if (originalCapabilities.getCapability(PLATFORM_NAME) == null) {
DesiredCapabilities dc = new DesiredCapabilities(originalCapabilities);
dc.setCapability(PLATFORM_NAME, defaultName);
return dc;
}
return originalCapabilities;
}

@Override
public List<T> findElements(By by) {
return super.findElements(by);
return originalCapabilities.getCapability(PLATFORM_NAME) == null
? originalCapabilities.merge(new ImmutableCapabilities(PLATFORM_NAME, defaultName))
: originalCapabilities;
}

@Override
public ExecuteMethod getExecuteMethod() {
return executeMethod;
}

@Override
public WebDriver context(String name) {
checkNotNull(name, "Must supply a context name");
try {
execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name));
return this;
} catch (WebDriverException e) {
throw new NoSuchContextException(e.getMessage(), e);
}
}

@Override
public Set<String> getContextHandles() {
Response response = execute(DriverCommand.GET_CONTEXT_HANDLES);
Object value = response.getValue();
try {
List<String> returnedValues = (List<String>) value;
return new LinkedHashSet<>(returnedValues);
} catch (ClassCastException ex) {
throw new WebDriverException(
"Returned value cannot be converted to List<String>: " + value, ex);
}
}

@Override
public String getContext() {
String contextName =
String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue());
if ("null".equalsIgnoreCase(contextName)) {
return null;
}
return contextName;
}

/**
* This method is used to get build version status of running Appium server.
*
* @return map containing version details
*/
public Map<String, Object> getStatus() {
//noinspection unchecked
return (Map<String, Object>) execute(DriverCommand.STATUS).getValue();
}

@Override
public DeviceRotation rotation() {
Response response = execute(DriverCommand.GET_SCREEN_ROTATION);
DeviceRotation deviceRotation =
new DeviceRotation((Map<String, Number>) response.getValue());
if (deviceRotation.getX() < 0 || deviceRotation.getY() < 0 || deviceRotation.getZ() < 0) {
throw new WebDriverException("Unexpected orientation returned: " + deviceRotation);
}
return deviceRotation;
}

@Override
public void rotate(DeviceRotation rotation) {
execute(DriverCommand.SET_SCREEN_ROTATION, rotation.parameters());
}


@Override
public void rotate(ScreenOrientation orientation) {
execute(DriverCommand.SET_SCREEN_ORIENTATION,
ImmutableMap.of("orientation", orientation.value().toUpperCase()));
}

/**
* This method is used to add custom appium commands in Appium 2.0.
*
* @param httpMethod the available {@link HttpMethod}.
* @param url The url to URL template as https://www.w3.org/TR/webdriver/#endpoints.
* @param url The url to URL template as https://www.w3.org/TR/webdriver/#endpoints.
* @param methodName The name of custom appium command.
*/
public void addCommand(HttpMethod httpMethod, String url, String methodName) {
Expand All @@ -257,51 +176,10 @@ public void addCommand(HttpMethod httpMethod, String url, String methodName) {
((AppiumCommandExecutor) getCommandExecutor()).refreshAdditionalCommands();
}

@Override
public ScreenOrientation getOrientation() {
Response response = execute(DriverCommand.GET_SCREEN_ORIENTATION);
String orientation = response.getValue().toString().toLowerCase();
if (orientation.equals(ScreenOrientation.LANDSCAPE.value())) {
return ScreenOrientation.LANDSCAPE;
} else if (orientation.equals(ScreenOrientation.PORTRAIT.value())) {
return ScreenOrientation.PORTRAIT;
} else {
throw new WebDriverException("Unexpected orientation returned: " + orientation);
}
}

@Override
public Location location() {
return locationContext.location();
}

@Override
public void setLocation(Location location) {
locationContext.setLocation(location);
}

public URL getRemoteAddress() {
return remoteAddress;
}

@Override
public boolean isBrowser() {
String browserName = CapabilityHelpers.getCapability(getCapabilities(),
CapabilityType.BROWSER_NAME, String.class);
if (!isBlank(browserName)) {
try {
return (boolean) executeScript("return !!window.navigator;");
} catch (WebDriverException ign) {
// ignore
}
}
try {
return !containsIgnoreCase(getContext(), "NATIVE_APP");
} catch (WebDriverException e) {
return false;
}
}

@Override
protected void startSession(Capabilities capabilities) {
Response response = execute(new AppiumNewSessionCommandPayload(capabilities));
Expand Down Expand Up @@ -333,4 +211,14 @@ protected void startSession(Capabilities capabilities) {
}
setSessionId(response.getSessionId());
}

@Override
public Response execute(String driverCommand, Map<String, ?> parameters) {
return super.execute(driverCommand, parameters);
}

@Override
public Response execute(String command) {
return super.execute(command, Collections.emptyMap());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import java.util.Map;

public class AppiumExecutionMethod implements ExecuteMethod {
private final AppiumDriver<?> driver;
private final AppiumDriver driver;

public AppiumExecutionMethod(AppiumDriver<?> driver) {
public AppiumExecutionMethod(AppiumDriver driver) {
this.driver = driver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@
* limitations under the License.
*/

package io.appium.java_client.android;
package io.appium.java_client;

import static io.appium.java_client.android.AndroidMobileCommandHelper.replaceElementValueCommand;
import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.remote.RemoteWebElement;

import io.appium.java_client.CommandExecutionHelper;
import io.appium.java_client.MobileElement;

public class AndroidElement extends MobileElement {
public interface CanSetElementValue extends ExecutesMethod {
/**
* This method replace current text value.
* @param value a new value
* Set a value to an element.
*
* @param webElement Web element instance.
* @param value Value to set.
*/
public void replaceValue(String value) {
CommandExecutionHelper.execute(this, replaceElementValueCommand(this, value));
default void setElementValue(RemoteWebElement webElement, String value) {
this.execute(MobileCommand.SET_VALUE, ImmutableMap.of(
"id", webElement.getId(),
"value", value
));
}
}

This file was deleted.

Loading