Nitesh
Nitesh

Reputation: 129

Appium: Any work around to use ids of an element to automate the app

I am using Appium for testing mobile application.When I use xpath it is working fine but I am not able to work on Ids for that app.

//This is working fine

remoteDriver.findElementByXPath("/linear[1]/window[2]/linear[1]/linear[1]/linear[2]/linear[1]/text[2]").click(); // Click on signIn button 

// This doesn't recognize the element

remoteDriver.findElementById("ButtonSignIn").click(); // Click on signIn button

If anybody has experience on appium using ids then please share your experience.

Thanks

Upvotes: 4

Views: 7524

Answers (6)

Poras Bhardwaj
Poras Bhardwaj

Reputation: 1123

Use android uiautomatorviewer to get correct android element id.Click the link below:

Can we find element by ID in appium

Upvotes: 0

Mikala Easte
Mikala Easte

Reputation: 3

I have been using findElement(By.id("")) instead. For example:

remoteDriver.findElement(By.id("ButtonSignIn")).click();

It has been working well for me so far.

Upvotes: 0

Kio Krofovitch
Kio Krofovitch

Reputation: 3022

Appium "id" correlates to the Android Resource Id, and can be accessed like this:

remoteDriver.findElementById("com.exampleCompany.appName:id/ButtonSignIn").click();

One issue I have run into using IDs with Appium is ListView. Because lists are created 'on the fly' in Android, each row actually has the same Android Resource Id, even though each row is filled with different content from the user's perspective.

If you find that your buttons are part of a ListView and that is why they cannot be accessed, you could get around this by dynamically setting the content descriptors for each row as it is created, and then referencing these through Appium using "find element by name."

Upvotes: 1

leady
leady

Reputation: 11

Instead of ids, I used FindElementsByClassName("abc.widget.EditText") (for example) or FindElement(By.Name("Continue")

The class names work great...if there is more than 1 on the page...just select the index value of the one you need. For example: textfields[1].Sendkeys("1234")

Upvotes: 0

user2220762
user2220762

Reputation: 565

Using the Id is possible only for android API level 18 or above (Jelly Bean). If you use the uiautomatorviewer , then in Node Details you shall see "resource-id" which can be used easily to automate the app, but the same id will not work for android API level < 18. Work around is to use other element properties like - xpath,tagName,className etc. For more details use the link - https://github.com/appium/appium/blob/master/docs/finding-elements.md

Upvotes: 3

Saeed Gatson
Saeed Gatson

Reputation: 517

Double check the ID being used. The ID shouldn't be "ButtonSignIn" but rather "com.company.app:id/ButtonSignIn".

The call should be:

remoteDriver.findElementById("com.company.app:id/ButtonSignIn").click();

Basically, if the layout.xml file specifies the ID for the button with android:id="@+id/ButtonSignIn" (and your app is com.company.app) then in your test you should use "com.company.app:id/ButtonSignIn" instead of "ButtonSignIn".

Also find by ID should work with Appium because it uses Selendroid, which supports it.

Upvotes: 0

Related Questions