第一个Appium脚本

测试环境

  • Win 10 64bit
  • Python 3.5
  • Appium 1.7.2
  • Andriod 5.1.1 模拟器& Android 5.1 MX4
  • 测试App:考研帮Android版 3.1.0

测试场景

自动安装考研帮App(kaoyan3.1.0.apk),然后启动App

测试步骤

  • 获取待测试app的packageName和Activity
  • 配置Capability
  • 连接设备
  • 编辑脚本并运行
  • 查看结果

运行前检查事项

  1. 检查设备是否连接
  2. 检查Appium server是否启动
  3. 检查Capability配置信息是否正确

测试脚本

test_kyb.py

from appium import webdriver

 

desired_caps={}

desired_caps['platformName']='Android'

 

#模拟器设备

desired_caps['platformVersion']='5.1.1'

desired_caps['deviceName']='127.0.0.1:62025'

 

#mx4真机

# desired_caps['platformVersion']='5.1'

# desired_caps['deviceName']='MX4'

# desired_caps['udid']='750BBKL22GDN'

 

desired_caps['app']=r'C:UsersShuqingDesktopkaoyan3.1.0.apk'

 

desired_caps['appPackage']='com.tal.kaoyan'

desired_caps['appActivity']='com.tal.kaoyan.ui.activity.SplashActivity'

 

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

 

pycharm常用快捷键

  • 复制粘贴当前行 Ctrl+D
  • 注释 Ctrl+/
  • 运行当前脚本 Ctrl+shift+F10
  • 折叠展开代码 Ctrl + Numpad + /-
  • 方法定义跳转 Ctrl+B

1.首次启动Appium会在设备上安装2个守护app,Appium Settings和Unlock 部分设备系统由于权限的问题(如:三星S6 edge+)需要用户手动确认安装,否则不安装守护App会导致脚本运行失败,安装好后不要随意卸载这两个App。

  • Unlock :用于解锁手机弹窗提示
  • Appium Setting:Appium守护app
  1. from appium import webdriver 中的webdriber模块和selenium中的webdriver模块不一样!

webdriver模块源码路径:

{python安装路径}}Libsite-packagesappiumwebdriver

Appium报错&解决方案——那些年我们踩过的坑

Appium服务未启动

urllib.error.URLError: <urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。

【解决方案】点击启动Appium按钮后,出现[Appium] Welcome to Appium v1.7.2提示后再运行脚本

会话冲突

error: Failed to start an Appium session, err was: Error: Requested a new session but one was in progress

【报错分析】 之前的会话没有关闭,然后你又运行了测试实例,也没有设置覆盖.

【解决方案】 重新停止appium服务,开启Appium服务 在AdVance界面勾选Allow Session Override选项 ,重启Appium 测试结束在AfterClass加driver.quit()

未安装java环境

selenium.common.exceptions.WebDriverException: Message: A new session could not be created. (Original error: 'java -version' failed. Error: Command failed: C:WINDOWSsystem32cmd.exe /s /c "java -version"

设备未连接

selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: Could not find a connected Android device.

【解决方案】由于设备未连接,或者连接后未开启USB Debug。需要重新连接设备即可。

更换手机设备后如下对应的属性要记得更新,否则无法正常运行脚本。

desired_caps['platformVersion'] = 'XXX'

desired_caps['deviceName'] = 'Galaxy S6 edge+'

launchable activity 值写错

Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity

【解决方案】

  • launchable activity 写错更正即可。
  • 如果是存在此activity,则一定是AndroidMainfest.xml.xml中,当前activity设置的属性exported=false,表示当前activity无法被外部程序唤醒。(appium无法唤醒此类)需要研发人员去修改参数。
系统权限问题

Failure [INSTALL_FAILED_USER_RESTRICTED])

【解决方案】

  1. USB安装管理权限限制,关闭即可。
  2. 开启安装允许未知来源app选项
服务异常

An unknown server-side error occurred while processing the command” while opening the App

【解决方案】重新启动Appium服务

原文地址:https://www.cnblogs.com/lp475177107/p/12155337.html