Skip to content

#147 Fix. The Appium node silent starting #240

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Sep 11, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.4.1</version>
</dependency>
</dependencies>
<packaging>jar</packaging>
<name>java-client</name>
Expand Down Expand Up @@ -65,6 +70,12 @@
<url>https://github.com/jonahss</url>
<id>jonahss</id>
</developer>
<developer>
<email>tichomirovsergey@gmail.com</email>
<name>Sergey Tikhomirov</name>
<url>https://github.com/TikhomirovSergey</url>
<id>TikhomirovSergey</id>
</developer>
</developers>

<distributionManagement>
Expand Down
162 changes: 87 additions & 75 deletions src/main/java/io/appium/java_client/AppiumDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
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.AppiumCommandExecutor;
import io.appium.java_client.remote.MobileCapabilityType;

import java.net.URL;
Expand All @@ -55,6 +57,8 @@

import javax.xml.bind.DatatypeConverter;

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.Dimension;
Expand All @@ -64,14 +68,7 @@
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.*;
import org.openqa.selenium.remote.html5.RemoteLocationContext;
import org.openqa.selenium.remote.http.HttpMethod;

Expand Down Expand Up @@ -197,76 +194,34 @@ protected static ImmutableMap<String, Object> getCommandImmutableMap(
return builder.build();
}

public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) {
private AppiumDriver(CommandExecutor executor, Capabilities capabilities){
super(executor, capabilities);
this.executeMethod = new AppiumExecutionMethod(this);
locationContext = new RemoteLocationContext(executeMethod);
super.setErrorHandler(errorHandler);
}

super(remoteAddress, desiredCapabilities);

this.executeMethod = new AppiumExecutionMethod(this);
this.remoteAddress = remoteAddress;
locationContext = new RemoteLocationContext(executeMethod);

ImmutableMap.Builder<String, CommandInfo> builder = ImmutableMap
.builder();
builder.put(RESET, postC("/session/:sessionId/appium/app/reset"))
.put(GET_STRINGS,
postC("/session/:sessionId/appium/app/strings"))
.put(KEY_EVENT,
postC("/session/:sessionId/appium/device/keyevent"))
.put(CURRENT_ACTIVITY,
getC("/session/:sessionId/appium/device/current_activity"))
.put(SET_VALUE,
postC("/session/:sessionId/appium/element/:id/value"))
.put(PULL_FILE,
postC("/session/:sessionId/appium/device/pull_file"))
.put(PULL_FOLDER,
postC("/session/:sessionId/appium/device/pull_folder"))
.put(HIDE_KEYBOARD,
postC("/session/:sessionId/appium/device/hide_keyboard"))
.put(PUSH_FILE,
postC("/session/:sessionId/appium/device/push_file"))
.put(RUN_APP_IN_BACKGROUND,
postC("/session/:sessionId/appium/app/background"))
.put(PERFORM_TOUCH_ACTION,
postC("/session/:sessionId/touch/perform"))
.put(PERFORM_MULTI_TOUCH,
postC("/session/:sessionId/touch/multi/perform"))
.put(IS_APP_INSTALLED,
postC("/session/:sessionId/appium/device/app_installed"))
.put(INSTALL_APP,
postC("/session/:sessionId/appium/device/install_app"))
.put(REMOVE_APP,
postC("/session/:sessionId/appium/device/remove_app"))
.put(LAUNCH_APP, postC("/session/:sessionId/appium/app/launch"))
.put(CLOSE_APP, postC("/session/:sessionId/appium/app/close"))
.put(END_TEST_COVERAGE,
postC("/session/:sessionId/appium/app/end_test_coverage"))
.put(LOCK, postC("/session/:sessionId/appium/device/lock"))
.put(IS_LOCKED,
postC("/session/:sessionId/appium/device/is_locked"))
.put(SHAKE, postC("/session/:sessionId/appium/device/shake"))
.put(COMPLEX_FIND,
postC("/session/:sessionId/appium/app/complex_find"))
.put(OPEN_NOTIFICATIONS,
postC("/session/:sessionId/appium/device/open_notifications"))
.put(GET_NETWORK_CONNECTION,
getC("/session/:sessionId/network_connection"))
.put(SET_NETWORK_CONNECTION,
postC("/session/:sessionId/network_connection"))
.put(GET_SETTINGS, getC("/session/:sessionId/appium/settings"))
.put(SET_SETTINGS, postC("/session/:sessionId/appium/settings"))
.put(START_ACTIVITY,
postC("/session/:sessionId/appium/device/start_activity"))
.put(TOGGLE_LOCATION_SERVICES, postC("/session/:sessionId/appium/device/toggle_location_services"));

ImmutableMap<String, CommandInfo> mobileCommands = builder.build();

HttpCommandExecutor mobileExecutor = new HttpCommandExecutor(
mobileCommands, remoteAddress);
super.setCommandExecutor(mobileExecutor);

super.setErrorHandler(errorHandler);
public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities) {
this(new AppiumCommandExecutor(
getMobileCommands(), remoteAddress), desiredCapabilities);
this.remoteAddress = remoteAddress;
}

