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

 

posted @ 2022-04-03 19:29  依羽杉  阅读(179)  评论(0)    收藏  举报