1. 程式人生 > >Appium公共方法封裝

Appium公共方法封裝

沒有 onerror assertion cep link android 自動化測試 title css

最近在研究Appium+python寫Android自動化測試腳本,之前用過selenium+python寫web的自動化測試腳本,在此基礎上修改。

還是使用POM,一個page頁面對應一個test_case,base頁面封裝一些公共方法。封裝的一個查找元素的公共方法:

def find_element(self, *loc):
try:
WebDriverWait(self.driver,10,0.5).until(EC.visibility_of_element_located(loc))
return self.driver.find_element(*loc)
except AssertionError as e:
self.driver.close()

*loc表示這是一個元組對象。

之前使用selenium寫page頁面調用該方法查找元素,例如:

login_username_loc = (By.XPATH, ‘XXX‘)

el = self.find_element(*self.login_button_loc)

Appium的webdriver中新增了一些查找元素方法,我要通過accessibility_id查找元素,find_element_by_accessibility_id方法。但是selenium的selenium.webdriver.common.by中沒有accessibility_id,如下所示,只有:ID、XPATH、LINK_TEXT、PARTIAL_LINK_TEXT、NAME、TAG_NAME、CLASS_NAME、CSS_SELECTOR。

class By(object):
"""
Set of supported locator strategies.
"""

ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"

查找appium中的webdriver發現,新增的appium.webdriver.common.mobileby有個MobileBy對象,這是對by對象的一個擴展。

所以可以通過MobileBy來定位對象:

login_button_loc = (MobileBy.ACCESSIBILITY_ID, ‘登錄‘)

el = self.find_element(*self.login_button_loc)

這樣就避免了重新封裝公共方法,還可以通過之前的公共方法來定位元素。

Appium公共方法封裝