前言:
這是一篇關于如何在 Windows 上設置 AndroidViewClient 的教程,AndroidViewClient 是一個Android平臺上的自動測試工具,類似于 monkeyrunner。 但初步調(diào)研發(fā)現(xiàn)這個工具比 monkeyrunner 要強大一些,所以決定嘗試一下。這個項目可以在 GitHub 上查看源碼和介紹。原文地址在這里。
為了能在翻譯中得到原文的全部信息,我會一段一段地給出原文,然后在下面給出翻譯,如果有圖片,我會保存下來重新上傳到簡書,以避免網(wǎng)絡等問題造成圖片不能查看的情況。
原文:
How to Install, Setup and Use AndroidViewClient on Windows
By Darpan Dodiya on March 26, 2016
譯文:
如何在Windows上安裝,搭建和使用AndroidViewClient
作者 Darpan Dodiya 2016年3月26日
原文:
Step by step tutorial on setting up AndroidViewClient on Windows machines.
譯文:
教你在 windows 機器上一步一步地搭建 AndroidViewClient
原文:
AndroidViewClient is a python library and tools that simplifies test script creation and android test automation, providing higher level operations and the ability of obtaining the tree of Views present at any given moment on the device or emulator screen and perform operations on it.
譯文:
AndroidViewClient 是一個 python 工具庫,它簡化了測試腳本的創(chuàng)建和 android 自動化測試。提供更好級別的操作和獲取任何時刻展現(xiàn)在設備或者模擬器上的View樹并且在它上面執(zhí)行操作的能力。
原文:
AndroidViewClient is commonly used to create tests scripts but can be in stand-alone scripts as well. Test script creation can be even further simplified and automated by using Culebra.
譯文:
AndroidViewClient 通常被用來創(chuàng)建測試腳本,但同樣可以獨立使用。測試腳本創(chuàng)建可以通過使用 Culebra 變得更加簡單和自動化。
原文:
Culebra is a sweet tool!
譯文:
Culebra 是一個美麗的工具。
原文:
AndroidViewClient on GitHub: https://github.com/dtmilano/AndroidViewClient
譯文:
AndroidViewClient 在 GitHub 的地址:https://github.com/dtmilano/AndroidViewClient](https://github.com/dtmilano/AndroidViewClient
原文:
When I decided to learn and use AndroidViewClient, it took me a day to figure how to utilize it. Mostly because I was newbie to Python language and didn’t know much about the setup tools.
譯文:
當我決定學習和使用 AndroidViewClient,我花費了一天的時間來弄明白怎樣使用它。主要是因為我是 Python 語言的新手,不知道如何搭建這些工具。
原文:
After a bit of hands on, now I feel AndroidViewClient is fairly easy to employ.
譯文:
經(jīng)過一些實際操作,現(xiàn)在我覺得 AndroidViewClient 使用起來還算簡單。
原文:
Tutorial for Setting up AndroidViewClient on Windows
譯文:
在 Windows 上搭建 AndroidViewClient 的教程
原文:
- Download and install Python 2.7.
Download link: https://www.python.org/downloads/
Note: AndroidViewClient doesn’t support Python 3.x versions. Download the Python 2.7.11 installer.
譯文:
- 下載安裝 Python 2.7。
下載地址: https://www.python.org/downloads/
注意:AndroidViewClient 不支持 Python 3.x 版本,下載 Python 2.7.11 安裝器。
原文:
- Setup system environment variables.
Right Click on Computer > Properties.
From the left pane, Click on, Advanced System Settings.
Environment Variables > Path > Edit > Append in Variable value, C:\Python27;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk
(Change C:\Python27 path as per your installation directory of Python.)
譯文:
- 設置環(huán)境變量
右鍵“電腦” > “屬性”
環(huán)境變量> Path > Edit > Append in Variable value, C:\Python27;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk
(修改 C:\Python27 路徑為你安裝Python的位置。)

