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;
 
Used for Webdriver 3 touch actions See the Webriver 3 spec https://dvcs.w3.org/hg/webdriver/raw-file/default/webdriver-spec.html The flow is to chain individual touch actions into an entire gesture. e.g. TouchAction action = new TouchAction(driver); action.press(element).waitAction(300).moveTo(element1).release().perform(); Calling perform() sends the action command to the Mobile Driver. Otherwise, more and more actions can be chained.
 
 public class TouchAction {
 
   private MobileDriver driver;
 
   public TouchAction(MobileDriver driver) {
     this. = driver;
      = ImmutableList.builder();
   }

  
Press on the center of an element.

Parameters:
el element to press on
Returns:
this TouchAction, for chaining
 
   public TouchAction press(WebElement el) {
     ActionParameter action = new ActionParameter("press", (RemoteWebElement)el);
     .add(action);
     return this;
   }

  
Press on an absolute position on the screen

Parameters:
x x coordinate
y y coordinate
Returns:
this TouchAction, for chaining
 
   public TouchAction press(int xint y) {
     ActionParameter action = new ActionParameter("press");
     action.addParameter("x"x);
     action.addParameter("y"y);
     .add(action);
     return this;
   }

  
Press on an element, offset from upper left corner by a number of pixels

Parameters:
el element to press on
x x offset
y y offset
Returns:
this TouchAction, for chaining
 
   public TouchAction press(WebElement elint xint y) {
     ActionParameter action = new ActionParameter("press", (RemoteWebElement)el);
     action.addParameter("x"x);
     action.addParameter("y"y);
     .add(action);
     return this;
   }

  
Remove the current touching implement from the screen (withdraw your touch)

Returns:
this TouchAction, for chaining
 
   public TouchAction release() {
     ActionParameter action = new ActionParameter("release");
     .add(action);
     return this;
   }

  
Move current touch to center of an element.

Parameters:
el element to move to
Returns:
this TouchAction, for chaining
  public TouchAction moveTo(WebElement el) {
    ActionParameter action = new ActionParameter("moveTo", (RemoteWebElement)el);
    .add(action);
    return this;
  }

  
Move current touch to an absolute position on the screen

Parameters:
x x coordinate
y y coordinate
Returns:
this TouchAction, for chaining
  public TouchAction moveTo(int xint y) {
    ActionParameter action = new ActionParameter("moveTo");
    action.addParameter("x"x);
    action.addParameter("y"y);
    .add(action);
    return this;
  }

  
Move current touch to an element, offset from upper left corner

Parameters:
el element to move current touch to
x x offset
y y offset
Returns:
this TouchAction, for chaining
  public TouchAction moveTo(WebElement elint xint y) {
    ActionParameter action = new ActionParameter("moveTo", (RemoteWebElement)el);
    action.addParameter("x"x);
    action.addParameter("y"y);
    .add(action);
    return this;
  }

  
Tap the center of an element.

Parameters:
el element to tap
Returns:
this TouchAction, for chaining
  public TouchAction tap(WebElement el) {
    ActionParameter action = new ActionParameter("tap", (RemoteWebElement)el);
    .add(action);
    return this;
  }

  
Tap an absolute position on the screen

Parameters:
x x coordinate
y y coordinate
Returns:
this TouchAction, for chaining
  public TouchAction tap(int xint y) {
    ActionParameter action = new ActionParameter("tap");
    action.addParameter("x"x);
    action.addParameter("y"y);
    .add(action);
    return this;
  }

  
Tap an element, offset from upper left corner

Parameters:
el element to tap
x x offset
y y offset
Returns:
this TouchAction, for chaining
  public TouchAction tap(WebElement elint xint y) {
    ActionParameter action = new ActionParameter("tap", (RemoteWebElement)el);
    action.addParameter("x"x);
    action.addParameter("y"y);
    .add(action);
    return this;
  }

  
A wait action, used as a NOP in multi-chaining

Returns:
this TouchAction, for chaining
  public TouchAction waitAction() {
    ActionParameter action = new ActionParameter("wait");
    .add(action);
    return this;
  }

  
Waits for specified amount of time to pass before continue to next touch action

Parameters:
ms time in milliseconds to wait
Returns:
this TouchAction, for chaining
  public TouchAction waitAction(int ms) {
    ActionParameter action = new ActionParameter("wait");
    action.addParameter("ms"ms);
    .add(action);
    return this;
  }

  
Press and hold the at the center of an element until the contextmenu event has fired.

Parameters:
el element to long-press
Returns:
this TouchAction, for chaining
  public TouchAction longPress(WebElement el) {
    ActionParameter action = new ActionParameter("longPress", (RemoteWebElement)el);
    .add(action);
    return this;
  }

  
Press and hold the at an absolute position on the screen until the contextmenu event has fired.

Parameters:
x x coordinate
y y coordinate
Returns:
this TouchAction, for chaining
  public TouchAction longPress(int xint y) {
    ActionParameter action = new ActionParameter("longPress");
    action.addParameter("x"x);
    action.addParameter("y"y);
    .add(action);
    return this;
  }

  
Press and hold the at an elements upper-left corner, offset by the given amount, until the contextmenu event has fired.

Parameters:
el element to long-press
x x offset
y y offset
Returns:
this TouchAction, for chaining
  public TouchAction longPress(WebElement elint xint y) {
    ActionParameter action = new ActionParameter("longPress", (RemoteWebElement)el);
    action.addParameter("x"x);
    action.addParameter("y"y);
    .add(action);
    return this;
  }

  
Cancel this action, if it was partially completed by the driver
  public void cancel() {
    ActionParameter action = new ActionParameter("wait");
    .add(action);
    this.perform();
  }

  
Perform this chain of actions on the driver.

Returns:
this TouchAction, for possible segmented-touches.
  public TouchAction perform() {
    return this;
  }

  
Get the mjsonwp parameters for this Action

Returns:
A map of parameters for this touch action to pass as part of mjsonwp
    ImmutableList.Builder parameters = ImmutableList.builder();
    for (ActionParameter action : actionList){
      parameters.add(action.getParameterMap());
    }
    return ImmutableMap.of("actions"parameters.build());
  }
  protected void clearParameters() {
     = ImmutableList.builder();
  }

  
Just holds values to eventually return the parameters required for the mjsonwp
  private class ActionParameter {
    private String actionName;
    public ActionParameter(String actionName) {
      this. = actionName;
       = ImmutableMap.builder();
    }
    public ActionParameter(String actionNameRemoteWebElement el) {
      this. = actionName;
       = ImmutableMap.builder();
      addParameter("element"el.getId());
    }
    public ImmutableMap<StringObjectgetParameterMap() {
      ImmutableMap.Builder builder = ImmutableMap.builder();
      builder.put("action").put("options".build());
      return builder.build();
    }
    public void addParameter(String nameObject value) {
      .put(namevalue);
    }
  }
New to GrepCode? Check out our FAQ X