How to locate elements in mobile app ? (findElement By class, id) - android

i am learning automation testing with mobile applications.
When i had gone through some tutorials i found that we can locate elements using UIAutomator Viewer-- by taking screenshot and finding elements.
But by taking screenshot i could not find elements of some elements.
So, do we have any other ways to find elements or any other tools like UIAutomator?
Please help.
If u have any good links for mobile automation testing please do forward.
Thanks.

UIAutomator is provided by Android SDK and this is part of Android tools.
You can use the tool that provided by Appium. Please see details and example here

Generally, Install UI automator incase you are using appium in windows, if your using mac, you can identify using appium inspector.
find_element(class: "classname")
find_element(id: "resource-id name")
if you want to click on them add .click
find_element(class: "classname").click
If your still not having the details of id , text , content-desc, you can choose to add xpath
Go through this link for more info.
If you are part of testing team, testing on the app being developed, You may raise a bug to the developer in case, there are no unique identifiers and ask the dev team to add one.

When you are trying to find elements using UIautomatorviewer, you need to highlight the element which you want to find the locator.
If it's not giving you either id, name or xpath then you have to write
xpath manually. For writing xpaths manually refer these link1 and link2.
As another option, you can use appium desktop client to find the elements in the mobile app. For that you need to follow the below steps,
1. In appium window, click on 'Start Inspector Session' icon
2. In there you need to add the relevant capabilities there.
refer the image here.
desiredCapabilities.setCapability("app", "C:\\Users\\VWEERAY\\Desktop\\APPIUM\\mediashare.apk");
desiredCapabilities.setCapability("appPackage", "com.android.chrome");
desiredCapabilities.setCapability("appActivity", "org.chromium.chrome.browser.document.ChromeLauncherActivity");
desiredCapabilities.setCapability("platformName", "Android");
desiredCapabilities.setCapability("deviceName", "192.168.56.101:5555");
desiredCapabilities.setCapability("deviceName", "AndroidDevice1:5554");
desiredCapabilities.setCapability("platformVersion", "6.0");
3.Then click on 'Start Session' in the bottom.
4. It will open with a Mobile view and from there you can find locators like in the uiautomatorviewer.

Related

Xpath for Android element using Appium

I am new to automation. I am working on Android + Robot Framework + Appium. I am trying to get access to this 3 locators on the picture, but nothing seems to work.
As you can see, some of them even don't have an id or any other elements, so I guess I should use xpath to access them, but there is so many layers...
Could anyone please help me?
The main problem for me was to write a proper xpath, as I didn't know I should use classes for this.
So if I wanted to use classes and their indexes, the correct answer for third view is:
xpath=//android.view.View[#index='1']/android.widget.FrameLayout[#index='3']/android.view.View[#index='0']
And two previous paths are similar.
You are using windows so, you can use UIAutomator available in android-sdk/tools to locate the elements. Here is an image of that tool
Not sure if you are using Appium Inspector only.
But you can certainly find the X-Path to an element using it (image attached) :
Note : This is a random image from web just to depict the x-path is an attribute for elements inspected using Appium Inspector.
On the contrary to inspect elements within a WebView for an application, I would suggest using
chrome://inspect/#devices
on your chrome browser.

How to use selectors in Mobile App using Appium

I am trying to inspect an Android/iOS app .
When i inspect an android app, I get the following:-
resource-id: com.company.droid.qa:id/textNewsDate
class:- android.widget.TextView
package:- com.company.droid.qa
Will package name will be added in resource-id across all ids in app ?
For Some element, i did't see the text only see the id. Can i know why ?
Using: UIAutomatorViewer to inspect an android app
Is there any better tool to inspect an android/ios app in linux machine?
UIAutomator is good to find the locator of an element. The reason you don't see text/ID for some elements because your developers didn't add it for that element. You can ask your developer to modify it as per your need.

Issue automating Cordova application using Appium

