Like selenium WebDriver locators, we have locator strategies for AndroidDriver (for android) & IOSDriver (for iOS), both extending the AppiumDriver for mobile app automation.
Prerequisites:
- selenium locators
- locator tool : Appium UI inspector
For appium java-client >=8.x, AppiumBy
class provides all the android and IOS specific locator methods to identify the app elements.
driver.findElement(AppiumBy.
Along with above app specific locators, you can still use the selenium specific locators by using below syntax
driver.findElement(By.
To get the locators you can use appium inspector.
Launch appium inspector and add the app capabilities / options to launch to app mockup on the inspector and inspect on any element to see the details as below.
We will be using sample app the-app from cloud grey
When we click on the objects, right side we can observe different locator values such as index, text, resource-id, class or content-desc etc…
findElementByName
Android – text
attribute can represented as name
iOS – name attribute is treated as ID & name
driver.findElementByName("messageinput").click(); //or driver.findElement(By.name("messageinput")).click();
findElementByAccessibilityId
Android -> accessibility-id
or content-desc
iOS -> accessibility-id
driver.findElementByAccessibilityId("Echo Box").click();
findElementById
Android -> resource-id
is treated as ID
iOS -> accessibility-id
& name
can be treated as ID
Note – for iOS, accessibility & name locator strategies are similar, either one of this we can use for ID.
driver.findElementById("Echo Box").click();
findElementByXPath
You can use combination of type
[in iOS] or className [in Android] in combination with any other attributes. refer below
driver.findElementByXPath("//XCUIElementTypeOther.widget.Button[@name='Echo Box']).click();
driver.findElementByXPath("//android.view.ViewGroup[@content-desc='Echo Box']).click();
findElementByClassName
Android – class
attribute is treated as className
WebElement element = driver.findElementByClassName("android.widget.EditText");
iOS – type
attribute is treated as className
WebElement element = driver.findElementByClassName("XCUIElementTypeTextField");
NOTE : It’s good practice to avoid the use of findElementByClassName
, as there could be many edit boxes or buttons present on the screen, and we won’t get unique class name, still sometimes this can be useful if none of the locators work.
findElements
Above details talk about driver.findElement… to identify single element on application, but if we want to capture list of webelements, then we need to use driver.findElements…
List<WebElement> buttons = driver.findElementsByClassName("android.widget.Button");
if any options that are not mentioned above, then type driver. and key press CTRL + SPACE to get all the options to identify elements.
driver can be of AndroidDriver or IOSDriver
(for appium server 1.x, java-client < 8.x)
To know how to identify the mobile apps for both Android & iOS
Selenium locators are applicable for Appium as well, we will see what else Appium specific locators we have
AndroidDriver & IOSDriver has most common findlement locators and also they have some specific locators present, which you can see by using individual drivers as below
AndroidDriver
//for android locators driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap); driver.findElement(MobileBy. //or driver.findEl
IOSDriver
//for IOS locators driver = new IOSDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap); driver.findElement(MobileBy. //or driver.findEl
Reference –
https://appium.io/docs/en/commands/element/find-elements/index.html#selector-strategies
https://appiumpro.com/editions/20-making-your-appium-tests-fast-and-reliable-part-2-finding-elements
1 Comment