Appium 自动化测试 <二、Appium 与 pyth
2019-04-11 本文已影响12人
自律改变现状
基于上一篇文章,使用 python 实现 iOS 模拟器自动化测试。
下载 PyCharm 编译器。
网上有很多的 PyCharm 安装教程,这里就不记录了。
安装完成之后, 创建一个新的项目,
![](https://img.haomeiwen.com/i8320892/9980767ab0240a4b.png)
命名为 Appium 并创建。
![](https://img.haomeiwen.com/i8320892/e2dbdff308e48230.png)
进入主界面后到 preferences 设置参数、添加依赖库。
![](https://img.haomeiwen.com/i8320892/365cab1595504366.png)
- 点击 Python Integrated Tools ---> Testing ---> Default test runner ---> 选择 pytest, 如下图:
![](https://img.haomeiwen.com/i8320892/78430c011525b736.png)
-
设置 project interpreter python 版本, 如下:
-
在下面加号添加依赖库:
添加依赖库
![](https://img.haomeiwen.com/i8320892/da7b536326ddcb53.png)
安装成功,会有 installed successfully 提示。
![](https://img.haomeiwen.com/i8320892/052c8583d56febf9.png)
![](https://img.haomeiwen.com/i8320892/4baeeb881848bd89.png)
关闭 preferences ,回到主界面,创建一个py文件。
![](https://img.haomeiwen.com/i8320892/ceae990e54a9867f.png)
![](https://img.haomeiwen.com/i8320892/a621dc2df856e1a9.png)
写一个简单Demo
import time
from appium import webdriver
desired_caps = {
"platformName": "iOS",
"platformVersion": "10.0",
"deviceName": "iPhone 6",
"newCommandTimeout": "120",
"bundleId": "BB.AppiumDemo",
"noReset": True
}
def test():
# 获取设备
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
# 获取输入框
tf = driver.find_element_by_xpath("//XCUIElementTypeApplication[@name='AppiumDemo']/XCUIElementTypeWindow[1]/XCUIElementTypeOther/XCUIElementTypeTextField")
# 给输入框设置文本
tf.send_keys("马上点击button")
# 清除输入框文本
tf.clear()
tf.send_keys("3秒后关闭alert")
# 点击名为 Button 的按钮
driver.find_element_by_accessibility_id("Button").click()
# 设置睡眠时间
time.sleep(3)
driver.find_element_by_accessibility_id("确定").click()
tf.clear()
tf.send_keys("5秒后退出自动化测试")
time.sleep(5)
# 退出自动化测试
driver.quit()
if __name__=='__main__':
test()
运行代码之前先开启 Appium Desktop 程序,不然编译报错.
运行py文件。
![](https://img.haomeiwen.com/i8320892/b5aed71dec0c9d21.png)
成功编译后,会唤起模拟器包名为 "BB.AppiumDemo" 的程序,在这过程中会先安装一个叫做 WebDriver.. 驱动,它会闪几下(启动与闪退),这是正常现象,不用担心。
![](https://img.haomeiwen.com/i8320892/26a74244abf29de8.gif)
下篇我将总结 iOS 真机自动化测试与环境搭建。