一个完整的Appium手机自动化测试实例

实现过程:

1.使用环境

appium 、安卓SDK 、python

本文重点是自动化实例,环境搭建过程省略。

2.找到被测APP的包名和Activity Name

手机连接上电脑后,在DOS环境先使用adb devices命令确认手机与电脑连接正常。

然后 打开被测APP,输入下面的命令,就会显示出APP的包名与ActivityName

本人最常使用第3条命令

     adb shell dumpsys window w | findstr \/ | findstr name=

或 adb shell dumpsys window |findstr mCurrent

或 adb shell "dumpsys window w|grep \/|grep name=|sed 's/mSurface=Surface(name=//g'|sed 's/)//g'|sed 's/ //g'"

   

3.使用Python编写自动化脚本

启动Appium,启动pycharm,开始编写自动化脚本。写脚本之前要先导入selenium库,可以使用pip install selenium命令直接在DOS环境安装,也可以通过pycharm Project Interpreter安装。

from selenium import webdriver
import time

#初始化信息
desired_caps={}
desired_caps["platformName"]="Android"
desired_caps["platformVersion"]="6.0"
desired_caps["deviceName"]="HuaWeiP9"
desired_caps["appPackage"]="com.taobao.taobao"
desired_caps["appActivity"]="com.taobao.tao.homepage.MainActivity3"


driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",desired_caps)
#在搜索框输入关键词
driver.find_element_by_id("com.taobao.taobao:id/home_searchedit").click()
# 等待时间
time.sleep(3)
driver.find_element_by_id("com.taobao.taobao:id/searchEdit").send_keys("adidas")
time.sleep(3)
driver.find_element_by_id("com.taobao.taobao:id/searchbtn").click()
#截图
driver.quit()

 

 

其它:

1.通过UI Automator Viewer(安卓SDK自带的工具)可以查看APP页面元素属性,进而定位元素,实现各种操作

 2.运行下面这一行代码时报错:

driver.find_element_by_id("com.taobao.taobao:id/searchEdit").send_keys("adidas")

Message: Parameters were incorrect. We wanted {"required":["value"]} and you sent ["text","sessionId","id","value"]

原因:

selenium新版导致的问题,降级后解决:

最新的版本卸载,安装3.3.1这个版本

1. pip uninstall selenium
2. pip install selenium==3.3.1

posted on 2019-05-06 11:37  不断努力的小蜗牛  阅读(32481)  评论(0编辑  收藏  举报