欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

appium+python android自动化测试

程序员文章站 2022-07-12 22:15:51
...

本文主要学习参考:https://www.cnblogs.com/fnng/p/4540731.html

很早之前就听别人说:人生苦短,何不python;刚好借此工作机会,鼓起勇气了解下python;接下来就说说我搭建appuim环境过程

首先我疑惑的三个问题是:

1、appium python IDE用哪一款

2、appium 一些依赖有哪些

3、appium 怎么和android真机连接

 

我想在解决这三个问题前,我想先感性了解下appium长啥样:

1、appium 是一个自动化测试开源工具,支持多语言开发(java、php、python等);

2、appium 为你服务流程是这样的(这点大体上解决了我的疑惑2):

appium+python android自动化测试

 

疑惑1:appium python IDE用哪一款

1、度娘告诉我,你得先安装python环境啊,还说你可以到官方网:https://www.python.org/ 下载

(注意:在安装的过程中需要勾选:“Add Python 3.x to PATH” , 如果没有勾选,需要在安装完成之后,将Python的安装目录(如:C:\Python36)添加到环境变量PATH下面)

2、还要安装Selenium(这个是什么鬼,自己问度娘吧,因为我也不知道啊),运行window cmd通过pip命令安装Selenium

pip install selenium

3、步骤一安装python环境,有自带一款IED叫IDLE,那就让我们来试下吧

from selenium import webdriver


driver = webdriver.Chrome()
driver.get('https://www.baidu.com')

print(driver.title)

driver.quit()

4、尴尬,执行报错了:

Traceback (most recent call last):
  File "C:\Users\ZhanXS\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
    stdin=PIPE)
  File "C:\Users\ZhanXS\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 756, in __init__
    restore_signals, start_new_session)
  File "C:\Users\ZhanXS\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1155, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\ZhanXS\Desktop\test\drv.py", line 4, in <module>
    driver = webdriver.Chrome()
  File "C:\Users\ZhanXS\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
    self.service.start()
  File "C:\Users\ZhanXS\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

大概是说没有 chromedriver ,度娘这时又说了,要安装Selenuim3浏览器驱动(我这边以chrome浏览器为例):

1)、Chrome浏览器驱动:https://sites.google.com/a/chromium.org/chromedriver/home

2)、设置浏览器驱动:把驱动解压放到某个目录,再把这个目录添加到Path环境变量中

3)、验证浏览器驱动是否正常使用

from selenium import webdriver

driver = webdriver.Chrome()    # Chrome浏览器

4)、具体更详细内容,请参考:http://www.testclass.net/selenium_python/selenium3-browser-driver/

疑惑2:appium 一些依赖有哪些

1、appium-server

appium-desktop 下载地址:https://github.com/appium/appium-desktop/releases

(根据自己的平台选择相关的包进行下载。本文以 Windows 为例,所以选择 appium-desktop-Setup-1.2.4.exe 文件进行下载)

2、appium-client

其实,python-client 的项目名称叫:Appium-Python-Client: https://github.com/appium/python-client

运行windows cmd命令安装: pip install Appium-Python-Client

3、运行第一个Appium测试

1)、一台能adb连接的android真机

2)、启动appium-server  ip:127.0.0.1   port:4724

3)、用python IDLE编写 appnium 测试脚本

#coding=utf-8
from appium import webdriver

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['deviceName'] = '1234567890123'   # 可通过adb devices,查看真机deviceName
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = '.Settings'

driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 连接android真机,启动android settings设置界面

# driver.find_element_by_name("=").click()

driver.quit() # 退出android settings设置界面

如果有弹出android settings设置界面,恭喜你,和我一样,环境你搭建成功了,如果没有,哈哈,你懂得:那就多折腾下吧。

(android SDK开发环境也是要安装的,这个appium-server连接真机时要用到)