Appium
1、基於python的測試框架
- 編程語言:python 客戶端
- IDE:pycharm
- 客戶端安裝包:appium_python_client
- 多版本隔離工具:venv
- 國內依賴源:http://pypi.douban.com/simple/
- 測試框架:pytest, unittest, nose 推薦 pytest
2、代碼等待
隱式等待:
1、定義:服務端在設置的時間內不斷查找元素,直到查找到元素或達到等待時間爲止,作用於全局
2、代碼:driver.implicitly_wait(10)
顯式等待:
1、定義:客戶端(即用例端)固定等待設置的時間,作用於特定代碼快
2、代碼:
from time import sleep
sleep(20)
**備註:**隱式等待和 顯式等待可以結合使用,全局使用隱式等待,在特定爲止使用顯式等待
3、 常用定位方式
- List item
- id
- accessbilityID
- XPath
-className 不常用
-image
-UIautomator2 安卓
-XCUITest IOS
4、常見定位方法
findElementByXXX()
findElementBy(By,value) #PO 設計模式使用
5、常用自動化API
- click
- sendkeys
- swipe
- TouchActions
TouchActions
1、定義:對元素執行tap操作
Perform a tap action on the element
2、使用方法:
from appium.webdriver.common.touch_action import TouchAction
// ...
actions = TouchAction(driver)
actions.tap_and_hold(20, 20)
actions.move_to(10, 100)
actions.release()
actions.perform()
3、鏈式操作:
舉例:執行長按-拖動-釋放長按-提交操作
TouchAction(driver).long_press().move_to().release().perform()
swipe
1、定義:從一個點到另一個點滑動,持續時間可選
Swipe from one point to another point, for an optional duration
2、方法:driver.swipe()
3、適用性:
針對分辨率不同的手機不適用,不能使用絕對位置,應該使用相對位置,後續如用兼容性測試應對swipe方法進行封裝
6、 pytest
命令行安裝pytest: pip install -U pytest
查看pytest 版本: pytest --version
This is pytest version 5.3.5。。。
基於pytest的腳本:
from appium import webdriver
class TestDemo:
def setup(self):
desired_caps = {}
desired_caps['platformName'] = 'Android' # 設備系統
desired_caps['platformVersion'] = '6.0.1' # 設備系統版本
desired_caps['deviceName'] = '127.0.0.1:62001' # 設備名稱
desired_caps['appPackage'] = '包名' # 測試app包名
desired_caps['appActivity'] = 'app啓動參數'
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 啓動app
self.driver.implicitly_wait(20) #設置隱式等待
def test_lanuch(self):
driver = self.driver
# 等待10秒
driver.implicitly_wait(10)
# #同意協議
el1 = driver.find_element_by_id("tv.danmaku.bili:id/agree")
el1.click()
# 定位遊戲手柄
el2 = driver.find_element_by_xpath(
"/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.support.v4.widget.DrawerLayout/android.widget.FrameLayout/android.view.ViewGroup/android.widget.LinearLayout/android.view.ViewGroup/android.support.v7.widget.LinearLayoutCompat/android.widget.FrameLayout[1]/android.widget.ImageView[2]")
el2.click()
def teardown(self):
self.driver.quit()
運行前啓動
啓動手機/模擬器
啓動APPium
直接在控制檯啓動APPium Server