Appium: Android Resource ID: translates to what in their selenium integration? - android

So for example I have:
wd.findElement.By(name("searchBttn"));
the resource-id in the ui xml screen cap is
com.aut.android:id/searchBttn
and Appium can't find the item. I have tried with ID and Name. Is there something else here i could try is there a direct line for line correlation list somewhere that i am missing?

You can find an Android item in Appium by Resource ID, but you have to specify the entire package name along with the resource id:
wd.findElement.By(id("com.aut.android:id/searchBttn"));

You can fetch element by few more way other than name or id is not available
Can find element find by using resource-id like below example:
driver.findElement(By.xpath("//*[#resource-id='login']")).click();
driver.findElement(By.xpath("//*[#id='cash']")).click();
Here: com.android.calculator2:id/digit5 is complete resource id and can use like this:
driver.findElement(By.id("com.android.calculator2:id/digit5")).click();
Can find by class name:
driver.findElement(By.className("android.widget.Button")).click();

At least name works for me. I'm trying to find out how it works for ID, but you can look at the following.
Example: If the button text is set to "testbtn", then you can access it from Appium with the following command:
driver.findElement(By.name("testbtn")).click();
This works for me, try it.
EDIT:
As one of the Appium devs stated here, it currently seems to be not possible to access elements by ID on Android.
EDIT2:
This feature is now implemented! As I found out, you can now access elements from Android by ID using selendroid (it seems to be a sub part of Appium).
In your test class you need to specify capabilities.setCapability("device", "selendroid"); when setting up instead of capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
Now you can just access a button with
driver.findElement(By.id("testbtn")).click();
You can find the official documentation from Selendroid here

I had the same problem in python where I couldn't find the elements that had the id attribute even though I could see them in the appium inspector.
Some of the apps have multiple contexts that the driver can handle like NATIVE_APP & WEB_VIEW so I had to switch the context of the driver to WEB_VIEW so it could find the elements with the driver.find_elements_by_id("")(python syntax)
Documentation on how to switch contexts with code examples -
http://appium.io/docs/en/writing-running-appium/web/hybrid/index.html

Related

Appium+ WebdriverIO finding element issues

I created a small test android app which has a text box and a button and am able to build this and launch on a real device (android) using WebDriverIO.
However, when I try to select elements I can't locate them.
Example: the text field has the following properties:
id: editTextTextPersonName
input type: textPersonName
contentDescription: #string/fldName1 (which is the string "helpme" in the xml)
Using UIAutomatorViewer I can see the field has the following:
resouce-id** = com.example.myfirstapp:id/editTextTextPersonName
package = android.widget.EditText
class = android.widget.EditText
content-desc: helpme
However, I cannot locate this using WebDriverIO.
I have tried
$('~helpme').setValue('test')
but that doesn't work.
Does anyone have any suggestions?
To be honest, you're doing everything correct. AFAIK there is an issue with the $('~helpme') selector within WebdriverIO/Appium not being able to find the correct element / failing on Android.
What I did in the past, especially if I needed to use cross-platform locators, was that I created a method like this
const locatorStrategy = (selector: string): string => {
return driver.isIOS ? `id=${selector}` : `//*[#content-desc="${selector}"]`;
};
which could be used like this
$(locatorStrategy('input-email'));
You can also validate your selector with Appium Inspector. This is how Appium Inspector looks like
You can then press this button
then select XPATH and add this //*[#content-desc="input-email"] selector
hit Search which will lead to this
So in your case, if you would use or something like above or just this
$('//*[#content-desc="helpme"]').setValue('test')
then it should work

Android application xpath with content desc - handle the line break

Objective: To click on Access for all guest users.
I am unable to find this element.
uiautomatorviewer shows the following screenshot.
Content description is following copy/pasted
"Access for all guest users
Provider not listed? Stay tuned, we will be adding more TV providers shortly."
Notice: the line break after user. I believe this is why my xpath is not working.
My xpath is:
"//android.view.View[#content-desc ='Access for all guest usersProvider not listed? Stay tuned, we will be adding more TV providers shortly.']"
I cannot use index instead of content-desc because of the object in layers above have the same class and index.
Using tap to a specific might solve the problem but is not a good solution.
any ideas on how to handle that linebreak.
If you notice the XML hierarchy, you will notice that your element is inside a WebView (3rd node from top). So its not getting identified. You have to first switch to WebView and then use your code.
The way you mentioned the xpath should work, once you switch to WebView.

No ID for my Android UI Testing

