Appium - Element identification methods

You can use below element selector strategies

  1. XPATH
  2. ID
  3. Class Name
  4. Accessibility ID (XCUITest - accessibility-id attribute and Android - content-desc attribute)

XPATH Examples

If you are familiar with Selenium XPATH expressions, you already know how to write xpath expressions for Appium app elements as well. The syntax is exactly same. You can see XPATH examples here - XPATH Examples.

Below example shows how we can use various identification methods in Appium

package org.softpost.ios.simulator;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.touch.offset.PointOption;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

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

/**
 * Created by Sagar on 10-07-2016.
 */

public class AppiumTestAppTest {
    AppiumDriver driver;
    @Test
    public void testContacts() throws Exception{
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("platformName", "iOS");
        caps.setCapability("platformVersion", "11.0");
        caps.setCapability("deviceName", "iPhone 8");
        caps.setCapability("AutomationName" , "XCUITest");
        caps.setCapability("waitForQuiescence" , false);

        caps.setCapability("app", "/Users/admin/TestApp.app");

        try {
            driver = new IOSDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);
            driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            //WebElement e = driver.findElementByAccessibilityId("ComputeSumButton");
            //WebElement e = driver.findElementById("ComputeSumButton");
            WebElement e = driver.findElement(By.xpath("//*[@name='ComputeSumButton']"));

            // e = driver.findElementByTagName("XCUIElementTypeButton");

            e.click();
            Thread.sleep(2000);

        }catch (Exception ex){
            ex.printStackTrace();
        }finally {
            driver.quit();
        }

    }

}

Youtube | Facebook | Twitter | Website Home | Online Grocery Shopping Brisbane