之前一篇博客已经讲述怎样手动使用appium-desktop启动测试机上的app,但我们实际跑自动化脚本的过程中,是需要用脚本调用appium启动app的,接下来就尝试写Python脚本启动app并登陆app。环境为Windows10 + Python3.7 + appium1.18.0 + Android手机 + 今日头条app + Pycharm

一,连接测试手机,获取测试机及被测APP配置

具体的获取方法请参考APP自动化测试之appium连接真机启动app,配置信息如下:

{"platformName": "Android","platformVersion": "10","deviceName": "PCT_AL10","appPackage": "com.ss.android.article.news","appActivity": ".activity.MainActivity"}

二,编写Python脚本启动app

1,编辑器推荐大家使用PyCharm,下载pycharm社区版本,免费无需破解。

2,因为登陆需要输入账号、密码,所以这里新增了两个参数。参数unicodeKeyboard即是否启用Unicode格式输入字符串,默认值为False,设置为True则表示启用。参数resetKeyboard即使用unicodeKeyboard功能进行Unicode输入后,是否将键盘重置为原始状态,默认False。

desired_caps = {"platformName": "Android","platformVersion": "10","deviceName": "PCT_AL10","appPackage": "com.ss.android.article.news","appActivity": ".activity.MainActivity","unicodeKeyboard": True,"resetKeyboard": True,}

3,传入desired_caps通过appium启动app,构造driver对象(即创建一个session)。

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

appium在本机开启的话则IP为127.0.0.1,端口默认为4723

4,电脑连接手机,开启appium服务,运行脚本,调试是否能启动app。

from appium import webdriverdesired_caps = {"platformName": "Android","platformVersion": "10","deviceName": "PCT_AL10","appPackage": "com.ss.android.article.news","appActivity": ".activity.MainActivity","unicodeKeyboard": True,"resetKeyboard": True,}# 启动appdriver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

我们会看到手机上今日头条app被打开。

三,通过appium Inspector定位元素,登陆APP

1,登陆操作步骤为:

启动今日头条app --> 点击【我知道了】--> 确定管理权限 --> 点击底部tab“未登陆” 进入未登录页面 --> 点击 “登陆” --> 点击“...” --> 选择密码登陆 --> 输入账号密码 --> 点击登陆

2,根据操作步骤,使用appium的Inspector获取登陆相关操作元素的属性

[进入appium Inspector页面](#),先点击选择元素按钮,然后在窗口左侧展示的手机页面上,点击选择需要定位的元素,窗口右侧会展示该元素的属性值。手机app页面切换后,点击窗口刷新页面按钮,左侧页面会刷新。

appium Inspector获取不到的元素属性的话,还可以用Android SDK里自带的工具uiautomatorviewer获取。后面会专门介绍怎样获取元素属性,这里不详述。

3,登陆操作脚本

# -*- coding:utf-8 -*-import timefrom appium import webdriverdesired_caps = {"platformName": "Android","platformVersion": "10","deviceName": "PCT_AL10","appPackage": "com.ss.android.article.news","appActivity": ".activity.MainActivity","unicodeKeyboard": True,"resetKeyboard": True,}# 启动appdriver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)# 登陆操作driver.find_element_by_id("com.ss.android.article.news:id/chj").click() # 点击【我知道了】time.sleep(1)driver.find_element_by_id("android:id/button1").click() # 点击权限管理-确定按钮time.sleep(1)driver.find_element_by_xpath("//android.widget.TabWidget/android.widget.RelativeLayout[@index=3]").click() # 点击未登录time.sleep(1)driver.find_element_by_id("com.ss.android.article.news:id/a1c").click() # 未登录页点击登录按钮time.sleep(1)driver.find_element_by_id("com.ss.android.article.news:id/bfm").click() # 登录页点击“。。。”time.sleep(1)driver.find_element_by_xpath("//android.widget.LinearLayout[@index=4]").click() # 选择密码登录time.sleep(1)driver.find_element_by_id("com.ss.android.article.news:id/c7").send_keys("********") # 输入账号time.sleep(1)driver.find_element_by_id("com.ss.android.article.news:id/ch").send_keys("********") # 输入密码time.sleep(1)driver.find_element_by_id("com.ss.android.article.news:id/a31").click() # 点击登录time.sleep(5)

我们查看手机,会发现手机正在自动做登录今日头条的app操作,且登录成功。

至此,我们通过编写一个简单的Python脚本完成了登录app的操作,如果加上断言,就是一条完整的用例。