自动化脚本常用的封装方法

1.点击元素:

    等待元素(会等待最多 10 秒(超时时间),每 0.5 秒检查一次,直到条件满足)出现在页面上并且可点击,然后执行点击操作。页面上的元素不一定会立即加载出来或者可点击,需要等待它们变为可操作状态才能进行后续操作,以避免因元素未加载或未准备好而导致的测试失败。

    def Click1(self, locate, locateType=By.XPATH):
        # 等待元素加载出来
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((locateType, locate)))
        WebDriverWait(self.driver, 10, 0.5).until(EC.element_to_be_clickable((locateType, locate)))
        # 点击
        self.driver.find_element(locateType, locate).click()

2.输入文本:

    等待最多 15 秒(超时时间),每 1 秒检查一次,直到页面中出现指定 XPath 的元素后返回找到的元素对象

    def inputText(self, locate, text, locateType=By.XPATH):
        # 等待元素加载出来
        WebDriverWait(self.driver, 8, 0.5).until(EC.presence_of_element_located((locateType, locate)))
        # 输入文本
        element = self.driver.find_element(locateType, locate)
        element.clear()  # 清空输入框
        element.send_keys(text)  # 输入文本

3.等待元素加载出来:

    等待最多 15 秒(超时时间),每 1 秒检查一次,直到页面中出现指定 XPath 的元素后返回找到的元素对象

    def find_xpath(self, locate):
        # 等待元素加载出来
        WebDriverWait(self.driver, 15, 1).until(EC.presence_of_element_located((By.XPATH, locate)))
        WebDriverWait(self.driver, 15, 1).until(
            EC.visibility_of(self.driver.find_element(By.XPATH, locate)))  # 判断元素是可见的
        # 返回元素
        return self.driver.find_element(By.XPATH, locate)

4.鼠标悬停到指定元素:

    def move_mouse(self, locate, locateType=By.XPATH):
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((locateType, locate)))
        DH = self.driver.find_element(locateType, locate)
        actions = ActionChains(self.driver)
        actions.move_to_element(DH).perform()

5.判断界面字段对应的元素是否存在,存在则打印:字段正常显示_校验通过!;不存在则打印:字段未显示_校验失败~:

    def search_assertion(self, element_xpath, expected_text):
        asserelement = self.driver.find_element(By.XPATH, element_xpath)
        actual_text = asserelement.text
        print(actual_text)
        if expected_text == actual_text:
            print('字段正常显示_校验通过!')
        else:
            print('字段未显示_校验失败~')
        time.sleep(2)

6.切ifram:

    def iframeInto2(self, locate, locateType=By.XPATH):
        # 等待加载出来
        frames = locate.split(";")
        for frame in frames:
            WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located((locateType, frame)))
            iframe = self.driver.find_element(locateType, frame)
            self.driver.switch_to.frame(iframe)

7.根据title切换窗口:

    def locateWindowTitle(self, title):
        try:
            time.sleep(5)
            all_h = self.driver.window_handles  # 获得所有窗口句柄
            # 遍历句柄,对比句柄的title
            for i in all_h:
                tl = self.driver.title
                if tl == title:
                    # self.driver.switch_to.window(i)
                    break
                else:
                    self.driver.switch_to.window(i)
        except:
            raise AssertionError("页面切换失败!")
        else:
            pass

8.切换页面: 

    def locateNewPage(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值