1. 程式人生 > 其它 >web自動化10-bagepage封裝

web自動化10-bagepage封裝

1、通⽤的⻚⾯操作封裝到 basepage

好處: 任何的web項⽬當中,都可以直接使⽤ basepage 中的⽅法。

2、常用的basepage封裝

  • 等待
  • 雙擊, 懸停, 拖動, 右擊
  • 回車, copy
  • iframe, window, alert
  • 下拉框操作, 多選, 單選
  • 檔案上傳
  • 修改元素屬性(js)

 

3、driver傳遞

class BasePage:
"""通用的頁面操作類,任何web網頁都可以使用"""

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

def wait_page_loaded(self, url, timeout=10):
"""等待某個頁面載入""" wait = WebDriverWait(self.driver, timeout=timeout) wait.until(expected_conditions.url_contains(url))

4、顯性等待可點選,可見

    def wait_element_clickable(self, locator, timeout=10):
        """等待某個元素可以被點選"""
        wait = WebDriverWait(self.driver, timeout=timeout)
        el = wait.until(expected_conditions.element_to_be_clickable(locator))
        
return el def wait_element_visible(self, locator, timeout=10): """等待元素可見""" wait = WebDriverWait(self.driver, timeout=timeout) el = wait.until(expected_conditions.visibility_of_element_located(locator)) return el

5、點選、輸入、雙擊, 懸停, 拖動, 右擊

   def click(self, locator, timeout=10):
        
"""點選. locator = ('xpath', '//*...') locator = ('id', 'kw') 找到這個元素, 判斷元素是否可以被點選,如果可以被點選,再執行 el.click() """ el = self.wait_element_clickable(locator, timeout=timeout) el.click() def send_keys(self, locator, words, timeout=10): """輸入""" el = self.wait_element_visible(locator, timeout=timeout) el.send_keys(words) def double_click(self, locator): """雙擊""" action = ActionChains(self.driver) el = self.wait_element_clickable(locator) action.double_click(el).perform() def move_to_element(self, locator): """懸停""" action = ActionChains(self.driver) el = self.wait_element_visible(locator, timeout=timeout) action.move_to_element(el).perform() def drag_and_drop(self, locator1, locator2): """拖動""" action = ActionChains(self.driver) el1 = self.wait_element_clickable(locator1) el2 = self.wait_element_clickable(locator2) action.drag_and_drop(el1, el2).perform() def context_click(self, locator): """懸停""" action = ActionChains(self.driver) el = self.wait_element_visible(locator, timeout=timeout) action.context_click(el).perform()

6、回車, copy

    def enter(self):
        """回車"""
        action = ActionChains(self.driver)
        action.send_keys(Keys.ENTER).perform()


    def union_two_keys(self,firstkey=Keys.CONTROL,secondkey):
        """組合鍵"""
        action = ActionChains(self.driver)
        action.key_down(firstkey).send_keys(secondkey).key_up(firstkey).perform()

7、iframe

    def switch_to_frame(self, locator):
        """切換 iframe"""
        el = self.wait_element_visible(locator)
        self.driver.switch_to.frame(el)

8、檔案上傳

    def send_file(self, locator, file_path):
        """傳送檔案"""
        el = self.wait_element_visible(locator)
        el.send_keys(file_path)

9、文字斷言和截圖

def allure_screenshot(self, name=None):
    """截圖"""

    f = self.driver.get_screenshot_as_png()
    return allure.attach(f, name=name, attachment_type=allure.attachment_type.PNG)


def assert_element_attribute_equal(self, locator, attr_name, expected):
    """斷言元素的text文字等於"""
    el = self.wait_element_visible(locator)
    actual = el.get_attribute(attr_name)
    print("{}.文字是{},期待的文字是{}".format(locator, actual, expected))
    assert actual == expected