7

I am trying to get started with Appium for testing my company's mobile applications. I wish to use the Python bindings to write the scripts, and I need to start with Android apps.

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

But I'm a total newbie and I don't have a clear picture of how to identify the controls in my company's apps. I'm experienced with Python so I thought I would just build a list of control elements and introspect them.

I'm stuck! All of the methods like driver.find_elements_by_tag_name() require a specific identifier (or at least I haven't found any wildcard that works).

How can I introspect the Appium tree of elements that represents the Android app under test? How can I enumerate all the elements so I can introspect them? Is there a tree I can walk to find all elements in the app?

I was hoping I could figure out the elements without needing to get the source code for the apps, build the apps in Eclipse, etc. but I can do this if necessary.

P.S. I would prefer to use Python, but would be open to using something else to do the introspection if that works better. I could still write the actual tests in Python, unless the other language was significantly better somehow.

4

2 回答 2

3

我仍然想要一种从 Python 内省 Selenium 接口的方法。但是我找到了一种可行的方法来清楚地了解应用程序的布局,然后很容易弄清楚如何编写 Selenium 测试。

首先,让您的应用程序在连接到 Android 开发计算机的真实设备上或在模拟器中运行。基本上,如果您运行adb devices,您希望看到一个设备,即运行您的应用程序的设备。接下来,运行该uiautomatorviewer工具,然后单击Device Screenshot工具栏图标。(工具栏图标只有两个:第一个是Open图标,看起来像一个文件夹,一个你想要的看起来像一堆手机。)

完成此操作后,将出现您的应用程序的图像,左侧是屏幕截图,右侧是可浏览的树形轮廓。大纲显示了应用程序的所有控件,以及它们的文本标签(如果有)和其他信息(例如该clickable属性是该控件true还是false该控件的属性)。

一个警告:控件显示为编号,但在 Selenium 绑定中,数字可能不一样。在ApiDemos示例应用程序中,该Graphics按钮的索引号为 4,因为它是第五个按钮,但要通过其位置访问它,我必须使用索引 5。索引 0 是一个不可点击的对象,其中包含文本“API Demos”,在FrameLayout构成屏幕标题的不同对象。

因此,我能够对android.py脚本进行此更改:

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

注释掉driver.find_element_by_name()呼叫,而是TextView在整个应用程序中找到第六个。这不是最佳实践,但它表明uiautomationviewer结果确实让我查看了我需要了解的有关控件的内容。

现在我知道的足够多,可以做一点内省:

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

这并不比打电话更好,driver.find_element_by_name()但它表明我在正确的轨道上。

uiautomatorviewer是解决我的问题的实用方法。如果你有一个纯 Python 的,请告诉我。

于 2013-03-27T02:25:29.150 回答
3

Appium 支持 WebDriver 的“页面源”方式。所以你可以这样做:

# 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
于 2013-03-29T00:02:06.040 回答