iOS appium元素定位,封装时候使用MobileBy和By结合
iOS:
1 | from selenium.webdriver.common.by import By |
2 | class MobileBy(By): |
3 | IOS_PREDICATE = ‘-ios predicate string’ |
4 | IOS_UIAUTOMATION = ‘-ios uiautomation’ |
5 | IOS_CLASS_CHAIN = ‘-ios class chain’ |
6 | ANDROID_UIAUTOMATOR = ‘-android uiautomator’ |
7 | ANDROID_VIEWTAG = ‘-android viewtag’ |
8 | ANDROID_DATA_MATCHER = ‘-android datamatcher’ |
9 | ACCESSIBILITY_ID = ‘accessibility id’ |
10 | IMAGE = ‘-image’ |
11 | CUSTOM = ‘-custom’ |
android:
1 | class By(object): |
2 | “”” |
3 | Set of supported locator strategies. |
4 | “”” |
5 | |
6 | ID = “id” |
7 | XPATH = “xpath” |
8 | LINK_TEXT = “link text” |
9 | PARTIAL_LINK_TEXT = “partial link text” |
10 | NAME = “name” |
11 | TAG_NAME = “tag name” |
12 | CLASS_NAME = “class name” |
13 | CSS_SELECTOR = “css selector” |
综合封装:
1 | def find_element(self, *loc): |
2 | try: |
3 | # 元素可见时,返回查找到的元素;以下入参为元组的元素,需要加* |
4 | WebDriverWait(self.driver, 30).until(lambda driver: driver.find_element(*loc).is_displayed()) |
5 | return self.driver.find_element(*loc) |
6 | |
7 | except selenium.common.exceptions.NoSuchElementException: |
8 | # logging.warning(‘Can not find element: %s’ % loc[1]) |
9 | self.log.myloggger(‘Can not find element: %s’ % loc[1], flag=2) |
10 | raise |
11 | except selenium.common.exceptions.TimeoutException: |
12 | self.log.myloggger(‘Can not find element: %s’ % loc[1], flag=2) |
13 | raise |
14 | |
15 | def find_elements(self, *loc): |
16 | try: |
17 | WebDriverWait(self.driver, 30).until(lambda driver: driver.find_elements(*loc)) |
18 | return self.driver.find_elements(*loc) |
19 | |
20 | except selenium.common.exceptions.NoSuchElementException: |
21 | # logging.warning(‘Can not find element: %s’ % loc[1]) |
22 | self.log.myloggger(‘Can not find element: %s’ % loc[1], flag=2) |
23 | self.get_screenshot() |
24 | raise |