原文:
- Open Windows command prompt and type >python
It shall show Python shell running.
If you get an error like, ‘python’ is not recognized as an internal or external command, operable program or batch file., you might have missed something from Step 1, 2.
譯文:
- 打開 Windows 命令行提示符并且輸入 >python
它就會顯示 Python 命令行正在運行
如果你得到一個類似的錯誤‘python’ is not recognized as an internal or external command, operable program or batch file.(譯注:python命令沒有識別,作為一個內(nèi)部或者外部的命令,操作程序或者批處理文件),你可能錯過了步驟1,2中的一些事情。

原文:
- Install setuptools
You need to have setuptools installed in order to download and use AndroidViewClient library from PyPi.
Go to: https://bootstrap.pypa.io/ez_setup.py
Right Click > Save as > Save file on Desktop.
Open your Desktop > Shift + Right Click > Open command window here > Run: >python ez_setup.py
If the installation is successful, you’ll see:
譯文:
(譯注:這一步我操作的時候出錯了,解決辦法見后面的譯注,為了保持原文和翻譯的完整性,所以保留了這些已經(jīng)失效的步驟。)
- 安裝 setuptools
為了從 PyPi 下載和使用 AndroidViewClient,你需要安裝setuptools 。
訪問https://bootstrap.pypa.io/ez_setup.py
右鍵 > 另存為 > 把文件保存到桌面
打開你的桌面 > shift + 右鍵 > 在這里打開命令行 > 運行 python ez_setup.py
如果安裝成功你會看見

譯注:
我執(zhí)行這一步的時候出問題了,提示ez_setup已經(jīng)棄用了,所以我下載安裝了setuptools 40.0.0,下載地址https://pypi.org/project/setuptools/#files,參考https://blog.csdn.net/gordennizaicunzai/article/details/78992700。并安裝pip。
原文:
- Verify installation of setuptools.
Go to your Python installation folder e.g. C:\Python27 and you’ll see a new folder named Scripts.
譯文
- 驗證 setuptools 的安裝。
進入 Python 的安裝目錄,例如 C:\Python27 你會看見一個新文件夾名叫 Scripts。

原文:
- Run easy_install
Go to that Scripts folder, open command prompt (Shift + Right Click) and run: >easy_install.exe –upgrade androidviewclient
This will search PyPI (Python Package Index) for AndroidViewClient, download and install it. Of course, it goes without saying that you will require internet connection.
譯文:
(譯注:這一步我操作的時候出錯了,解決辦法見后面的譯注,為了保持原文和翻譯的完整性,所以保留了這些已經(jīng)失效的步驟。)
- 運行 easy_install
進入 Scripts 目錄,打開命令提示符(shift + 右鍵),并且運行 >easy_install.exe –upgrade androidviewclient
這會在PyPI (Python 安裝包索引)搜索 AndroidViewClient,下載并且安裝它,當然,不用多說,你需要網(wǎng)絡連接。

譯注:
我執(zhí)行這一步失敗了,提示error: Not a URL, existing file, or requirement spec: '–upgrade',改為執(zhí)行 pip install androidviewclient,掛了代理,才完成這一步的操作。
原文:
- Verify installation of AndroidViewClient
Go to: C:\Python27\Lib\site-packages
You shall see a folder named, androidviewclient-11.5.1-py2.7.egg. Implying the installation has been successful.
譯文:
驗證 AndroidViewClient 的安裝
進入 C:\Python27\Lib\site-packages
你會看見一個文件夾名叫 androidviewclient-11.5.1-py2.7.egg,說明安裝成功。
原文:
- Download zip file from GitHub.
Go to: https://github.com/dtmilano/AndroidViewClient/archive/master.zip, download and extract the zip file.
譯文:
- 從 GitHub 下載 zip文件
進入 https://github.com/dtmilano/AndroidViewClient/archive/master.zip 下載并解壓zip文件

原文:
- Go to extracted folder, go to examples folder and run check-import.py
cmd: >python check-import.py
It shall output “OK”
譯文:
- 進入解壓目錄,進入 examples 目錄,然后運行 check-import.py
cmd: >python check-import.py
它會輸出 OK

