[翻译]如何在Windows上安装,搭建和使用AndroidViewClient (How to Install, Setup and Use AndroidViewClient on Wind...

前言:

这是一篇关于如何在 Windows 上设置 AndroidViewClient 的教程,AndroidViewClient 是一个Android平台上的自动测试工具,类似于 monkeyrunner。 但初步调研发现这个工具比 monkeyrunner 要强大一些,所以决定尝试一下。这个项目可以在 GitHub 上查看源码和介绍原文地址在这里。
为了能在翻译中得到原文的全部信息,我会一段一段地给出原文,然后在下面给出翻译,如果有图片,我会保存下来重新上传到简书,以避免网络等问题造成图片不能查看的情况。


原文:

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 工具库,它简化了测试脚本的创建和 android 自动化测试。提供更好级别的操作和获取任何时刻展现在设备或者模拟器上的View树并且在它上面执行操作的能力。

原文:

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 通常被用来创建测试脚本,但同样可以独立使用。测试脚本创建可以通过使用 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.

译文:

经过一些实际操作,现在我觉得 AndroidViewClient 使用起来还算简单。

原文:

Tutorial for Setting up AndroidViewClient on Windows

译文:

在 Windows 上搭建 AndroidViewClient 的教程

原文:

  1. 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.

译文:

  1. 下载安装 Python 2.7。

下载地址: https://www.python.org/downloads/

注意:AndroidViewClient 不支持 Python 3.x 版本,下载 Python 2.7.11 安装器。

原文:

  1. 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.)

译文:

  1. 设置环境变量

右键“电脑” > “属性”

环境变量> Path > Edit > Append in Variable value, C:\Python27;C:\Python27\Lib;C:\Python27\DLLs;C:\Python27\Lib\lib-tk

(修改 C:\Python27 路径为你安装Python的位置。)

1-Setting-up-Environment-Variables-AndroidViewClient-Tutorial.png

原文:

  1. 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.

译文:

  1. 打开 Windows 命令行提示符并且输入 >python

它就会显示 Python 命令行正在运行

如果你得到一个类似的错误‘python’ is not recognized as an internal or external command, operable program or batch file.(译注:python命令没有识别,作为一个内部或者外部的命令,操作程序或者批处理文件),你可能错过了步骤1,2中的一些事情。

2-Type-command-python-in-Windows-command-prompt-AndroidViewClient-Tutorial.png

原文:

  1. 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:

译文:

(译注:这一步我操作的时候出错了,解决办法见后面的译注,为了保持原文和翻译的完整性,所以保留了这些已经失效的步骤。)

  1. 安装 setuptools

为了从 PyPi 下载和使用 AndroidViewClient,你需要安装setuptools 。

访问https://bootstrap.pypa.io/ez_setup.py

右键 > 另存为 > 把文件保存到桌面

打开你的桌面 > shift + 右键 > 在这里打开命令行 > 运行 python ez_setup.py

如果安装成功你会看见

3-After-successful-installation-of-setuptools-AndroidViewClient-Tutorial.png

译注:

我执行这一步的时候出问题了,提示ez_setup已经弃用了,所以我下载安装了setuptools 40.0.0,下载地址https://pypi.org/project/setuptools/#files,参考https://blog.csdn.net/gordennizaicunzai/article/details/78992700。并安装pip。

原文:

  1. Verify installation of setuptools.

Go to your Python installation folder e.g. C:\Python27 and you’ll see a new folder named Scripts.

译文

  1. 验证 setuptools 的安装。

进入 Python 的安装目录,例如 C:\Python27 你会看见一个新文件夹名叫 Scripts。

4-Scripts-folder-AndroidViewClient-Tutorial.png

原文:

  1. 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.

译文:

(译注:这一步我操作的时候出错了,解决办法见后面的译注,为了保持原文和翻译的完整性,所以保留了这些已经失效的步骤。)

  1. 运行 easy_install

进入 Scripts 目录,打开命令提示符(shift + 右键),并且运行 >easy_install.exe –upgrade androidviewclient

这会在PyPI (Python 安装包索引)搜索 AndroidViewClient,下载并且安装它,当然,不用多说,你需要网络连接。

5-easy_install.exe-upgrade-androidviewclient-AndroidViewClient-Tutorial.jpg

译注:

我执行这一步失败了,提示error: Not a URL, existing file, or requirement spec: '–upgrade',改为执行 pip install androidviewclient,挂了代理,才完成这一步的操作。

原文:

  1. 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,说明安装成功。

原文:

  1. Download zip file from GitHub.

Go to: https://github.com/dtmilano/AndroidViewClient/archive/master.zip, download and extract the zip file.

