Pefrorm MultiTouch Action Using Appium In Android Application Automation

MultiTouch Action In android mobile software app Is one of the common action. Earlier we learn usage of TouchAction class In appium android software automation test to perform DRAG AND DROP and SWIPE ELEMENT. You also needs to automate multi touch action In your android mobile software app. Here I have demonstrated simple example on how to perform multi touch action In your android mobile softwareapp.

Install MultiTouch Tester App In Android Mobile
We will use MultiTouch Tester software App In this android appium multi touch action automation test example. So you need to download and Install MultiTouch Tester android software App In your mobile device. You can download and Install MultiTouch Tester App from Google Play Store or THIS PAGE.

Aim To Achieve In This Test
We wants to perform multi touch at 5 different points at the same time on MultiTouch Tester App's screen as shown In bellow Image.


This Is what we wants to achieve from this appium android test example.

Get X And Y Coordinates Of All Touch Points
In above Image you can see that there are 5 points where we wants to perform MultiTouch Action In appium android automation test. You can get X Y Coordinates of all 5 touch points manually using uiautomatorviewer too as described In THIS POST

In our appium test, We will calculate X and Y Coordinates of all 5 touching points pragmatically by calculating screen's width  and then will get different x and y points using size.width and size.height methods.

Create And Run Android Appium MultiTouch Action Test
Create new MultiTouch.java file under your project and copy paste bellow given test script Inside It.

package Android;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class MultiTouch {
 AndroidDriver driver;
 Dimension size;

 @BeforeTest
 public void setUp() throws Exception {
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "ZX1B32FFXF");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "4.4.2");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage","com.the511plus.MultiTouchTester");
  capabilities.setCapability("appActivity","com.the511plus.MultiTouchTester.MultiTouchTester");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }

 @Test
 public void mTouchAction() throws InterruptedException {
  
  size = driver.manage().window().getSize();
  //Get X Y Coordinates for touch action 1(Top left side).
  int x1 = (int) (size.width * 0.20);
  int y1 = (int) (size.height * 0.20);
  
  //Get X Y Coordinates for touch action 2(Top right side).
  int x2 = (int) (size.width * 0.80);
  int y2 = (int) (size.height * 0.20);
  
  //Get X Y Coordinates for touch action 3(Bottom left side).
  int x3 = (int) (size.width * 0.20);
  int y3 = (int) (size.height * 0.80);
  
  //Get X Y Coordinates for touch action 4(Bottom right side).
  int x4 = (int) (size.width * 0.80);
  int y4 = (int) (size.height * 0.80);
  
  //Get X Y Coordinates for touch action 5(middle of the screen).
  int x5 = size.width / 2;
  int y5 = size.height / 2;
  
  // Create object of MultiTouchAction class.
  MultiTouchAction maction = new MultiTouchAction((MobileDriver) driver);

  // Set touch action1 on given X Y Coordinates of screen.
  TouchAction action1 = new TouchAction((MobileDriver) driver).longPress(x1, y1).waitAction(1500);
  // Set touch action2 on given X Y Coordinates of screen.
  TouchAction action2 = new TouchAction((MobileDriver) driver).longPress(x2, y2).waitAction(1500);
  // Set touch action3 on given X Y Coordinates of screen.
  TouchAction action3 = new TouchAction((MobileDriver) driver).longPress(x3, y3).waitAction(1500);
  // Set touch action4 on given X Y Coordinates of screen.
  TouchAction action4 = new TouchAction((MobileDriver) driver).longPress(x4, y4).waitAction(1500);
  // Set touch action5 on given X Y Coordinates of screen.
  TouchAction action5 = new TouchAction((MobileDriver) driver).longPress(x5, y5).waitAction(1500);

  // Generate multi touch action chain using different actions and perform It.
  maction.add(action1).add(action2).add(action3).add(action4).add(action5).perform();
 }

 @AfterTest
 public void End() {
  driver.quit();
 }
}

MultiTouch Action Script Description
In above test you can see,
  • First of all we have collected X and Y coordinates(x1,y1, x2,y2, ..ect) for different touch point positions.
  • MultiTouchAction Is a class which provides us a facility to generate multi touch action chain using touch action chain.
  • We have created touch action chain for all 5 touch points using TouchAction class.
  • And then added all 5 touch actions In MultiTouchAction chain using .add(TouchAction action) method.
  • perform() action will perform steps given In MultiTouchAction chain.
Now you can run above test script In your android mobile device using appium and testng. It will launch MultiTouch Tester App In your android mobile device and then perform multitouch action.

1 comment:

  1. Touch action 1 is 20 20 or 20(width from left) 80(height from bottom)

    ReplyDelete