List all elements in Selenium Python bindings for Appium

I'm trying to get started with Appium to test my company’s mobile applications. I want to use Python bindings to write scripts, and I need to start with Android applications.

I have all the examples of Appium. I can run grunt android and the tests work, and I can run the android.py sample application.

But I'm a complete newbie, and I don't have a clear picture of how to identify controls in my enterprise applications. I am experienced with Python, so I thought I would just create a list of controls and interest them.

I am stuck! For all methods, such as driver.find_elements_by_tag_name() , a specific identifier is required (or at least I did not find any template that works).

How can I introspect the Appium tree of elements that represent an Android application under testing? How can I list all the elements so that I can explore them? Is there a tree that I can find to find all the elements in the application?

I was hoping I could find the elements without having to get the source code for the applications, create applications in Eclipse, etc., but I can do this if necessary.

PS I would prefer to use Python, but would be open to using something else to do introspection if this works better. I can still write actual tests in Python if the other language was not in any way significantly better.

+6
source share
2 answers

Appium supports the WebDriver page source method. So you can do this:

 # assume you have a driver object import json source = driver.page_source source = json.loads(source) # you can now work with source as a python object 
+3
source

I would still like to learn the Selenium interface from Python. But I found an effective way to get a clear idea of ​​how the application was laid out, and it's pretty easy to understand how to write Selenium tests.

First, run the application, either on a real device connected to an Android development computer, or in an emulator. Basically, if you run adb devices , you want to see one device that launches your application. Then run the uiautomatorviewer tool, and then click the Device Screenshot toolbar icon. (There are only two icons on the toolbar: the first Open icon looks like a folder with files, and the one you want looks like a stack of mobile phones.)

Once you do this, an image of your application will appear with a screenshot on the left and an overview tree on the right. The diagram shows all the controls of the application, as well as their text labels, if any, and other information (for example, for this element true or false for this control).

One caveat: the controls are numbered, but in Selenium bindings, the numbers may not be the same. In the ApiDemos application ApiDemos the Graphics button has an index number of 4, since it is the fifth button, but I had to use index 5 to access it in its position. Index 0 was not a clickable object containing the text "API Demos" in another FrameLayout object making up the title for the screen.

So, I was able to make this change in the android.py script:

 #elem = driver.find_element_by_name('Graphics') elem = driver.find_elements_by_tag_name('TextView')[5] 

Comment on the call to driver.find_element_by_name() , and instead find the sixth TextView in the entire application. This is not best practice, but it shows that the results of the uiautomationviewer allow me to view the information I need to know about the controls.

Now I know enough to do a little introspection:

 for elem in driver.find_elements_by_tag_name('TextView'): if elem.text == "Graphics": break else: print("Could not find desired item") 

This is no better than just calling driver.find_element_by_name() , but it shows that I'm on the right track.

uiautomatorviewer is a practical solution to my problem. If you have pure-Python, let me know.

+2
source

All Articles