译文:

  1. 从 GitHub 下载 zip文件

进入 https://github.com/dtmilano/AndroidViewClient/archive/master.zip 下载并解压zip文件

6-AndroidViewClient-folder-after-extraction-AndroidViewClient-Tutorial.png

原文:

  1. Go to extracted folder, go to examples folder and run check-import.py

cmd: >python check-import.py

It shall output “OK”

译文:

  1. 进入解压目录,进入 examples 目录,然后运行 check-import.py
cmd: >python check-import.py

它会输出 OK

8-Run-check-import-file-AndroidViewClient-Tutorial.png

原文:

If not, you made some error setting up AndroidViewClient properly. Try to re-follow aforementioned tutorial steps carefully.

译文:

如果没有,你在搭建 AndroidViewClient 的时候出现了错误,重新跟着前面教程的步骤仔细尝试一下。

原文:

If you’ve made so far, congrats! You’ve successfully setup AndroidViewClient on your system and you’re ready to use it.

译文:

如果你走到这里,恭喜,你已经在你的系统上成功地搭建了 AndroidViewClient, 准备使用它了。

原文:

Tutorial for culebra and dump.

译文:

culebra 和 dump 教程

原文:

  1. 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

译文:

  1. 运行 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

译文:

使用详情和文档可以在这里找到:(译注:见上面的链接)

原文:

  1. 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

译文:

(译注:这一步我操作的时候出错了,解决办法见后面的译注,为了保持原文和翻译的完整性,所以保留了这些已经失效的步骤。)
安装 pillow

为了运行 culebra GUI, 你还得到一个 PIL package 的错误,在那种情况下,参考步骤7,运行 >easy_install.exe –upgrade pillow

译注:

我运行这一步也出错了,改成 pip install pillow,挂了代理,才安装成功。

原文:

Extras

  1. 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.

译文:

补充

  1. 安装 ADB

确保你的机器上已经安装了ADB, 如果你不确定或者没有安装,前往(译注:见上面的链接)

下载15秒 ADB 安装器并运行它,它会在你的机器上正确地设置好adb.

原文:

  1. 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

译文:

  1. adb 版本错误

如果你在运行 culebra 或者 dump 时得到一个 adb 版本错误,传递 -E 参数

cmd: >python culebra -E

-E 代表忽略 adb 版本检查

然后从脚本执行 : ignoreversioncheck’: True

原文:

  1. UIAutomator

AndroidViewClient internally uses uiautomator. If you’ve Android SDK installed on your machine,

Find uiautomator at: <location of your sdk>/tools/uiautomatorviewer.bat

译文:

  1. UIAutomator

AndroidViewClient 内部使用 uiautomator,如果你已经在机器上安装了 Android SDK。

在 <location of your sdk>/tools/uiautomatorviewer.bat 这里找到 uiautomator

7-UI-Automator-Viewer-AndroidViewClient-Tutorial.jpg

原文:

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 文件,文件会在存储空间的根目录(大多数情况是 /sdcard/window_dump.xml)

原文:

Let me know if I’ve missed something!

译文:

如果你有什么问题,让我知道。


后记:

下面是我在使用过程中遇到的一些问题及其解决办法:

  1. 例如要通过id点击一个按钮,调用ViewClient的findViewByIdOrRaise的时候,id应该怎么传呢?

解决方案:
通过执行 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
  1. 有些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")

  1. 根据文本内容查找View候,有时候一个页面有多个文本内容完全相同的控件,这个时候,可以指定root,root是一个View对象,这样查找只会在这个root的子view进行,从而避免查找到的不是自己想要的情况。
    vc = ViewClient(device, serialno)
    tabRoot = vc.findViewByIdOrRaise(VIEW_ID_PREFIX + u'main2_tab_layout')
    toutiaoTabText = vc.findViewWithTextOrRaise(u'头条', tabRoot)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容

  • Getting Started Use the Current Stable Version (7.1) Buil...
    Leonzai阅读 1,930评论 0 3
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,057评论 25 707
  • 昨天下午去拍食安小卫士的宣传照片,占用了时间,计划没完成,看着孩子也挺累的了,就没强求她把作业补上。但是今...
    守护你长大阅读 107评论 0 0
  • ——《愿君快乐如初》(六十三) 五岁3个月的小初哥哥很爱画画,很享受在画画班里没规没矩地创作过程。每次下课,他都会...
    爱君如初阅读 245评论 1 0
  • 你不仅是特定种类“智人”的一员,而且你还是存在于该种类历史的特殊时期,生存在地球上的一个特定地方。时间和地...
    张添雅阅读 199评论 0 1