I would like to write some UI Test on my Android application in order to take automated screenshots.
My application is written with React-Native.
In order to write my tests in need to know the resource-id of my component but as you can see in this screenshot i can't see any resource-id with ui Automator Viewer in this React-Native example app.
Look this picture.
I would like to know if there is a way to give some IDs to my components so I can write some test or if there is another way to select my components.
Note : I'm trying to write Espresso Test.
The testID only works on iOS, and is not mapped to the resource-id in Android.
You can specify an accessibilityLabel to your component. In uiautomatorviewer it shows up as content-desc
To be cross-platform, you should specify both:
<Text style={styles.welcome} accessibilityLabel="MyId" testID="MyId">
Welcome!
</Text>
You can then search for it using something like webdriver with the tilde selector browser.waitForExist("~MyId")
First of all, ID of each UI Component of Android objects has to be defined in the resource(res) folder as layout XMLs(you need to make sure you have defined it in the right tags) and to obtain the IDs in the Android please follow the steps:
1. Launch Android Studio, Click on "Android Device Monitor" icon next to "SDK Manager"
2. Connect your device and launch the application
3. Select your device name from the list of devices on the left panel
4. Click on Dump "View Hierarchy for UI Automate"
5. Now you can hoover over any view on thde device dump and can see Resource-d in the node details in the right column.
Note: You can also take screenshots
1)First thing first if its your project you can go to the layout and add id to these component and then take screenshot using uiautomator viewer you will find your resource id
2) If option 1 is not possible(that's highly unlikely to happen) you can still test it as from your screenshot i can see only one edittext box so using uiauatomator you can test it on the basis of the class . Here below is the code:
private static UiDevice mDevice;
func(){
mDevice = UiDevice.getInstance(getInstrumentation());
UiObject xyz=new UiObject(new UiSelector().**getClass**(android.widget.EditText));
xyz.click();
xyz.setText("abcdef");
}
// I am not sure its getclass() or getclassName() check both option
//xyz.click() is necessary as in uiautoamtor you won't be able to right text
in a textbox until and unless it is selected
Yes there is! You can pass a testID prop to any component that implements the <View /> props, as documented here.
testID string
Used to locate this view in end-to-end tests.
See also this very informative blogpost about UITesting in React Native. It is about iOS testing, but the javascript part is going to be the same for Android.

Android hierarchy for XPath to interact with specific item

I'm new to automatization, Android, Selenium, Appium and xpath, too. I know it's suck a great beggining.
I write tests for Android devices, but the application I have to test have a lot of costum views. I found out the best way to interact with these custom items is to put an "android:contentDescription" field in the Views. My only question is how to get access to the element with have a specified contentDescription? This is az android specific question, I'm not even sure that the content-desc is the field I'm looking for.
I have the hierarchy provided by Android UI Animator Viewer:
http://i.imgur.com/NUGc56o.png
The ways i've tried:
xpath: //*[contains(#android:contentDescription,'example text')]
I was able to get access by finding them as an ImageView, but as I mentioned I need to work with custom Views
My code looks like somtihng like this:
driver.findElementByXPath("//*[constains(#content-desc,'Login')]").click();
Thanks for the help!
You could also try using Accessibility labels or the UIAutomator locator strategy.
Here's Appium's documentation on those.
Your xpath is incorrect. It should be: "//android.widget.ImageView[#content-desc='Login']"
Here's some pseudocode of what you should do:
login_image = driver.findElementByXPath("//android.widget.ImageView[#content-desc='Login']"); // Gets you the WebElement
print login_image.getClass(); // Just for debugging, make sure it's not nil/null
login_image.click(); // Click on it!

what does the "#+android:id/title" mean?

In normal, we should use #+id/ to define an id and use #id to reference an id. Today I found #+android:id/title in apps/settings/res/layout/preferenc_progress.xml.
How to understand it and how to use it?
It is used for resources that are shipped with the SDK.
You can take a look at them by browsing to
[PATH TO ANDROID SDK]/platforms/android-[VERSION]/data/res
By using the android in android.R.whatever you just specify the R file to look up. For more information you should read Accessing Platform Resources.
That belongs to the app preferences activity screen definition.
title and summary are standard Android fields of a TextView preference item.
I think it does the same thing. It's just a more formal way of saying it by specifying where the namespace is.
I've never met this way of giving id, but in theory this means adding new id title to android package. So you'll be able to use it in your code like android.R.id.title. But I'm not sure resource compiler will really create any id in android package. I think it can be used only with predefined ids. But I'll give you more precise answer later, when I'll be able to check it.
EDIT: I've checked it and found some differences. Firstly, if you define Android's id using #+android:id/some_id, which is already present in SDK, this id will not be defined in your R.java file. If it's not present in SDK, it will be defined in R.java, but with different kind of value. Secondly, if you'll try to convert id from its string representation to int value, Resources.getIdentifier() method will return 0 in case of #+android:id format.
This means it will create an id in your resource file.

Categories

Resources