记一个Android菜鸡的走偏之路。。
前些日子给组里QA姐姐们探了探Appium这个自动化测试框架在一些平台上的使用,网上的资料都比较零散,就自己尝试归纳总结了一下搭建环境的步骤
之前未曾装过Appium的相关环境的话,按以下步骤一路走完的话应该就可以跑起来了。。
都是一些简单的操作,视实际按需取用吧
emmm仅对我本地Mac OS 10.12.6 + python 2.7.10版本负责(:з」∠)
环境搭建
0.安装Xcode Command Line Tools
(app store安装Xcode后)
terminal下输入
xcode-select --install
1.安装brew
terminal下输入
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2.git
terminal下输入
brew install git
3.使用brew安装node和npm
terminal下输入
brew install node
安装完输入 npm -v
来测试是否成功安装
4.安装java
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
下载Java SE Development Kit 8
选择jdk-xxxxx-macosx-x64.dmg下载并安装
5.安装Android Developer Tools
https://developer.android.com/index.html
获取android studio并安装运行一次
6.添加java&ADB环境变量
terminal下输入
vim ~/.bash_profile
按i进入Insert模式
在最后添加:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
#此处“jdk1.8.0_xxx.jdk”为实际安装的java版本
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export ANDROID_HOME=~/Library/Android/sdk
export PATH=$ANDROID_HOME/platform-tools:$PATH
按ESC输入:wq
回车保存退出
terminal下输入java -v
查看java版本是否正确,输入adb
查看ADB是否配置正确
7.命令行安装appium
terminal下输入
npm install -g appium
npm install -g appium-doctor
npm install wd
由于安装需要连接国外的技术站点,国内网络不稳定导致安装失败。大家可尝试使用淘宝镜像,在~目录创建文件.npmrc,然后加入一行
chromedriver_cdnurl=http://npm.taobao.org/mirrors/chromedriver
8.为python安装pip
8.1 Install easy_install
curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python
8.2 Install pip
sudo easy_install pip
9.安装Appium for python客户端
pip install Appium-Python-Client --user
10.使用appium-doctor检查安装
terminal下输入
appium-doctor
输出如
info AppiumDoctor Appium Doctor v.1.4.3
info AppiumDoctor ### Diagnostic starting ###
info AppiumDoctor ✔ The Node.js binary was found at: /usr/local/bin/node
info AppiumDoctor ✔ Node version is 8.1.4
info AppiumDoctor ✔ Xcode is installed at: /Applications/Xcode.app/Contents/Developer
info AppiumDoctor ✔ Xcode Command Line Tools are installed.
info AppiumDoctor ✔ DevToolsSecurity is enabled.
info AppiumDoctor ✔ The Authorization DB is set up properly.
WARN AppiumDoctor ✖ Carthage was NOT found!
info AppiumDoctor ✔ HOME is set to: /Users/t_chenju
info AppiumDoctor ✔ ANDROID_HOME is set to: /Users/t_chenju/Library/Android/sdk
info AppiumDoctor ✔ JAVA_HOME is set to: /Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home
info AppiumDoctor ✔ adb exists at: /Users/t_chenju/Library/Android/sdk/platform-tools/adb
info AppiumDoctor ✔ android exists at: /Users/t_chenju/Library/Android/sdk/tools/android
info AppiumDoctor ✔ emulator exists at: /Users/t_chenju/Library/Android/sdk/tools/emulator
WARN AppiumDoctor ✖ Bin directory for $JAVA_HOME is not set
info AppiumDoctor ### Diagnostic completed, 2 fixes needed. ###
info AppiumDoctor
info AppiumDoctor ### Manual Fixes Needed ###
info AppiumDoctor The configuration cannot be automatically fixed, please do the following first:
WARN AppiumDoctor - Please install Carthage. Visit https://github.com/Carthage/Carthage#installing-carthage for more information.
WARN AppiumDoctor - Add '$JAVA_HOME/bin' to your PATH environment
info AppiumDoctor ###
info AppiumDoctor
info AppiumDoctor Bye! Run appium-doctor again when all manual fixes have been applied!
info AppiumDoctor
检查WARN项
至此,本地的appium脚本测试环境就搭建好了
可按照下方 -在MQC中使用Appium进行脚本测试- 链接里的内容进行脚本开发对环境进行验证
appium reference<中文>
http://appium.io/slate/cn/master/?python
在MQC中使用Appium进行脚本测试
http://mqc.aliyun.com/doc.htm?id=17
在AWS-Device Farm中使用appium进行脚本测试
与MQC仅支持一个main.py作为testsuite,并且使用unittest作为用例测试工具不同的是,AWS-Device Farm所提供的测试基础环境是可移植并可按实际需求添加第三方拓展包的。我们需要在本地建立workspace文件夹,将此文件夹作为一个python的虚拟环境与本地开发环境隔离开来(沙盒)。在AWS上进行脚本测试时将此环境中的所必需的测试脚本文件、第三方拓展一并打包上传即可。
- 若已在上步“在MQC中使用Appium进行脚本测试”编写好可以运行的脚本文件,可以将其 main.py 与 desired_capabilities.py一同拷贝至下面的“tests”文件夹即可。
安装虚拟环境工具
- terminal下输入
sudo easy_install virtualenv
- 按以下链接中 “Build the Appium Python Test Package” 标签下的步骤进行构建环境
https://docs.aws.amazon.com/devicefarm/latest/developerguide/test-types-android-appium-python.html- 注意:由于virtualenv,此文件夹与本地python环境隔离,在以上网页 <b>step4</b> "Make sure you have py.test installed in your virtual environment and test cases are discoverable by the following command, which you should run from your virtual environment workspace folder." <b>之前</b>,将编写好的脚本文件拷贝至tests文件夹,并在此环境中安装你需要的第三方包及Appium-Client。例如:
pip install Appium_Python_Client
- 注意:由于virtualenv,此文件夹与本地python环境隔离,在以上网页 <b>step4</b> "Make sure you have py.test installed in your virtual environment and test cases are discoverable by the following command, which you should run from your virtual environment workspace folder." <b>之前</b>,将编写好的脚本文件拷贝至tests文件夹,并在此环境中安装你需要的第三方包及Appium-Client。例如:
- 按照以上网页的步骤得到的 .zip 文件即为符合要求的AWS测试脚本集文件。
进一步的测试范例,可参考
https://github.com/appium/sample-code/tree/master/sample-code/examples
常用的一些api
查找当前layout控件元素
1. driver.find_element_by_id()
2. driver.find_element_by_name() (iOS可用)
3. driver.find_element_by_accessibility_id()
4. driver.find_element_by_classname()
5. driver.find_element_by_xpath()