[翻译]如何在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的位置。)

[翻译]如何在Windows上安装,搭建和使用AndroidViewClient (How to Install, Setup and Use AndroidViewClient on Wind..._第1张图片
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中的一些事情。

[翻译]如何在Windows上安装,搭建和使用AndroidViewClient (How to Install, Setup and Use AndroidViewClient on Wind..._第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

如果安装成功你会看见

[翻译]如何在Windows上安装,搭建和使用AndroidViewClient (How to Install, Setup and Use AndroidViewClient on Wind..._第3张图片
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。

[翻译]如何在Windows上安装,搭建和使用AndroidViewClient (How to Install, Setup and Use AndroidViewClient on Wind..._第4张图片
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,下载并且安装它,当然,不用多说,你需要网络连接。

[翻译]如何在Windows上安装,搭建和使用AndroidViewClient (How to Install, Setup and Use AndroidViewClient on Wind..._第5张图片
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文件

[翻译]如何在Windows上安装,搭建和使用AndroidViewClient (How to Install, Setup and Use AndroidViewClient on Wind..._第6张图片
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

[翻译]如何在Windows上安装,搭建和使用AndroidViewClient (How to Install, Setup and Use AndroidViewClient on Wind..._第7张图片
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: /tools/uiautomatorviewer.bat

译文:

  1. UIAutomator

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

/tools/uiautomatorviewer.bat 这里找到 uiautomator

[翻译]如何在Windows上安装,搭建和使用AndroidViewClient (How to Install, Setup and Use AndroidViewClient on Wind..._第8张图片
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)

你可能感兴趣的:([翻译]如何在Windows上安装,搭建和使用AndroidViewClient (How to Install, Setup and Use AndroidViewClient on Wind...)