Appium的BasePage封裝

class BasePage:
    # 定位廣告,彈窗的locator
    # 遊戲首頁的廣告:book_button_close
    # 首頁青少年模式的選擇-我知道了:tv.danmaku.bili:id/text3

    _black_list = [
        # 遊戲廣告頁,彈窗
        (By.ID, 'book_button_close'),
        # 首頁-青少年模式選擇
        (By.ID, 'tv.danmaku.bili:id/text3')

    ]

    '''封裝driver方法'''

    def __init__(self, driver: WebDriver):
        self.driver = driver

    '''封裝查找元素方法,並且返回頁面元素'''
    '''如果有彈窗之類的找不到元素,則關閉彈窗再查找元素'''

    def find_element(self, locator):
        try:
            self.driver.implicitly_wait(10)
            return self.driver.find_element(*locator)
        except:
            self.handle_exception()
            # self.find_element(locator)
            return self.driver.find_element(*locator)

    '''封裝點擊方法'''

    def find_element_and_click(self, locator):
        self.driver.find_element(*locator).click()

    '''彈窗廣告等異常處理方法,具體在find_element方法內進行調用'''

    def handle_exception(self):
        for locator in self._black_list:
            elements = self.driver.find_elements(*locator)
            if len(elements) >= 1:
                elements[0].click()
            else:
                print("%s not found" % str(locator))

            # self.driver.page_source獲取頁面所有元素
            # page_source = self.driver.page_source
            # if "image_cancel" in page_source:
            #     self.find_element(*locator).click()
            # elif "tip" in page_source:
            #     pass
            # 向上滑動

    def swipe_up(self, t=500, n=1):
        s = self.driver.get_window_size()
        x1 = s['width'] * 0.5  # x座標
        y1 = s['height'] * 0.75  # 起點y座標
        y2 = s['height'] * 0.25  # 終點y座標
        print('手機的尺寸是: ', s)
        for i in range(n):
            self.driver.swipe(x1, y1, x1, y2, t)


    # 向下滑動
    def swipe_down(self, t=500, n=1):
        s = self.driver.get_window_size()
        x1 = s['width'] * 0.5  # x座標
        y1 = s['height'] * 0.25  # 起點y座標
        y2 = s['height'] * 0.75  # 終點y座標
        for i in range(n):
            self.driver.swipe(x1, y1, x1, y2, t)

    # 向左滑動
    def swipe_left(self, t=500, n=1):
        s = self.driver.get_window_size()
        x1 = s['width'] * 0.75
        y1 = s['height'] * 0.5
        x2 = s['width'] * 0.25
        for i in range(n):
            self.driver.swipe(x1, y1, x2, y1, t)

    # 向右
    def swipe_right(self, t=500, n=1):
        s = self.driver.get_window_size()
        x1 = s['width'] * 0.25
        y1 = s['height'] * 0.5
        x2 = s['width'] * 0.75
        for i in range(n):
            self.driver.swipe(x1, y1, x2, y1, t)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章