前提是配置好了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()