原文:
If not, you made some error setting up AndroidViewClient properly. Try to re-follow aforementioned tutorial steps carefully.
譯文:
如果沒有,你在搭建 AndroidViewClient 的時候出現(xiàn)了錯誤,重新跟著前面教程的步驟仔細嘗試一下。
原文:
If you’ve made so far, congrats! You’ve successfully setup AndroidViewClient on your system and you’re ready to use it.
譯文:
如果你走到這里,恭喜,你已經(jīng)在你的系統(tǒng)上成功地搭建了 AndroidViewClient, 準備使用它了。
原文:
Tutorial for culebra and dump.
譯文:
culebra 和 dump 教程
原文:
- Run culebra / dump tools
Go to, extracted zip folder/tools. You’ll see two files, culebra and dump. They are very handy tools in script generation.
Use them via,
cmd: >python culebra
cmd: >python dump
譯文:
- 運行 culebra / dump 工具
進入解壓zip的目錄/tools, 你會看見兩個文件,culebra 和 dump, 他們是生成腳本非常方便的工具。
這樣使用:
cmd: >python culebra
cmd: >python dump
原文:
Detailed usage and documentation can be found at:
Culebra – https://github.com/dtmilano/AndroidViewClient/wiki/culebra
Culebra GUI – https://github.com/dtmilano/AndroidViewClient/wiki/Culebra-GUI
Dump – https://github.com/dtmilano/AndroidViewClient/wiki/dump
譯文:
使用詳情和文檔可以在這里找到:(譯注:見上面的鏈接)
原文:
- Install pillow
In order to run culebra GUI, you’ll get error of PIL package. In that case,
Refer to step 7, and run: >easy_install.exe –upgrade pillow
譯文:
(譯注:這一步我操作的時候出錯了,解決辦法見后面的譯注,為了保持原文和翻譯的完整性,所以保留了這些已經(jīng)失效的步驟。)
安裝 pillow
為了運行 culebra GUI, 你還得到一個 PIL package 的錯誤,在那種情況下,參考步驟7,運行 >easy_install.exe –upgrade pillow
譯注:
我運行這一步也出錯了,改成 pip install pillow,掛了代理,才安裝成功。
原文:
Extras
- ADB installation
Make sure you’ve ADB installed on your machine. If you’re unsure or haven’t installed it yet,
Head over to: http://forum.xda-developers.com/showthread.php?p=48915118
Download 15 Seconds ADB Installer and run it. It shall set up adb correctly on your machine.
譯文:
補充
- 安裝 ADB
確保你的機器上已經(jīng)安裝了ADB, 如果你不確定或者沒有安裝,前往(譯注:見上面的鏈接)
下載15秒 ADB 安裝器并運行它,它會在你的機器上正確地設置好adb.
原文:
- adb version error
If you get adb version error while running culebra or dump, pass -E as argument.
cmd: >python culebra -E
-E stands for Ignore ADB version check
While running from script: ignoreversioncheck’: True
譯文:
- adb 版本錯誤
如果你在運行 culebra 或者 dump 時得到一個 adb 版本錯誤,傳遞 -E 參數(shù)
cmd: >python culebra -E
-E 代表忽略 adb 版本檢查
然后從腳本執(zhí)行 : ignoreversioncheck’: True
原文:
- UIAutomator
AndroidViewClient internally uses uiautomator. If you’ve Android SDK installed on your machine,
Find uiautomator at: <location of your sdk>/tools/uiautomatorviewer.bat
譯文:
- UIAutomator
AndroidViewClient 內(nèi)部使用 uiautomator,如果你已經(jīng)在機器上安裝了 Android SDK。
在 <location of your sdk>/tools/uiautomatorviewer.bat 這里找到 uiautomator

