Fanch
Fanch

Reputation: 3274

Which method should I use (fastest) to select element?

Well there are a lot of different methods to select elements between Drivers. I would like to know which one is the fastest and the most suitable for native apps (iOS and Android) .

With the Appium Driver class there is :

findElementByAccessibilityId(String using)

With the Mobile class there is :

findElement(org.openqa.selenium.By by) //with ById/Xpath/Name/ClassName...

With Android and iOS driver class there are :

findElementByAndroidUIAutomator(String using)
findElementByIosUIAutomation(String using)

And using the RemoteWebDriver class there are :

findElementById();
findElementByXPath();
findElementById(); //css, className etc... -> WebElement which can be cast in mobileElement

So I'm guessing using UIAutomator and UIAutomation are faster but selendroid is needed for Android 2.3+.

How do you do and why? Can you provide me some examples for findElementByAndroidUIAutomator(String using) and findElementByIosUIAutomation(String using)

I saw there are some issues with XPath selectors. From my point of view using findElement(By.name) seems quite simple.

Upvotes: 2

Views: 3780

Answers (3)

Fanch
Fanch

Reputation: 3274

Well , just use UiAutomator, UiAutomation when you can (by default client-libs do it -for Id/Name etc...-, except for XPath which is slower, so use it when you haven't the choice.

See https://github.com/appium/java-client/issues/158

I use a function which takes in parameter the string selector and a custom enum for each type (id, xpath, even custom type like for Android parentIdChildEditText-still using UiAutomator-, etc...) -> don't do it !

After years of experience, just use AccessibilityId if you can : which

  • for ios is name in your appium xml source <=> accessibilityIdentifier for your ios dev
  • for android is contentDesc <=> same appium source/dev identifier

Otherwise, the simplest selector for you (even xpath with text, still better to have custom test-id though). For these type of tests, selector performance is your last problem, maintenance and robustness are the main points.

Upvotes: 0

user7392865
user7392865

Reputation:

How do you do and why? Can you provide me some examples for findElementByAndroidUIAutomator(String using) and findElementByIosUIAutomation(String using)

AndroidDriver driver = new AndroidDriver();
WebElement element = driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"org.zwanoo.android.speedtest:id/upload\")");

Where "org.zwanoo.android.speedtest:id/upload" is the package id together with your element id. That is the way you can find it in your UiAutomatorviewer or Appium Inspector.

Upvotes: 2

Gaurav
Gaurav

Reputation: 1370

You should follow this sequentially:

ID, Name, ClassName, XPath. Whatever is available first use it. You can also ask your developers to add unique id for each element which is consider to be the best way.

Upvotes: 0

Related Questions