Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   +Copyright 2014 Appium contributors
   +Copyright 2014 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.
   +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 java.net.URL;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import static io.appium.java_client.MobileCommand.*;
 
 
   private final static ErrorHandler errorHandler = new ErrorHandler(new ErrorCodesMobile(), true);
   private URL remoteAddress;
   private ComplexFind complexFind;
 
   public AppiumDriver(URL remoteAddressCapabilities desiredCapabilities){
 
     super(remoteAddressdesiredCapabilities);
 
     this. = new AppiumExecutionMethod(this);
     this. = remoteAddress;
      = new ComplexFind(this);
 
     ImmutableMap.Builder<StringCommandInfobuilder = ImmutableMap.builder();
     builder
             .put(postC("/session/:sessionId/appium/app/reset"))
             .put(postC("/session/:sessionId/appium/app/strings"))
             .put(postC("/session/:sessionId/appium/device/keyevent"))
             .put(getC("/session/:sessionId/appium/device/current_activity"))
             .put(postC("/session/:sessionId/appium/element/:id/value"))
             .put(postC("/session/:sessionId/appium/device/pull_file"))
             .put(postC("/session/:sessionId/appium/device/pull_folder"))
             .put(postC("/session/:sessionId/appium/device/hide_keyboard"))
             .put(postC("/session/:sessionId/appium/device/push_file"))
             .put(postC("/session/:sessionId/appium/app/background"))
             .put(postC("/session/:sessionId/touch/perform"))
             .put(postC("/session/:sessionId/touch/multi/perform"))
             .put(postC("/session/:sessionId/appium/device/app_installed"))
             .put(postC("/session/:sessionId/appium/device/install_app"))
             .put(postC("/session/:sessionId/appium/device/remove_app"))
             .put(postC("/session/:sessionId/appium/app/launch"))
             .put(postC("/session/:sessionId/appium/app/close"))
             .put(postC("/session/:sessionId/appium/app/end_test_coverage"))
             .put(postC("/session/:sessionId/appium/device/lock"))
             .put(postC("/session/:sessionId/appium/device/shake"))
             .put(postC("/session/:sessionId/appium/app/complex_find"))
             .put(postC("/session/:sessionId/appium/device/open_notifications"))
             .put(getC("/session/:sessionId/network_connection"))
             .put(postC("/session/:sessionId/network_connection"))
             ;
     ImmutableMap<StringCommandInfomobileCommands = builder.build();
 
     HttpCommandExecutor mobileExecutor = new HttpCommandExecutor(mobileCommandsremoteAddress);
     super.setCommandExecutor(mobileExecutor);
 
   }
 
   @Override
   public Response execute(String driverCommandMap<String, ?> parameters) {
 
     return super.execute(driverCommandparameters);
   }
 
   @Override
   protected Response execute(String command) {
    return execute(command, ImmutableMap.<StringObject>of());
  }
    return ;
  }

  
Reset the currently running app for this session
  public void resetApp() {
  }

  
Get all defined Strings from an Android app for the default language

Returns:
a string of all the localized strings defined in the app
  public String getAppStrings() {
    Response response = execute();
    return response.getValue().toString();
  }

  
Get all defined Strings from an Android app for the specified language

Parameters:
language strings language code
Returns:
a string of all the localized strings defined in the app
  public String getAppStrings(String language) {
    ImmutableMap.Builder builder = ImmutableMap.builder();
    builder.put("language"language);
    ImmutableMap<StringIntegerparameters = builder.build();
    Response response = execute(parameters);
    return response.getValue().toString();
  }

  
Send a key event to the device

Parameters:
key code for the key pressed on the device
  public void sendKeyEvent(int key) {
    sendKeyEvent(keynull);
  }

  
Send a key event along with an Android metastate to an Android device Metastates are things like *shift* to get uppercase characters

Parameters:
key code for the key pressed on the Android device
metastate metastate for the keypress
  public void sendKeyEvent(int keyInteger metastate) {
    ImmutableMap.Builder builder = ImmutableMap.builder();
    builder.put("keycode"key);
    if (metastate != null) { builder.put("metastate"metastate); }
    ImmutableMap<StringIntegerparameters = builder.build();
    execute(parameters);
  }

  
Get the current activity being run on the mobile device
  public String currentActivity() {
    Response response = execute();
    return response.getValue().toString();
  }

  