原文:
Another way to take uiautomator dump is: >adb shell uiautomator dump
This will take dump and store it in window_dump.xml file on device. File location will be inside root folder of storage space. (Mostly, /sdcard/window_dump.xml)
譯文:
獲取 uiautomator dump 的另一個方式是 >adb shell uiautomator dump
這會獲取 dump 并把它保存在設備的 window_dump.xml 文件,文件會在存儲空間的根目錄(大多數(shù)情況是 /sdcard/window_dump.xml)
原文:
Let me know if I’ve missed something!
譯文:
如果你有什么問題,讓我知道。
后記:
下面是我在使用過程中遇到的一些問題及其解決辦法:
- 例如要通過id點擊一個按鈕,調(diào)用ViewClient的findViewByIdOrRaise的時候,id應該怎么傳呢?
解決方案:
通過執(zhí)行 python .\dump, 就能看見這個按鈕的id是com.kaolafm.app.monkeyrunnerstudy:id/first_button,沒錯,就是這么長。要是只寫id/first_button,就會拋出一個異常:com.dtmilano.android.viewclient.ViewNotFoundException: Couldn't find View with ID='id/first_button' in tree with root=ROOT
PS D:\work\opensource\AndroidViewClient\tools> python .\dump
android.widget.FrameLayout
android.widget.TextView MonkeyRunnerStudy
android.widget.Button com.kaolafm.app.monkeyrunnerstudy:id/first_button FirstButton
- 有些view沒有Id,例如Android自動生成的toolbar,這個時候,可以運行culebra,查看AndroidViewClient自動生成的Id或者查找代碼,這樣可以方便我們找到控件的id從而進行操作,例如
PS D:\work\opensource\AndroidViewClient\examples> python ..\tools\culebra
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2013-2018 Diego Torres Milano
Created on 2018-08-14 by Culebra v15.4.0
__ __ __ __
/ \ / \ / \ / \
____________________/ __\/ __\/ __\/ __\_____________________________
___________________/ /__/ /__/ /__/ /________________________________
| / \ / \ / \ / \ \___
|/ \_/ \_/ \_/ \ o \
\_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
"""
import re
import sys
import os
from com.dtmilano.android.viewclient import ViewClient
TAG = 'CULEBRA'
_s = 5
_v = '--verbose' in sys.argv
kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'debug': {}, 'startviewserver': True, 'compresseddump': True}
vc = ViewClient(device, serialno, **kwargs2)
#vc.dump(window='-1') # FIXME: seems not needed
vc.dump(window='-1')
no_id1 = vc.findViewByIdOrRaise("id/no_id/1")
com_kaolafm_app_monkeyrunnerstudy___id_drawer_layout = vc.findViewByIdOrRaise("com.kaolafm.app.monkeyrunnerstudy:id/drawer_layout")
no_id3 = vc.findViewByIdOrRaise("id/no_id/3")
no_id4 = vc.findViewByIdOrRaise("id/no_id/4")
no_id4 = vc.findViewWithContentDescriptionOrRaise(u'''Open navigation drawer''')
no_id5 = vc.findViewByIdOrRaise("id/no_id/5")
no_id5 = vc.findViewWithTextOrRaise(u'我的主頁')
no_id6 = vc.findViewByIdOrRaise("id/no_id/6")
no_id6 = vc.findViewWithContentDescriptionOrRaise(u'''更多選項''')
com_kaolafm_app_monkeyrunnerstudy___id_fab = vc.findViewByIdOrRaise("com.kaolafm.app.monkeyrunnerstudy:id/fab")
- 根據(jù)文本內(nèi)容查找View候,有時候一個頁面有多個文本內(nèi)容完全相同的控件,這個時候,可以指定root,root是一個View對象,這樣查找只會在這個root的子view進行,從而避免查找到的不是自己想要的情況。
vc = ViewClient(device, serialno)
tabRoot = vc.findViewByIdOrRaise(VIEW_ID_PREFIX + u'main2_tab_layout')
toutiaoTabText = vc.findViewWithTextOrRaise(u'頭條', tabRoot)