前提是配置好了adb环境变量(安卓),安装了python
1. 安装appium server
下载地址 : http://appium.io/
2. 安装appium client和selenium
在cmd中输入 pip install selenium
pip install Appium-Python-Client
如果出现retrying问题, 使用带pip源的命令,如
pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple/
pip install Appium-Python-Client -i https://pypi.mirrors.ustc.edu.cn/simple/
3. 编写脚本, 代码中需要包含对appium server的设置, 可以根据实际需要增/删设置项, 如
# -*- coding: utf-8 -*-
from appium import webdriver
from time import sleep
CAPS = {
"deviceName": " MEIZU_E3",
"platformName": "Android",
"platformVersion": "7.1.1",
#'app' = 'E:/autotestingPro/app/UCliulanqi_701.apk' #指向.apk文件,如果设置appPackage和appActivity,那么这项会被忽略
"appPackage": " com.meizu.flyme.flymebbs",
"appActivity": ".ui.LoadingActivity",
#"noReset": True,
}
driver = webdriver.Remote('http://localhost:4723/wd/hub', CAPS)
sleep(3)
4. 打开appium server, 设置主机为 127.0.0.1,设置端口为 4723, 启动server
5. 连接手机,安装应用,运行脚本。完整测试脚本如下例(用Unittest):
# coding: utf-8
import unittest
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
desired_caps = {'platformName': 'Android',
'platformVersion': '5.1.1',
'deviceName': 'MEIZU_E3', #设备名来自adb devices
"appPackage": " com.meizu.flyme.flymebbs",
"appActivity": ".ui.LoadingActivity",}
appium_server = 'http://localhost:4723/wd/hub'
class LearnAppiumTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Remote(appium_server, desired_caps)
def tearDown(self):
self.driver.quit()
def test_01(self):
text_view = self.driver.find_element_by_id("text_view")
assert text_view.text == 'Hello World! Hello World!' # 测试应该不通过
def test_02(self):
wait = WebDriverWait(self.driver, 6)
wait.until(EC.element_to_be_clickable((By.ID, 'button')))
button = self.driver.find_element_by_id("button")
button.click()
wait = WebDriverWait(self.driver, 6)
wait.until(EC.presence_of_element_located((By.ID, 'text_view')))
text_view = self.driver.find_element_by_id("text_view")
assert text_view.text == '3' # 测试应该通过
if __name__ == '__main__':
unittest.main()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。