tap and press using appium

Some important points

  1. TouchAction class can be used to do tap, press and long press operation
  2. We can find the Point of Source and Destination element using getLocation method
  3. In same way, we can do this action in iOS as well

Below hypothetical code demonstrates how you can perform tap, press and long press actions in appium.

package org.softpost.android.simulator;

import io.appium.java_client.MultiTouchAction;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

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

/**
 * Created by Sagar on 10-07-2016.
 */
public class TapPressTest {
    AndroidDriver driver;

    @Test
    public void test1() throws Exception{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "android");

        caps.setCapability("deviceName", "Pixel 9");
        caps.setCapability("appPackage", "com.android.settings");
        caps.setCapability("appActivity","com.android.settings.Settings");
        caps.setCapability("automationName","uiautomator2");

    try {
        driver = new AndroidDriver<AndroidElement>(new URL("http://0.0.0.0:4723/wd/hub"), caps);
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        Thread.sleep(5000);


        TouchAction action = new TouchAction(((AndroidDriver<AndroidElement>)driver));

        WebElement battery = driver.findElement(By.xpath("//android.widget.TextView[contains(@text,'Battery')]"));

        //tap battery element
        action.tap(new PointOption().withCoordinates(battery.getLocation()));

        //press battery element
        action.press(new PointOption().withCoordinates(battery.getLocation()));

        WebElement search = driver.findElement(By.xpath("//*[contains(@resource-id,'com.android.settings:id/search_action_bar')]"));
        search.click();

        //long press battery element
        WebElement searchBox = driver.findElement(By.xpath("//android.widget.EditText[contains(@text,'Search')]"));
        action.longPress(new PointOption().withCoordinates(searchBox.getLocation()));

        Thread.sleep(5000);

    }catch (Exception ex) {
        ex.printStackTrace();
        System.out.println(driver.getPageSource());

    }finally {

        driver.quit();
    }
    }

    protected boolean WaitUntilElementIsDisplayed(By by, int seconds)
    {
        try
        {
            WebDriverWait wait = new WebDriverWait(driver, seconds);
            wait.until(ExpectedConditions.visibilityOfElementLocated(by));
            return true;
        }
        catch (Exception ex)
        {
            System.out.println("Exception while waiting {ex.Message}" + ex.getMessage());
            return false;
        }
    }
}

Web development and Automation testing

solutions delivered!!