TouchAction

Appium implements the new TouchAction API, which allows chaining touch events and, thereby, facilitates gesture implementation. Touch Action is pretty robust and supports a multitude of gestures, which ease the simulation:

We will discuss some of the methods mentioned earlier, that TouchAction supports:

  • press:
    • press(WebElement el): This method allows you to press on the center of the element
    • press(int x, int y): This method allows you to press on an absolute position (x and y coordinates)
    • press(WebElement el, int x, int y): This method allows you to press on an element offset from the upper-left corner by a number of pixels:

Consider the following usage examples:

TouchAction action = new TouchAction(appiumDriver);
action.press(appiumDriver.findElement(By.id("valid_id"))).perform();
Point point =appiumDriver.findElementById("valid_id").getLocation();
new TouchAction(appiumDriver).press(point.x + 20, point.y + 30).waitAction(1000).release().perform();
  • release:
    • release(): This method withdraws the touch

Consider the following example:

action.release();
  • long press:
    • longPress(int x, int y): This method allows you to press and hold the absolute position x,y until the context event has fired
    • longPress(int x, int y, int duration): This method allows you to press and hold for the specified duration at an absolute position x,y until the context event has fired
    • longPress(WebElement el): This method allows you to press and hold the center of an element until the context event has fired
    • longPress(WebElement el, int duration): This method allows you to press and hold the center of an element until the context event has fired
    • longPress(WebElement el, int x, int y): This method allows you to press and hold the elements in the upper-left corner, offset by the x,y amount, until the context event has fired:

Consider the following usage example:

TouchAction action = new TouchAction(appiumDriver);
action.longPress(appiumDriver.findElement(By.id("valid_id"))).perform();
  • move:
    • moveTo(int x, int y): This method allows you to move the current touch to a new position that is relative to the current position
    • moveTo(WebElement el): This method allows you to move the current touch to the center of the specified element
    • moveTo(WebElement el, int x, int y): This method allows you to move the current touch to the specified element and offset from the upper-left corner:

Consider this usage example:

WebElement drag = appiumDriver.findElement(By.id("drag_1")); 
WebElement drop = appiumDriver.findElement(By.id("drop_1"));
TouchAction dragNDrop = new TouchAction(appiumDriver).longPress(drag).moveTo(drop).release();
dragNDrop.perform();
  • perform:
    • perform(): This allows you to perform a chain of actions
  • tap:
    • tap(int x, int y): This method allows you to tap an absolute position (x,y) on the screen
    • tap(WebElement el): This method allows you to tap the center of the specified element
    • tap(WebElement el, int x, int y): This method allows you to tap an element with the specified offset from the upper-left corner
    • waitAction(): This method allows you to wait for the action to be completed and is used as a no-operation in multi-chaining
    • waitAction(int ms): This method allows you to wait for a specified amount of time to pass before it continues with performing the next touch action

Consider this usage example:

TouchAction action = new TouchAction(appiumDriver);
action.tap(appiumDriver.findElement(By.id("valid_id"))).perform();
// A case where an UI element is Start and when pressed enables another element called Stop
Point center1 = appiumDriver.findElementById("id_start").getCenter();

TouchAction action = new TouchAction(appiumDriver)
.tap(center1.x, center1.y)
.tap(appiumDriver.findElementById("id_stop"),5,5);
action.perform();

In the preceding API, when we pass coordinates along with the web element, the coordinates are treated as relative to the web element position. When we call the perform method, the sequence of the constructed event is sent to Appium, and the touch action is performed on the device.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.37.10