Selenium TouchActions

Applies to CrossBrowserTesting SaaS, last modified on January 19, 2022

For this document, we provide all example files in our Selenium TouchActions GitHub Repository.

The TouchActions class of Selenium implements actions for touch enabled devices, reusing the available composite and builder design patterns from Actions.

In this guide we will use JUnit for testing using the Selenium WebDriver and Java programming language to demonstrate using the longpress touch action to draw within a canvas element on iOS.

Run a test

Note: You will need to use your Username and Authkey to run your tests on CrossBrowserTesting To get yours, sign up for a free trial or purchase a plan.
  1. Download Selenium WebDriver for Java.

    Download Selenium for Java

    Click the image to enlarge it.

  2. Add the Selenium jars to the build path of your project.

    • Right click your project folder and select Properties.

      Eclipse folder menu

      Click the image to enlarge it.

    • From the Java Build Path tab, select the Libraries tab.

      Eclipse build path

      Click the image to enlarge it.

    • Select Add External JARs and add all jars for Selenium.

      Eclipse add JARs

      Click the image to enlarge it.

      Eclipse selected JARs

      Click the image to enlarge it.

    • Apply and Close.

  3. Create a new JUnit Test Case

    • Right click your project folder.

    • Hover over New and select JUnit Test Case.

      Eclipse JUnit testcase

      Click the image to enlarge it.

    • Name your test case and select Finish.

      Eclipse new JUnit

      Click the image to enlarge it.

    • Select OK to add JUnit to the build path.

      Eclipse add JUnit

      Click the image to enlarge it.

  4. Copying the following content:


    import java.util.concurrent.TimeUnit;
    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import com.mashape.unirest.http.Unirest;
    import com.mashape.unirest.http.exceptions.UnirestException;
    import io.appium.java_client.TouchAction;
    import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.ios.IOSDriver;
    import io.appium.java_client.ios.IOSElement;
    import io.appium.java_client.touch.LongPressOptions;
    import io.appium.java_client.touch.offset.PointOption;
    import java.util.HashMap;

    class TouchActionExample{
        String username = "YOUR_USERNAME";
        //username can be found on the Manage Account page, ensure @ is encoded with %40
        String authkey = "YOUR_AUTHKEY"; //authkey can be found on the Manage Account page
        String score;
        AppiumDriver driver;
        String sessionid;

        void setUp() throws Exception {
             //set capabilites
             DesiredCapabilities caps = new DesiredCapabilities();
                 HashMap<String, String> cbtoptions = new HashMap<String, String>();
                 cbtoptions.put("record_video", "true");
                 cbtoptions.put("deviceName", "iPhone 11 Pro Max");
             caps.setCapability("browserName", "Safari");
             caps.setCapability("platformVersion", "13.2");
             caps.setCapability("platformName", "iOS");
                 caps.setCapability("cbt:options", cbtoptions);

             String hubAddress = String.format("", username, authkey);
             URL url = new URL(hubAddress);

             driver = new IOSDriver(url, caps);
             driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
             sessionid = driver.getSessionId().toString();

        void tearDown() throws Exception {
            driver.quit();                                                                                                                     //close the driver

        void test() {
            try {    
                 IOSElement element = driver.findElement(By.xpath("//div[@class='sketch-canvas-container']/canvas"));
                 new TouchAction(driver)
                 .longPress(LongPressOptions.longPressOptions().withPosition(PointOption.point(184, 233)))
                 .moveTo(PointOption.point(200, 200))
                 .moveTo(PointOption.point(250, 250))
                score = "pass";                                                                                                                //set score to pass
            catch (Exception e){
                score = "fail";                                                                                                                //set score to fail if any exceptions
                catch(Exception err) {

        //setScore function - calls our api and set the score to pass or fail after a test
        publicvoid setScore() throws UnirestException{
                    .basicAuth(username, authkey)
                    .field("score", score)
        //takeSnapshot function - calls our api and takes snapshots throughout your automated test
        publicvoid takeSnapshot(String seleniumTestId) throws UnirestException {
                    .basicAuth(username, authkey)
                    .routeParam("seleniumTestId", seleniumTestId)



  5. Run your test by selecting the Run button.

    Eclipse run

    Click the image to enlarge it.

Congratulations! You have successfully created a test using Selenium TouchActions with CBT and JUnit. Now you are ready to see your build start to run in the CrossBrowserTesting app.


By following the steps outlined in this guide, you are now able to seamlessly run a test using Selenium TouchActions with CrossBrowserTesting. If you have any questions or concerns, please feel free to reach out to our Support team.

See Also


Highlight search results