Skip to content

Commit

Permalink
Merge pull request #182 from TikhomirovSergey/returning_of_mobileelement
Browse files Browse the repository at this point in the history
Generic AppiumDriver #178 #162 fix
  • Loading branch information
Jonahss committed Jun 19, 2015
2 parents 80e9131 + e2676a0 commit 4881341
Show file tree
Hide file tree
Showing 48 changed files with 641 additions and 212 deletions.
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,19 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerId>eclipse</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-eclipse</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down
154 changes: 97 additions & 57 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
+Copyright 2014 Appium contributors
+Copyright 2014 Software Freedom Conservancy
+Copyright 2014-2015 Appium contributors
+Copyright 2014-2015 Software Freedom Conservancy
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
Expand All @@ -17,31 +17,81 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import static io.appium.java_client.MobileCommand.CLOSE_APP;
import static io.appium.java_client.MobileCommand.COMPLEX_FIND;
import static io.appium.java_client.MobileCommand.CURRENT_ACTIVITY;
import static io.appium.java_client.MobileCommand.END_TEST_COVERAGE;
import static io.appium.java_client.MobileCommand.GET_NETWORK_CONNECTION;
import static io.appium.java_client.MobileCommand.GET_SETTINGS;
import static io.appium.java_client.MobileCommand.GET_STRINGS;
import static io.appium.java_client.MobileCommand.HIDE_KEYBOARD;
import static io.appium.java_client.MobileCommand.INSTALL_APP;
import static io.appium.java_client.MobileCommand.IS_APP_INSTALLED;
import static io.appium.java_client.MobileCommand.IS_LOCKED;
import static io.appium.java_client.MobileCommand.KEY_EVENT;
import static io.appium.java_client.MobileCommand.LAUNCH_APP;
import static io.appium.java_client.MobileCommand.LOCK;
import static io.appium.java_client.MobileCommand.OPEN_NOTIFICATIONS;
import static io.appium.java_client.MobileCommand.PERFORM_MULTI_TOUCH;
import static io.appium.java_client.MobileCommand.PERFORM_TOUCH_ACTION;
import static io.appium.java_client.MobileCommand.PULL_FILE;
import static io.appium.java_client.MobileCommand.PULL_FOLDER;
import static io.appium.java_client.MobileCommand.PUSH_FILE;
import static io.appium.java_client.MobileCommand.REMOVE_APP;
import static io.appium.java_client.MobileCommand.RESET;
import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND;
import static io.appium.java_client.MobileCommand.SET_NETWORK_CONNECTION;
import static io.appium.java_client.MobileCommand.SET_SETTINGS;
import static io.appium.java_client.MobileCommand.SET_VALUE;
import static io.appium.java_client.MobileCommand.SHAKE;
import static io.appium.java_client.MobileCommand.START_ACTIVITY;
import static io.appium.java_client.MobileCommand.TOGGLE_LOCATION_SERVICES;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.*;
import org.openqa.selenium.html5.Location;
import org.openqa.selenium.html5.LocationContext;
import org.openqa.selenium.remote.*;
import org.openqa.selenium.remote.html5.RemoteLocationContext;
import org.openqa.selenium.remote.http.HttpMethod;

import javax.xml.bind.DatatypeConverter;
import java.net.URL;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static io.appium.java_client.MobileCommand.*;
import javax.xml.bind.DatatypeConverter;

