appium自动化测试环境搭建
appium介绍
Appium是一个开源的、跨平台的测试框架,可以用来测试Native App、混合应用、移动Web应用(H5应用)。
Appium坚持的测试理念:
无需用户对APP进行任何修改或者重新编译,App应该Born to be automated;
不应该限制用户只能使用特定的语言或者框架来编写和执行测试;
移动测试框架的API应该是稳定的,合理的,不应对自动化API反复的造轮子;
移动测试框架应该是开源的,包括“精神和肉体”;
目前,Appium支持Python、JavaScript、Objective C、Java、Ruby、php、c#。WeTest目前首先选择支持Python语言和Java语言。其他语言将会陆续在WeTest上得到支持。您也可以告诉我们您希望下一个被支持的语言,Appium的架构是如何设计的?事实上,Appium 真正的工作引擎全部是第三方自动化框架。这样,就不需在你的应用里植入其他任何代码,也就是说你测试使用的应用和最终发布的应用完全一致。使用以下的第三方框架如下:
IOS: 苹果的 UIAutomation
Android 4.2+: Google’s UiAutomator
Android 2.3+: Google’s Instrumentation
Appium把这些第三方框架封装成一套 WebDriver API,(协议格式参见 JSON Wire Protocol),使用这种CS架构,Appium就可以通过任何语言来封装API调用,从而提供不同语言的客户端给不同的人员,Appium 的核心是一个web服务器,它提供了一套REST的接口。它收到客户端的连接,监听到命令,接着在移动设备上执行这些命令,然后将执行结果放在HTTP响应中返还给客户端,事实上,这种客户端/服务端的架构给予了许多的可能性:比如我们可以使用任何实现了该客户端的语言来写我们的测试代码。
appium安装步骤
(1)安装npm(如果是国内建议使用Alibaba的cnpm)
mac: brew install npm
windows: 直接去官网下载安装文件 (https://nodejs.org/en/) ,下载nodejs安装包安装后就可以运行npm
安装完成后建议切换到cnpm,命令:npm install -g cnpm –registry=https://registry.npm.taobao.org
运行命令查看版本:cnpm -v
(2)安装appium-doctor检测安装环境
执行:cnpm install appium-doctor -g
运行: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 7.10.0
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.
info AppiumDoctor ✔ Carthage was found at: /usr/local/bin/carthage
info AppiumDoctor ✔ HOME is set to: /Users/linkxzhou
WARN AppiumDoctor ✖ ANDROID_HOME is NOT set!
WARN AppiumDoctor ✖ JAVA_HOME is NOT set!
WARN AppiumDoctor ✖ adb could not be found because ANDROID_HOME is NOT set!
WARN AppiumDoctor ✖ android could not be found because ANDROID_HOME is NOT set!
WARN AppiumDoctor ✖ emulator could not be found because ANDROID_HOME is NOT set!
WARN AppiumDoctor ✖ Bin directory for $JAVA_HOME is not set
info AppiumDoctor ### Diagnostic completed, 6 fixes needed. ###
info AppiumDoctor
info AppiumDoctor ### Manual Fixes Needed ###
info AppiumDoctor The configuration cannot be automatically fixed, please do the following first:
WARN AppiumDoctor - Manually configure ANDROID_HOME.
WARN AppiumDoctor - Manually configure JAVA_HOME.
WARN AppiumDoctor - Manually configure ANDROID_HOME and run appium-doctor again.
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
红色的叉表示对应的环境有问题,需要自己配置,比如ANDROID_HOME和JAVA_HOME异常,在系统目录中配置ANDROID_HOME和JAVA_HOME,其中本人的配置如下(mac中的配置,写到~/.profile中,然后执行source ~/.profile):
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/
export ANDROID_HOME=/Users/xxxx/Library/Android/sdk/
export PATH=${PATH}:${JAVA_HOME}bin
以上配置在windows需要修改,将在环境变量中增加或者修改JAVA_HOME,ANDROID_HOME,PATH这几个值。
注意:
a.在ios上如果Carthage上有红色的叉,那么就需要安装carthage,运行:brew install carthage
b.如果只是测试单个平台可以通过appium-doctor –ios或者appium-doctor –android判断环境是否正常
配置完成再次执行:
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 7.10.0
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.
info AppiumDoctor ✔ Carthage was found at: /usr/local/bin/carthage
info AppiumDoctor ✔ HOME is set to: /Users/linkxzhou
info AppiumDoctor ✔ ANDROID_HOME is set to: /Users/linkxzhou/Library/Android/sdk/
info AppiumDoctor ✔ JAVA_HOME is set to: /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/
info AppiumDoctor ✔ adb exists at: /Users/linkxzhou/Library/Android/sdk/platform-tools/adb
info AppiumDoctor ✔ android exists at: /Users/linkxzhou/Library/Android/sdk/tools/android
info AppiumDoctor ✔ emulator exists at: /Users/linkxzhou/Library/Android/sdk/tools/emulator
info AppiumDoctor ✔ Bin directory of $JAVA_HOME is set
info AppiumDoctor ### Diagnostic completed, no fix needed. ###
info AppiumDoctor
info AppiumDoctor Everything looks good, bye!
info AppiumDoctor
(3)安装Appium和python环境
a.安装Appium
运行:cnpm install –g appium
执行完成以后就可以执行appium命令,appium -v(当然也可以去 https://github.com/appium/appium-desktop/releases/tag/v1.2.4 下载界面版本的appium安装,但是本文选择命令行版本)
b.安装python+webdriver环境(python的开发环境这里暂无)
由于本人的用例是用python写的,所以选择python环境,如果您对其他的比较熟悉,可以选择java或者ruby等语言;
运行:pip install selenium # 安装selenium
运行:pip install appium-Python-Client # 安装appium-Python-Client
c.执行appium
选择连接手机,先执行adb devices,记录对应的设备id,如下:
List of devices attached
A6KBYHMB99999999 device
运行:appium -a 127.0.0.1 -p 4723 -U A6KBYHMB99999999 –no-reset #表示启动4723端口,设备id是A6KBYHMB99999999
输入数据:
[Appium] Welcome to Appium v1.7.1
[Appium] Non-default server args:
[Appium] address: 127.0.0.1
[Appium] udid: A6KBYHMB99999999
[Appium] noReset: true
[Appium] Deprecated server args:
[Appium] -U,--udid => --default-capabilities '{"udid":"A6KBYHMB99999999"}'
[Appium] --no-reset => --default-capabilities '{"noReset":true}'
[Appium] Default capabilities, which will be added to each request unless overridden by desired capabilities:
[Appium] udid: 'A6KBYHMB99999999'
[Appium] noReset: true
[Appium] Appium REST http interface listener started on 127.0.0.1:4723
查看是否执行成功可以通过本地浏览器输入: http://127.0.0.1:4723/aaa ,获得输出: The URL ‘/aaa’ did not map to a valid resource
如果以上执行正常表示执行成功,那么下面我们就可以编辑测试用例开始自动化测试!
编辑测试用例(这里以android自动化作为样栗,其中appium本身是支持ios,android和web端)
以手头的demo为栗子,代码如下:
import os
import time
import unittest
from selenium import webdriver
from lib2to3.pgen2.driver import Driver
from lib2to3.tests.support import driver
PATH=lambda p:os.path.abspath(
os.path.join(os.path.dirname(__file__),p)
)
# 定义的测试驱动
global driver
class DownloadSDKAndroidTests(unittest.TestCase):
def setUp(self):
desired_caps={}
desired_caps['device'] = 'android'
desired_caps['platformName']='Android'
desired_caps['browserName']=''
desired_caps['version']='4.4.2'
#这是测试机的型号,可以查看手机的关于本机选项获得
desired_caps['deviceName']='MIUI-KXCCNBF22.0'
#被测试的App在电脑上的位置
desired_caps['app'] = PATH('/Volumes/CommonUser/mgtv/app-debug.apk')
#如果知道被测试对象的apppage,appActivity可以加上下面这两个参数,如果不知道,可以注释掉,不影响用例执行
#desired_caps['appPackage']='com.mgtv.downloader'
#desired_caps['appActivity']='.MainActivity'
self.driver=webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
def tearDown(self):
self.driver.quit()
#点击“开始下载”按钮
def test_startdownload(self):
print "test_startdownload ..."
time.sleep(2)
#button=self.driver.find_element_by_id("com.mgtv.downloader:id/button")
button=self.driver.find_element_by_name(u"开始下载")
button.click()
time.sleep(10)
#点击“暂停下载”按钮
def test_stopdownload(self):
print "test_stopdownload ..."
time.sleep(2)
button=self.driver.find_element_by_id("com.mgtv.downloader:id/button3")
button.click()
time.sleep(10)
#点击“恢复下载”按钮
def test_resumedownload(self):
print "test_resumedownload ..."
time.sleep(2)
button=self.driver.find_element_by_id("com.mgtv.downloader:id/button5")
button.click()
time.sleep(10)
#点击“删除下载”按钮
def test_deletedownload(self):
print "test_deletedownload ..."
time.sleep(2)
button=self.driver.find_element_by_id("com.mgtv.downloader:id/button2")
button.click()
time.sleep(10)
#点击“播放”按钮
def test_play(self):
print "test_play ..."
time.sleep(2)
button=self.driver.find_element_by_id("com.mgtv.downloader:id/button4")
button.click()
time.sleep(10)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(DownloadSDKAndroidTests)
unittest.TextTestRunner(verbosity=2).run(suite)
框架分析:
(1)setUp和tearDown两个必须存在,在setUp中设置’device’:‘android’,’platformName’:‘Android’,’version’:‘4.4.2’,’deviceName’:‘MIUI-KXCCNBF22.0’,’app’:PATH(‘/XXXX/app-debug.apk’) ,其中platformName表示设备类型,version表示android版本,deviceName表示机器名,app表示android包的本地路径;
(2)测试用例编写:定义test_*函数,然后就是获取各个控件,然后让python自动点击各个按钮,self.driver.find_element_by_id表示通过界面对应的控件id获取元素,然后触发button.click事件,就类似人为的点击按钮执行;
(3)执行脚本:python appiumTest1.py
test_deletedownload (__main__.DownloadSDKAndroidTests) ... test_deletedownload ...
ok
test_play (__main__.DownloadSDKAndroidTests) ... test_play ...
ok
test_resumedownload (__main__.DownloadSDKAndroidTests) ... test_resumedownload ...
ok
test_startdownload (__main__.DownloadSDKAndroidTests) ... test_startdownload ...
ERROR
test_stopdownload (__main__.DownloadSDKAndroidTests) ... test_stopdownload ...
ok
以上就是一个简单的自动化测试栗子,大家可以先熟悉并参考搭建环境,然后编辑属于自己业务逻辑的单元测试。