前提是配置好了adb环境变量(安卓),安装了python
1. 安装appium server
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 importwebdriverfrom time importsleep
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
importunittestfrom appium importwebdriverfrom selenium.webdriver.support.ui importWebDriverWaitfrom selenium.webdriver.support importexpected_conditions as ECfrom selenium.webdriver.common.by importBy
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'
classLearnAppiumTest(unittest.TestCase):defsetUp(self):
self.driver=webdriver.Remote(appium_server, desired_caps)deftearDown(self):
self.driver.quit()deftest_01(self):
text_view= self.driver.find_element_by_id("text_view")assert text_view.text == 'Hello World! Hello World!' #测试应该不通过
deftest_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()