We have a Cordova mobile application designed for Android platform. When we use Appium Inspector to inspect element properties we do not see either resource-id or content-desc for different elements. Could anyone please help in getting locators for these elements? Or is there anything that has to be taken care during application build time for getting any identifiers?
Attaching screenshot from Appium Inspector Window and Page Source for the page for reference.
Thanks in advance for any help!
Appium Version: 1.2.3
Java Client : 2.0.0
Page Source:
enter image description here
Finally I was able to resolve the issue, when WebView was exposed for the application. And I could inspect element properties on different screens on the mobile application.
The developers enabled this view. If anyone else is also struggling with this issue.
Please make sure you contact your developers and ask them to enable WebView for mobile application.

How to create Xpath with the help of UIAutomator for android App?

How to create Xpath with the help of UIAutomator for android App?
Link to any guide would be very helpful.
Currently UIautomator does not gives Xpath. I want to know the steps to create a xpath.
Ok so I recommend reading this article: http://www.w3schools.com/XPath/xpath_syntax.asp
But to give you an idea if you wanted to find a TextView with the text "hello world" you could use this xpath:
//android.widget.TextView[#text='hello world']
// means get any element
//android.widget.TextView means get any element with the class android.Widget.TextView
//android.widget.TextView[#text='hello world'] means get any element of class
android.Widget.TextView with attribute "text" equal to "hello world"
I hope this explanation helps. If you want a better understanding read that article
You can access elements form their ID in R.class. For example:
EditText editText = (EditText) getSolo().getView(R.id.note);
You can find it in UIautomator at resource-id column. Also you can click on elements by text and etc.
If you are testing web app, you can emulate view in chrome and get x-path from there. Go to Developer tools -> emulation and select the device and agent that you need.
I was using Robotium framework
and if you don`t know how to build xpath, here is good tutorial http://www.w3schools.com/XPath/
It is better to go for appium inspector (appium desktop app) if you are using appium mode, else if in selendroid mode use selendroid inspector - http://selendroid.io/inspector.html

How to determine the XPATH of Android UI elements. Selenium + Appium

I am currently working on automating an android application using Appium and Selenium and have run into some problems with locating elements by ID. I want to use XPATH but do not know how to get the XPATH of android UI elements. Does anyone know how to determine the XPATH of an element or know of a tool that can do this work quickly? Thanks!
You can use android sdk tool - uiautomatorviewer( https://developer.android.com/tools/testing/testing_ui.html) it's like appium inspector, but work better for me.
com.android2.calculator3:id/digit8
For Elements with id
driver.findElement(By.id("com.example.testapp:id/txtLogin"))
For Elements with class
android.widget.EditText[#index=0]
Syntax
{ClassName}[#{anyAttributeName}={valueOfAttribute}]
Code
driver.findElements(By.className("android.widget.EditText"));//will return ARRAY
There is a way you can figure out the xpath. First get the page source from the application. This will be in xml format. Then copy the source to a xpath tester website like xpath tester. Then create the xpath and check if it works. This is assuming you already know how to create xpaths.
Appium's client comes with an Inspector tool!
This will tell you the xpath of whatever element you're interested in
Try viewing the source code:
https://android.stackexchange.com/questions/5039/how-do-i-view-source-in-the-android-browser
Then figuring out the xpath becomes easier from there.
You can use UIAutomatorviewer utility in Android SDK to get the required locator. Go to your Android SDK installation's 'tools' folder and trigger uiautomatorviewer.bat file. From there, you can locate app elements.
Visit this link which shows how to use it.
http://www.testingdiaries.com/locate-android-app-elements/
Enable developer options in your phone and Connect your phone and invoke the chrome inspector for android devices( type -- about:inspect in chrome ) your device will be displayed.
Just click on the Inspect button & the screen in mobile will be displayed.
You are provided with the application and the complete DOM structure.. Make your own XPATH from that.
You could use it like this:
WebElement btn_yes = driver.findElement(By.xpath("//android.widget.Button[#text='Yes']"));

Categories

Resources