Performing Touch actions in Android web app

We can perform the touch actions in mobile app using below class in Selenium API.

org.openqa.selenium.interactions.touch.TouchActions

Here is the sample code that illustrates the TouchActions. Note that we can not pass the instance of RemoteWebDriver to the TouchActions class as it does not implement HasTouchScreen interface. So we have created below class that extends the RemoteWebDriver and implements the HasTouchScreen interface.

package nativeapptests;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.interactions.HasTouchScreen;
import org.openqa.selenium.interactions.TouchScreen;
import org.openqa.selenium.remote.RemoteTouchScreen;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

/**
 * Created by Sagar on 06-07-2016.
 */
public class MyTouchableWebDriver extends RemoteWebDriver implements HasTouchScreen{
    TouchScreen touch;

        public MyTouchableWebDriver(URL remoteAddress, Capabilities desiredCapabilities) {
        super(remoteAddress, desiredCapabilities);
        touch = new RemoteTouchScreen(getExecuteMethod());
        }

        public TouchScreen getTouch() {
        return touch;
        }

}

Here is the main test class. Note that we have used singleTap method to build TouchAction object. Alternatively we can also use click method of Selenium.

package browsertests;

import nativeapptests.MyTouchableWebDriver;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.URL;
import java.util.concurrent.TimeUnit;

public class MyTouchableWebDriverWebTest
{
    private WebDriver driver;

    @Test
    public void loadingSinglePageTest() throws Exception{
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
          capabilities.setCapability("deviceName", "sagarcell");
         capabilities.setCapability("app", "chrome");

        driver = new MyTouchableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities) {
        };
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.get("http://www.softpost.org");

        new TouchActions(driver).singleTap(driver.findElement(By.linkText("Java"))).build().perform();
        Thread.sleep(5000);
        System.out.println(driver.getTitle());
        driver.close();
        driver.quit();
        //quit the browser
    }
}

You may also like...

  • Mayuresh Shirodkar

    Is it possible to do a similar Web touch on iOS?

    • Vishal S Sa

      Yes it is possible in iPhone as well. The API remains same.

      • Mayuresh Shirodkar

        Hey Vishal,
        I tried the code for Android and it works perfectly fine.
        Used the same code with iOS, it din’t throw any error and had a 200 response for the touch/click api.But no action happened on the phone.
        This is similar to the action which happens when i use .click().

        Note: The listeners for the button is tap and not click.