Parameters:
remotePath On Android and iOS, this is either the path to the file (relative to the root of the app's file system). On iOS only, if path starts with /AppName.app, which will be replaced with the application's .app directory
Returns:
A byte array of Base64 encoded data.
  public byte[] pullFile(String remotePath) {
    Response response = execute(, ImmutableMap.of("path"remotePath));
    String base64String = response.getValue().toString();
    return DatatypeConverter.parseBase64Binary(base64String);
  }

  
Save base64 encoded data as a file on the remote mobile device. This is an Android only method.

Parameters:
remotePath Path to file to write data to on remote device
base64Data Base64 encoded byte array of data to write to remote device
  public void pushFile(String remotePathbyte[] base64Data) {
    ImmutableMap.Builder builder = ImmutableMap.builder();
    builder.put("path"remotePath).put("data"base64Data);
    execute(builder.build());
  }

  
Pull a folder from the simulator/device. Does not work on iOS Real Devices, but works on simulators

Parameters:
remotePath On Android and iOS, this is either the path to the file (relative to the root of the app's file system). On iOS only, if path starts with /AppName.app, which will be replaced with the application's .app directory
Returns:
A byte array of Base64 encoded data, representing a ZIP ARCHIVE of the contents of the requested folder.
  public byte[] pullFolder(String remotePath) {
    Response response = execute(, ImmutableMap.of("path"remotePath));
    String base64String = response.getValue().toString();
    return DatatypeConverter.parseBase64Binary(base64String);
  }

  
Hides the keyboard if it is showing. On iOS, there are multiple strategies for hiding the keyboard. Defaults to the "tapOutside" strategy (taps outside the keyboard). Switch to using hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done") if this doesn't work.
  public void hideKeyboard() {
  }

  
Hides the keyboard if it is showing. Available strategies are PRESS_KEY and TAP_OUTSIDE. One taps outside the keyboard, the other presses a key of your choosing (probably the 'Done' key). Hiding the keyboard often depends on the way an app is implemented, no single strategy always works. These parameters are only for iOS, and ignored by Android.

Parameters:
strategy HideKeyboardStrategy
keyName a String, representing the text displayed on the button of the keyboard you want to press. For example: "Done"
  public void hideKeyboard(String strategyString keyName) {
    ImmutableMap<StringStringparameters = ImmutableMap.of("strategy"strategy);
    if (keyName != null) {
      parameters = parameters.of("key"keyName);
    }
    execute(parameters);
  }

  
Hides the keyboard by pressing the button specified by keyName if it is showing. This is an iOS only command.

Parameters:
keyName The button pressed by the mobile driver to attempt hiding the keyboard
  public void hideKeyboard(String keyName) {
    execute(, ImmutableMap.of("keyName"keyName));
  }

  
Runs the current app as a background app for the number of seconds requested. This is a synchronous method, it returns after the back has been returned to the foreground.

Parameters:
seconds Number of seconds to run App in background
  public void runAppInBackground(int seconds) {
    execute(, ImmutableMap.of("seconds"seconds));
  }

  
Open the notification shade, on Android devices. Android only method.
  public void openNotifications() { execute(); }
  
Performs a chain of touch actions, which together can be considered an entire gesture. See the Webriver 3 spec https://dvcs.w3.org/hg/webdriver/raw-file/default/webdriver-spec.html It's more convenient to call the perform() method of the TouchAction object itself.

Parameters:
touchAction A TouchAction object, which contains a list of individual touch actions to perform
Returns:
the same touchaction object
  public TouchAction performTouchAction(TouchAction touchAction) {
    ImmutableMap<StringImmutableListparameters = touchAction.getParameters();
    touchAction.clearParameters();
    execute(parameters);
    return touchAction;
  }

  
Performs multiple TouchAction gestures at the same time, to simulate multiple fingers/touch inputs. See the Webriver 3 spec https://dvcs.w3.org/hg/webdriver/raw-file/default/webdriver-spec.html It's more convenient to call the perform() method of the MultiTouchAction object.

Parameters:
multiAction the MultiTouchAction object to perform.
  public void performMultiTouchAction(MultiTouchAction multiAction) {
    ImmutableMap<StringImmutableListparameters = multiAction.getParameters();
    execute(parameters);
  }

  
Convenience method for tapping the center of an element on the screen

Parameters:
fingers number of fingers/appendages to tap with
element element to tap
duration how long between pressing down, and lifting fingers/appendages
  public void tap(int fingersWebElement elementint duration) {
    MultiTouchAction multiTouch = new MultiTouchAction(this);
    for (int i = 0; i < fingersi++) {
      multiTouch.add(createTap(elementduration));
    }
    multiTouch.perform();
  }

  
Convenience method for tapping a position on the screen

Parameters:
fingers number of fingers/appendages to tap with
x x coordinate
y y coordinate
duration
  public void tap(int fingersint xint yint duration) {
    MultiTouchAction multiTouch = new MultiTouchAction(this);
    for (int i = 0; i < fingersi++) {
      multiTouch.add(createTap(xyduration));
    }
    multiTouch.perform();
  }

  
Convenience method for swiping across the screen

Parameters:
startx starting x coordinate
starty starting y coordinate
endx ending x coordinate
endy ending y coordinate
duration amount of time in milliseconds for the entire swipe action to take
  public void swipe(int startxint startyint endxint endyint duration) {
    TouchAction touchAction = new TouchAction(this);
    //appium converts press-wait-moveto-release to a swipe action
    touchAction.press(startxstarty).waitAction(duration).moveTo(endxendy).release();
    touchAction.perform();
  }

  
Convenience method for pinching an element on the screen. "pinching" refers to the action of two appendages pressing the screen and sliding towards each other. NOTE: This convenience method places the initial touches around the element, if this would happen to place one of them off the screen, appium with return an outOfBounds error. In this case, revert to using the MultiTouchAction api instead of this method.

Parameters:
el The element to pinch
  public void pinch(WebElement el) {
    MultiTouchAction multiTouch = new MultiTouchAction(this);
    Dimension dimensions = el.getSize();
    Point upperLeft = el.getLocation();
    Point center = new Point(upperLeft.getX() + dimensions.getWidth() / 2, upperLeft.getY() + dimensions.getHeight() / 2);
    TouchAction action0 = new TouchAction(this).press(elcenter.getX(), center.getY() - 100).moveTo(el).release();
    TouchAction action1 = new TouchAction(this).press(elcenter.getX(), center.getY() + 100).moveTo(el).release();
    multiTouch.add(action0).add(action1);
    multiTouch.perform();
  }

  
Convenience method for pinching an element on the screen. "pinching" refers to the action of two appendages pressing the screen and sliding towards each other. NOTE: This convenience method places the initial touches around the element at a distance, if this would happen to place one of them off the screen, appium will return an outOfBounds error. In this case, revert to using the MultiTouchAction api instead of this method.

Parameters:
x x coordinate to terminate the pinch on
y y coordinate to terminate the pinch on
  public void pinch(int xint y) {
    MultiTouchAction multiTouch = new MultiTouchAction(this);
    TouchAction action0 = new TouchAction(this).press(xy-100).moveTo(xy).release();
    TouchAction action1 = new TouchAction(this).press(xy+100).moveTo(xy).release();
    multiTouch.add(action0).add(action1);
    multiTouch.perform();
  }

  
Convenience method for "zooming in" on an element on the screen. "zooming in" refers to the action of two appendages pressing the screen and sliding away from each other. NOTE: This convenience method slides touches away from the element, if this would happen to place one of them off the screen, appium will return an outOfBounds error. In this case, revert to using the MultiTouchAction api instead of this method.

Parameters:
el The element to pinch
  public void zoom(WebElement el) {
    MultiTouchAction multiTouch = new MultiTouchAction(this);
    Dimension dimensions = el.getSize();
    Point upperLeft = el.getLocation();
    Point center = new Point(upperLeft.getX() + dimensions.getWidth() / 2, upperLeft.getY() + dimensions.getHeight() / 2);
    TouchAction action0 = new TouchAction(this).press(el).moveTo(elcenter.getX(), center.getY() - 100).release();
    TouchAction action1 = new TouchAction(this).press(el).moveTo(elcenter.getX(), center.getY() + 100).release();
    multiTouch.add(action0).add(action1);
    multiTouch.perform();
  }

  
Convenience method for "zooming in" on an element on the screen. "zooming in" refers to the action of two appendages pressing the screen and sliding away from each other. NOTE: This convenience method slides touches away from the element, if this would happen to place one of them off the screen, appium will return an outOfBounds error. In this case, revert to using the MultiTouchAction api instead of this method.

Parameters:
x x coordinate to start zoom on
y y coordinate to start zoom on
  public void zoom(int xint y) {
    MultiTouchAction multiTouch = new MultiTouchAction(this);
    TouchAction action0 = new TouchAction(this).press(xy).moveTo(xy-100).release();
    TouchAction action1 = new TouchAction(this).press(xy).moveTo(xy+100).release();
    multiTouch.add(action0).add(action1);
    multiTouch.perform();
  }

   
In iOS apps, named TextFields have the same accessibility Id as their containing TableElement. This is a convenience method for getting the named TextField, rather than its containing element.

Parameters:
name accessiblity id of TextField
Returns:
The textfield with the given accessibility id
  public WebElement getNamedTextField(String name) {
    if (element.getTagName() != "TextField") {
      return element.findElementByAccessibilityId(name);
    }
    return element;
  }

  
Checks if an app is installed on the device

Parameters:
bundleId bundleId of the app
Returns:
True if app is installed, false otherwise
  public boolean isAppInstalled(String bundleId) {
    Response response = execute(, ImmutableMap.of("bundleId"bundleId));
    return Boolean.parseBoolean(response.getValue().toString());
  }

  
Install an app on the mobile device

Parameters:
appPath path to app to install
  public void installApp(String appPath) {
    execute(, ImmutableMap.of("appPath"appPath));
  }

  
Remove the specified app from the device (uninstall)

Parameters:
bundleId the bunble identifier (or app id) of the app to remove
  public void removeApp(String bundleId) {
    execute(, ImmutableMap.of("bundleId"bundleId));
  }

  
Launch the app which was provided in the capabilities at session creation
  public void launchApp() {
  }

  
Close the app which was provided in the capabilities at session creation
  public void closeApp() {
  }

  
Get test-coverage data Android-only method

Parameters:
intent intent to broadcast
path path to .ec file
  public void endTestCoverage(String intentString path) {
    ImmutableMap.Builder builder = ImmutableMap.builder();
    builder.put("intent"intent).put("path"path);
    execute(builder.build());
  }

  
Lock the device (bring it to the lock screen) for a given number of seconds

Parameters:
seconds number of seconds to lock the screen for
  public void lockScreen(int seconds) {
    execute(, ImmutableMap.of("seconds"seconds));
  }

  
Simulate shaking the device This is an iOS-only method
  public void shake() {
    execute();
  }
  public MobileElement complexFind(String complex) {
    return .execute(complex);
  }
  public MobileElement scrollTo(String text) {
    return .scrollTo(text);
  }
  public MobileElement scrollToExact(String text) {
    return .scrollToExact(text);
  }

  
Get the current network settings of the device. This is an Android-only method

Returns:
NetworkConnectionSetting objects will let you inspect the status of AirplaneMode, Wifi, Data connections
    return new NetworkConnectionSetting(Integer.parseInt(response.getValue().toString()));
  }

  
Set the network connection of the device. This is an Android-only method

Parameters:
connection The NetworkConnectionSetting configuration to use for the device
  public void setNetworkConnection(NetworkConnectionSetting connection) {
    // the new version of the webdriver protocol is going forward with sending JSON message which look like
    // {name: "name of endpoint", parameters: "JSON parameters"}
    // this is for webdrivers which run on protocols besides HTTP (like TCP)
    // we're implementing that pattern here, for this new method, but haven't translated it to all other commands yet
    ImmutableMap.Builder builder = ImmutableMap.builder();
    builder.put("name""network_connection")
           .put("parameters", ImmutableMap.of("type"connection.value));
  }
  public WebDriver context(String name) {
    if (name == null) {
      throw new IllegalArgumentException("Must supply a context name");
    }
    execute(., ImmutableMap.of("name"name));
    return AppiumDriver.this;
  }
  public Set<StringgetContextHandles() {
    Object value = response.getValue();
    try {
      List<StringreturnedValues = (List<String>)value;
      return new LinkedHashSet<String>(returnedValues);
    } catch (ClassCastException ex) {
      throw new WebDriverException("Returned value cannot be converted to List<String>: " + valueex);
    }
  }
  public String getContext() {
    if (contextName.equals("null")) {
      return null;
    }
    return contextName;
  }
  public void rotate(ScreenOrientation orientation) {
    execute(., ImmutableMap.of("orientation"orientation.value().toUpperCase()));
  }
    String orientation = response.getValue().toString().toLowerCase();
    if (orientation.equals(..value())) {
      return .;
    } else if (orientation.equals(..value())) {
      return .;
    } else {
      throw new WebDriverException("Unexpected orientation returned: " + orientation);
    }
  }
    return findElement("-ios uiautomation"using);
  }
    return findElements("-ios uiautomation"using);
  }
    return findElement("-android uiautomator"using);
  }
    return findElements("-android uiautomator"using);
  }
    return findElement("accessibility id"using);
  }
    return findElements("accessibility id"using);
  }
  public Location location() {
    return .location();
  }
  public void setLocation(Location location) {
    .setLocation(location);
  }
  private TouchAction createTap(WebElement elementint duration) {
    TouchAction tap = new TouchAction(this);
    return tap.press(element).waitAction(duration).release();
  }
  private TouchAction createTap(int xint yint duration) {
    TouchAction tap = new TouchAction(this);
    return tap.press(xy).waitAction(duration).release();
  }
  private static CommandInfo getC(String url) {
    return new CommandInfo(url.);
  }
  private static CommandInfo postC(String url) {
    return new CommandInfo(url.);
  }
  private static CommandInfo deleteC(String url) {
    return new CommandInfo(url.);
  }
  public URL getRemoteAddress() {
      return ;
  }
New to GrepCode? Check out our FAQ X