Appium - unable to locate elements in view - android

Android - real device.
appium-dotnet-driver v2.0.0.1
c# / vs2015
I have the following using uiautomatorviewer, link-image can be found here.
https://discuss.appium.io/t/cannot-locate-any-elements-in-this-view/12099
I am using the following command to locate the relative layout
ReadOnlyCollection linlayouts =
_driver.FindElementsByXPath(
"//android.widget.LinearLayout[#resource-id='com.mobilaurus.supershuttle:id/content_container']//android.widget.RelativeLayout");
as i cannot seem to locate any element in this view, all other views previous and later all work using id's and xpath's fine, for some reason this particular view appium cannot locate any items/elements - i was using driver.tap but the co-ordinates are different on several devices so not practical
I have extended the timeout but appium applies the find request and never comes back..
Anyone got any ideas as to why this would be or is there a way to always find the button at the bottom of the screen for devices.
thanks in advance

I would recommend using an online xpath tester.
1) In the Appium Inspector hit the copy xml button
2) Use the xpath you provided above and see if you are receiving a unique element back
Sorry if this isn't helpful, something about your xpath just seems strange.

Related

Python, Appium, UIAutomator2 vs 1, Selenium - resourceID search not making sense

First post, please be kind. I searched for more hours than I care to admit - desperate for an answer to a seemingly simple answer.
After a few hours, I was able to cross the bridge from Selenium to Appium, and get everything properly set up, and start automating directly to my attached Moto G4 Play. Works with the Appium desktop perfect, and despite this being my first dive into Android internals, my initial tests with Python/Appium had me "auto-tapping" through the app quite nicely.
But, I ran into a brick wall because I simply cannot scroll the screen (in my script). I figured that I needed to specify in Appium desired capabilities that I wanted to use UIAutomator2, since it has more scrolling options. But, immediately, the UIA2 engine couldn't find (by resourceID) the same element that could be found with UIA1. I don't get it. So, I decided to print every element that python script could detect on the page (with UIA2), and amongst those elements, it clearly showed the exact ResourceID that was found with UIA1. At this point, I'm lost. I can provide specifics, as requested, but hoping someone can start to point me in the right direction.
Thanks.
EDIT: Additional info and code (thanks for the feedback data_garden). Here's the line of code that fails going from UIA1 to UIA2:
HeadMaster = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID, "Head__master-id")))
A TimeoutException is thrown on UIA2 (element not found):
The element is there, no doubt about that. I not only listed it via my script, but inspected it with the Appium desktop and the UIInspector.
Is it perhaps related to some deprecated symbols? Doubt it, considering underscores and dashes are pretty mainstream.
EDIT:
Also want to add this problem is occurring running Appium Server 1.7.2.

Some pages in my android Native app are not inspectable with Appium Desktop

Certain pages in our android native app are not inspectable with Appium Desktop and Appium Studio. However with uiAutomatorViewer, I can inspect those screens and take resource-ids. But Appium is unable to locate element with these Ids/xpaths. Did check with dev team but they are not sure about what is going on. Does anyone had similar issues?
Any help or suggestions would appreciated much!!
Before finding element may be you can try getDriver().getPageSource() and by using this it will get the focus of the driver and then try finding elements. If this doesnt help try printing getdriver().getpagesource() in console and try to look whether the element is present in the pagesource or not. if it is there then you can use document builder and parse the pagesource text and get xpath element out of it and once you get the element (this will be generic XML element) get attributes like location and using that tap at that coordinates and this will defenitely work
I had the same problem just when dialogs where shown, To have it working I send the app to background for 0 secs, after a dialog was dismissed, and I was able to resume my tests
Python code
driver.background_app(secs) #you can specify 0 secs
Java code
driver.runAppInBackground(secs) //you can specify 0 secs
Let me know if it worked

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 locate elements in mobile app ? (findElement By class, id)

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.

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