public AppiumDriver(AppiumDriverLocalService service, Capabilities desiredCapabilities) {
this(new AppiumCommandExecutor(
getMobileCommands(), service), desiredCapabilities);
this.remoteAddress = service.getUrl();
}

public AppiumDriver(AppiumServiceBuilder builder, Capabilities desiredCapabilities) {
this(builder.build(), desiredCapabilities);
}

public AppiumDriver(Capabilities desiredCapabilities) {
this(AppiumDriverLocalService.buildDefaultService(), desiredCapabilities);
}


@Override
protected Response execute(String command) {
return super.execute(command, ImmutableMap.<String, Object>of());
Expand Down Expand Up @@ -708,6 +663,63 @@ private static CommandInfo postC(String url) {
return new CommandInfo(url, HttpMethod.POST);
}

private static ImmutableMap<String, CommandInfo> getMobileCommands(){
ImmutableMap.Builder<String, CommandInfo> builder = ImmutableMap
.builder();
builder.put(RESET, postC("/session/:sessionId/appium/app/reset"))
.put(GET_STRINGS,
postC("/session/:sessionId/appium/app/strings"))
.put(KEY_EVENT,
postC("/session/:sessionId/appium/device/keyevent"))
.put(CURRENT_ACTIVITY,
getC("/session/:sessionId/appium/device/current_activity"))
.put(SET_VALUE,
postC("/session/:sessionId/appium/element/:id/value"))
.put(PULL_FILE,
postC("/session/:sessionId/appium/device/pull_file"))
.put(PULL_FOLDER,
postC("/session/:sessionId/appium/device/pull_folder"))
.put(HIDE_KEYBOARD,
postC("/session/:sessionId/appium/device/hide_keyboard"))
.put(PUSH_FILE,
postC("/session/:sessionId/appium/device/push_file"))
.put(RUN_APP_IN_BACKGROUND,
postC("/session/:sessionId/appium/app/background"))
.put(PERFORM_TOUCH_ACTION,
postC("/session/:sessionId/touch/perform"))
.put(PERFORM_MULTI_TOUCH,
postC("/session/:sessionId/touch/multi/perform"))
.put(IS_APP_INSTALLED,
postC("/session/:sessionId/appium/device/app_installed"))
.put(INSTALL_APP,
postC("/session/:sessionId/appium/device/install_app"))
.put(REMOVE_APP,
postC("/session/:sessionId/appium/device/remove_app"))
.put(LAUNCH_APP, postC("/session/:sessionId/appium/app/launch"))
.put(CLOSE_APP, postC("/session/:sessionId/appium/app/close"))
.put(END_TEST_COVERAGE,
postC("/session/:sessionId/appium/app/end_test_coverage"))
.put(LOCK, postC("/session/:sessionId/appium/device/lock"))
.put(IS_LOCKED,
postC("/session/:sessionId/appium/device/is_locked"))
.put(SHAKE, postC("/session/:sessionId/appium/device/shake"))
.put(COMPLEX_FIND,
postC("/session/:sessionId/appium/app/complex_find"))
.put(OPEN_NOTIFICATIONS,
postC("/session/:sessionId/appium/device/open_notifications"))
.put(GET_NETWORK_CONNECTION,
getC("/session/:sessionId/network_connection"))
.put(SET_NETWORK_CONNECTION,
postC("/session/:sessionId/network_connection"))
.put(GET_SETTINGS, getC("/session/:sessionId/appium/settings"))
.put(SET_SETTINGS, postC("/session/:sessionId/appium/settings"))
.put(START_ACTIVITY,
postC("/session/:sessionId/appium/device/start_activity"))
.put(TOGGLE_LOCATION_SERVICES, postC("/session/:sessionId/appium/device/toggle_location_services"));

return builder.build();
}

@SuppressWarnings("unused")
private static CommandInfo deleteC(String url) {
return new CommandInfo(url, HttpMethod.DELETE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CommandExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.Response;

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

Expand All @@ -18,8 +18,8 @@ abstract class DefaultGenericMobileDriver<T extends WebElement> extends RemoteWe
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);
public DefaultGenericMobileDriver(CommandExecutor executor, Capabilities desiredCapabilities){
super(executor, desiredCapabilities);
}

@Override
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/io/appium/java_client/android/AndroidDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.appium.java_client.android.internal.JsonToAndroidElementConverter;
import io.appium.java_client.remote.MobilePlatform;

import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Response;
Expand Down Expand Up @@ -51,6 +53,24 @@ public AndroidDriver(URL remoteAddress, Capabilities desiredCapabilities) {
this.setElementConverter(new JsonToAndroidElementConverter(this));
}

public AndroidDriver(AppiumDriverLocalService service, Capabilities desiredCapabilities) {
super(service, substituteMobilePlatform(desiredCapabilities,
ANDROID_PLATFORM));
this.setElementConverter(new JsonToAndroidElementConverter(this));
}

public AndroidDriver(AppiumServiceBuilder builder, Capabilities desiredCapabilities) {
super(builder, substituteMobilePlatform(desiredCapabilities,
ANDROID_PLATFORM));
this.setElementConverter(new JsonToAndroidElementConverter(this));
}

public AndroidDriver(Capabilities desiredCapabilities) {
super(substituteMobilePlatform(desiredCapabilities,
ANDROID_PLATFORM));
this.setElementConverter(new JsonToAndroidElementConverter(this));
}

/**
* Scroll forward to the element which has a description or name which contains the input text.
* The scrolling is performed on the first scrollView present on the UI
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.appium.java_client.ios.internal.JsonToIOSElementConverter;
import io.appium.java_client.remote.MobilePlatform;

import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;

Expand Down Expand Up @@ -36,15 +38,33 @@ public IOSDriver(URL remoteAddress, Capabilities desiredCapabilities) {
super(remoteAddress, substituteMobilePlatform(desiredCapabilities,
IOS_PLATFORM));
this.setElementConverter(new JsonToIOSElementConverter(this));
}
}

public IOSDriver(AppiumDriverLocalService service, Capabilities desiredCapabilities) {
super(service, substituteMobilePlatform(desiredCapabilities,
IOS_PLATFORM));
this.setElementConverter(new JsonToIOSElementConverter(this));
}

public IOSDriver(AppiumServiceBuilder builder, Capabilities desiredCapabilities) {
super(builder, substituteMobilePlatform(desiredCapabilities,
IOS_PLATFORM));
this.setElementConverter(new JsonToIOSElementConverter(this));
}

public IOSDriver(Capabilities desiredCapabilities) {
super(substituteMobilePlatform(desiredCapabilities,
IOS_PLATFORM));
this.setElementConverter(new JsonToIOSElementConverter(this));
}

/**
* Scroll to the element whose 'text' attribute contains the input text.
* This scrolling happens within the first UIATableView on the UI. Use the method on IOSElement to scroll from a different ScrollView.
* @param text input text contained in text attribute
*/
@SuppressWarnings("unchecked")
@Override
@Override
public RequiredElementType scrollTo(String text) {
return (RequiredElementType) ((ScrollsTo<?>)
findElementByClassName("UIATableView")).scrollTo(text);
Expand All @@ -56,7 +76,7 @@ public RequiredElementType scrollTo(String text) {
* @param text input text to match
*/
@SuppressWarnings("unchecked")
@Override
@Override
public RequiredElementType scrollToExact(String text) {
return (RequiredElementType) ((ScrollsTo<?>)
findElementByClassName("UIATableView")).scrollToExact(text);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.appium.java_client.remote;


import com.google.common.base.Throwables;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.remote.*;
import org.openqa.selenium.remote.http.HttpClient;
import org.openqa.selenium.remote.service.DriverService;

import java.io.IOException;
import java.net.ConnectException;
import java.net.URL;
import java.util.Map;

public class AppiumCommandExecutor extends HttpCommandExecutor{

private final DriverService service;

public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, URL addressOfRemoteServer) {
super(additionalCommands, addressOfRemoteServer);
service = null;
}

public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands, DriverService service) {
super(additionalCommands, service.getUrl());
this.service = service;
}

@Override
public Response execute(Command command) throws IOException, WebDriverException {
if (DriverCommand.NEW_SESSION.equals(command.getName()) && service != null) {
service.start();
}

try {
return super.execute(command);
} catch (Throwable t) {
Throwable rootCause = Throwables.getRootCause(t);
if (rootCause instanceof ConnectException &&
rootCause.getMessage().contains("Connection refused") && service != null){
if (service.isRunning())
throw new WebDriverException("The session is closed!", t);

if (!service.isRunning())
throw new WebDriverException("The appium server has accidentally died!", t);
}
Throwables.propagateIfPossible(t);
throw new WebDriverException(t);
} finally {
if (DriverCommand.QUIT.equals(command.getName()) && service != null) {
service.stop();
}
}
}

}
Loading