Appium + Python automated testing (b) - an example of a program run App

In a previous blog, we have built a good environment. Now, we use the built environment to run a test script, use a calculator script starts, and to achieve an addition operation.

Creating simulator

Before you run the App, you first need to create an Android emulator, you can use the real machine to run, the better.

Into the directory of the Android SDK, double-click to run AVD Manager.exe

 

 

Click Create to create an Android emulator

 

 

Enter the name of a simulator, select the device type and version of the API, click to create. Once created displays all current device, select the device, click Start to start

 

 

When you first start a bit slow, to wait a few minutes after the start just fine, just after the start of the simulator interface is so

 

 

Get packageName and activityName applications

Before writing test scripts, you know the apk be tested packageName and you want to start activityName, if a third-party apk, there are ready-made can be used directly. Here is the start simulator built-in application, it turned out should simulator apk

Application emulator are preset on the phone system / app directory, use the adb shell command to enter the directory name to view the calculator application apk

 

 

Use ls * .apk command to list all the apk, apk file to find Calculator.apk, then use adb pull system / app / Calculator.apk d: / command to export the file to your computer.

After apk export, Android SDK has a aapt tool can be used to view the application package name and the name of the activity to be started, tools in the SDK build-tools directory, the command format is "aapt dump badging apk path."

 

 

After the command, enter the first row contains the name of the application package

package: name=’com.android.calculator2’ versionCode=’17’ versionName=’4.2.2-3453820’

When the input end is coming, there is a line that contains the name of activity open when the application starts

 

 

launchable-activity: name=’com.android.calculator2.Calculator’ label=” icon=’

这样就得到了packageName和activityName

Capability

Capability是一个字典,配置的是自动化测试的一些必要信息,包含了本次测试的平台名称及版本号、启动的是浏览器还是app等,客户端将这些告诉服务器,服务器根据这些信息创建自动化会话。这里只介绍跟Android平台相关并且常用的几个

automationName:定义测试引擎,使用的android-sdk版本小于17时,使用Selendroid,大于等于17时使用Appium,默认是Appium

platformName:测试平台,通常用于移动设备,值有:Android、IOS、FirefoxOS

platformVersion:测试平台版本,根据设备的固件版本指定,例如Android的4.2、IOS的7.1

deviceName:设备名称

app:要安装的app的文件路径,可以是本地的绝对路径,也可以是远程网络路径

browserName:启动的浏览器名称,测试的是web应用时指定,Android平台设置为Chrome

newCommandTimeout:为了结束Appium会话,会设置一个等待从客户端发送命令的超时时间,默认为60秒,一般不需要设置

autoLaunch:测试时是否需要自动运行app

appPackage:设置app的包名,告诉Appium需要启动的app

appActivity:设置启动的Activity

appWaitActivity:要等待的Activity

appWaitPackage:要等待的appPackage

unicodeKeyboard:是否使用unicode键盘输入,在输入中文字符和unicode字符时设置为true

resetKeyboard:是否将键盘重置为初始状态,设置了unicodeKeyboard时,在测试完成后,设置为true,将键盘重置

上述的几个Capability只是在测试中经常用到的,更多的Capability在用到的时候可以在网上查阅

编写测试脚本

新建test.py,输入下面的代码

# -*- coding: utf-8 -*-

from appium import webdrivedesired_caps = {

'platformName': 'Android',
'deviceName': 'emulator-5554',
'platformVersion': '4.2',
'appPackage': 'com.android.calculator2',
'appActivity': 'com.android.calculator2.Calculator'
}
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',
desired_caps)
driver.find_element_by_name('7').click()
driver.find_element_by_name('+').click()
driver.find_element_by_name('8').click()
driver.find_element_by_name('=').click()

保存文件,打开Appium,点击右上角的三角形按钮启动Appium

 

 

出现> info: Welcome to Appium v1.4.16 (REV ae6877eff263066b26328d457bd285c0cc62430d)这行信息后,就表示Appium启动成功了

 

 

Appium启动成功后,运行测试脚本,模拟器会运行计算器应用,计算加法


————————————————
版权声明:本文为CSDN博主「zh175578809」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zh175578809/article/details/76862590

 

Guess you like

Origin www.cnblogs.com/kuaileya/p/11984084.html