public abstract class AppiumDriver extends RemoteWebDriver implements MobileDriver,
ContextAware, Rotatable, FindsByAccessibilityId, LocationContext,
DeviceActionShortcuts, TouchShortcuts, InteractsWithFiles,
InteractsWithApps, ScrollsTo, HasAppStrings {
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.ScreenOrientation;
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.CommandInfo;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.DriverCommand;
import org.openqa.selenium.remote.ErrorHandler;
import org.openqa.selenium.remote.ExecuteMethod;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.RemoteWebElement;
import org.openqa.selenium.remote.Response;
import org.openqa.selenium.remote.html5.RemoteLocationContext;
import org.openqa.selenium.remote.http.HttpMethod;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
* @param <RequiredElementType> means the required type from the list of allowed types below
* that implement {@link WebElement} Instances of the defined type will be
* returned via findElement* and findElements*.
* Warning (!!!). Allowed types:<br/>
* {@link WebElement}<br/>
* {@link TouchableElement}<br/>
* {@link RemoteWebElement}<br/>
* {@link MobileElement} and its subclasses that designed specifically for each target mobile OS (still Android and iOS)
*/
@SuppressWarnings("unchecked")
public abstract class AppiumDriver<RequiredElementType extends WebElement> extends DefaultGenericMobileDriver<RequiredElementType> {

private final static ErrorHandler errorHandler = new ErrorHandler(
new ErrorCodesMobile(), true);
Expand Down Expand Up @@ -71,40 +121,47 @@ protected static Capabilities substituteMobilePlatform(
return dc;
}

public MobileElement findElement(By by){
return (MobileElement) super.findElement(by);
@Override
public List<RequiredElementType> findElements(By by){
return super.findElements(by);
}

@Override
public List<RequiredElementType> findElementsById(String id){
return super.findElementsById(id);
}

public MobileElement findElementById(String using){
return (MobileElement) super.findElementById(using);
public List<RequiredElementType> findElementsByLinkText(String using) {
return super.findElementsByLinkText(using);
}

public MobileElement findElementByClassName(String using){
return (MobileElement) super.findElementByClassName(using);
public List<RequiredElementType> findElementsByPartialLinkText(String using) {
return super.findElementsByPartialLinkText(using);
}

public MobileElement findElementByName(String using){
return (MobileElement) super.findElementByName(using);
public List<RequiredElementType> findElementsByTagName(String using) {
return super.findElementsByTagName(using);
}

public MobileElement findElementByTagName(String using){
return (MobileElement) super.findElementByTagName(using);
public List<RequiredElementType> findElementsByName(String using) {
return super.findElementsByName(using);
}

public MobileElement findElementByCssSelector(String using){
return (MobileElement) super.findElementByCssSelector(using);
}
public List<RequiredElementType> findElementsByClassName(String using) {
return super.findElementsByClassName(using);
}

public MobileElement findElementByLinkText(String using){
return (MobileElement) super.findElementByLinkText(using);
public List<RequiredElementType> findElementsByCssSelector(String using) {
return super.findElementsByCssSelector(using);
}

public MobileElement findElementByPartialLinkText(String using){
return (MobileElement) super.findElementByPartialLinkText(using);
public List<RequiredElementType> findElementsByXPath(String using) {
return super.findElementsByXPath(using);
}

public MobileElement findElementByXPath(String using){
return (MobileElement) super.findElementByXPath(using);
@Override
public List<RequiredElementType> findElementsByAccessibilityId(String using) {
return (List<RequiredElementType>) findElements("accessibility id", using);
}

/**
Expand Down Expand Up @@ -210,15 +267,9 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super.setErrorHandler(errorHandler);
}

@Override
public Response execute(String driverCommand, Map<String, ?> parameters) {

return super.execute(driverCommand, parameters);
}

@Override
protected Response execute(String command) {
return execute(command, ImmutableMap.<String, Object> of());
return super.execute(command, ImmutableMap.<String, Object>of());
}

@Override
Expand Down Expand Up @@ -335,7 +386,7 @@ public TouchAction performTouchAction(TouchAction touchAction) {
* @see PerformsTouchActions#performMultiTouchAction(MultiTouchAction)
*/
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
@SuppressWarnings({ "rawtypes"})
public void performMultiTouchAction(MultiTouchAction multiAction) {
ImmutableMap<String, ImmutableList> parameters = multiAction
.getParameters();
Expand Down Expand Up @@ -562,7 +613,6 @@ public WebDriver context(String name) {
return AppiumDriver.this;
}

@SuppressWarnings("unchecked")
@Override
public Set<String> getContextHandles() {
Response response = execute(DriverCommand.GET_CONTEXT_HANDLES);
Expand Down Expand Up @@ -607,16 +657,6 @@ public ScreenOrientation getOrientation() {
}
}

@Override
public MobileElement findElementByAccessibilityId(String using) {
return (MobileElement) findElement("accessibility id", using);
}

@Override
public List<WebElement> findElementsByAccessibilityId(String using) {
return findElements("accessibility id", using);
}

@Override
public Location location() {
return locationContext.location();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,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
115 changes: 115 additions & 0 deletions src/main/java/io/appium/java_client/DefaultGenericMobileDriver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package io.appium.java_client;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.generic.searchcontext.*;

import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.Response;

import java.net.URL;
import java.util.List;
import java.util.Map;

@SuppressWarnings({ "unchecked", "rawtypes" })
abstract class DefaultGenericMobileDriver<T extends WebElement> extends RemoteWebDriver implements MobileDriver,
GenericSearchContext<T>, GenericFindsById<T>, GenericFindsByXPath<T>, GenericFindsByLinkText<T>, GenericFindsByTagName<T>,
GenericFindsByClassName<T>, GenericFindsByCssSelector<T>, GenericFindsByName<T>{

public DefaultGenericMobileDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, desiredCapabilities);
}

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

@Override
public List findElements(By by){
return super.findElements(by);
}

@Override
public T findElement(By by){
return (T) super.findElement(by);
}

@Override
public List findElementsById(String id){
return super.findElementsById(id);
}

@Override
public T findElementById(String id){
return (T) super.findElementById(id);
}

public T findElementByLinkText(String using) {
return (T) super.findElementByLinkText(using);
}

public List findElementsByLinkText(String using) {
return super.findElementsByLinkText(using);
}

public T findElementByPartialLinkText(String using) {
return (T) super.findElementByPartialLinkText(using);
}

public List findElementsByPartialLinkText(String using) {
return super.findElementsByPartialLinkText(using);
}

public T findElementByTagName(String using) {
return (T) super.findElementByTagName(using);
}

public List findElementsByTagName(String using) {
return super.findElementsByTagName(using);
}

public T findElementByName(String using) {
return (T) super.findElementByName(using);
}

public List findElementsByName(String using) {
return super.findElementsByName(using);
}

public T findElementByClassName(String using) {
return (T) super.findElementByClassName(using);
}

public List findElementsByClassName(String using) {
return super.findElementsByClassName(using);
}

public T findElementByCssSelector(String using) {
return (T) super.findElementByCssSelector(using);
}

public List findElementsByCssSelector(String using) {
return super.findElementsByCssSelector(using);
}

public T findElementByXPath(String using) {
return (T) super.findElementByXPath(using);
}

public List findElementsByXPath(String using) {
return super.findElementsByXPath(using);
}

@Override
public T findElementByAccessibilityId(String using) {
return (T) findElement("accessibility id", using);
}

@Override
public List findElementsByAccessibilityId(String using) {
return (List<T>) findElements("accessibility id", using);
}
}
Loading

0 comments on commit 4881341

Please sign in to comment.