web自动化针对PO模式进行二次封装之BasePage

import time
from datetime import datetime
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains
from ******.scripts.handle_logger import case_logger
from ******.scripts.constants import OutPuts_DIR
from ******.Common.file_upload import upload


class BasePage:
    '''
    BasePage类,针对PageObjects类的二次封装
    '''

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

    def wait_element_to_be_visible(self, loc, img_doc, timeout=20, frequency=0.5):
        '''
        等待元素可见
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :param timeout: 等待的超时时间
        :param frequency: 轮询频率
        :return:
        '''
        try:
            case_logger.info("开始等待页面元素<{}>是否可见!".format(loc))
            start_time = time.time()
            WebDriverWait(self.driver, timeout, frequency).until(EC.visibility_of_element_located(loc))
        except Exception as e:
            case_logger.error("页面元素<{}>等待可见失败!".format(loc))
            self.save_screenshot(img_doc)
            raise e
        else:
            end_time = time.time()
            case_logger.info("页面元素<{}>等待可见,等待时间:{}秒".format(loc, round(end_time - start_time, 1)))

    def wait_element_to_be_click(self, loc, img_doc, timeout=20, frequency=0.5):
        '''
        等待元素可点击
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :param timeout: 等待的超时时间
        :param frequency: 轮询频率
        :return:
        '''
        try:
            case_logger.info("开始等待页面元素<{}>是否可点击!".format(loc))
            start_time = time.time()
            WebDriverWait(self.driver, timeout, frequency).until(EC.element_to_be_clickable(loc))
        except Exception as e:
            case_logger.error("页面元素<{}>等待可点击失败!".format(loc))
            self.save_screenshot(img_doc)
            raise e
        else:
            end_time = time.time()
            case_logger.info("页面元素<{}>等待可点击,等待时间:{}秒".format(loc, round(end_time - start_time, 1)))

    def wait_element_to_be_exist(self, loc, img_doc, timeout=20, frequency=0.5):
        '''
        等待元素存在
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :param timeout: 等待的超时时间
        :param frequency: 轮询频率
        :return:
        '''
        try:
            case_logger.info("开始等待页面元素<{}>是否存在!".format(loc))
            start_time = time.time()
            WebDriverWait(self.driver, timeout, frequency).until(EC.presence_of_all_elements_located(loc))
        except Exception as e:
            case_logger.error("页面元素<{}>等待存在失败!".format(loc))
            self.save_screenshot(img_doc)
            raise e
        else:
            end_time = time.time()
            case_logger.info("页面元素<{}>等待存在,等待时间:{}秒".format(loc, round(end_time - start_time, 1)))

    def save_screenshot(self, img_doc):
        '''
        页面截屏保存截图
        :param img_doc: 截图说明
        :return:
        '''
        file_name = OutPuts_DIR + "\\{}_{}.png".format(datetime.strftime(datetime.now(), "%Y%m%d%H%M%S"), img_doc)
        self.driver.save_screenshot(file_name)
        case_logger.info("页面截图文件保存在:{}".format(file_name))

    def get_element(self, loc, img_doc):
        '''
        获取页面中的元素
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :return: WebElement对象
        '''
        case_logger.info("在{}中查找元素<{}>".format(img_doc, loc))
        try:
            ele = self.driver.find_element(*loc)
        except Exception as e:
            case_logger.error("在{}中查找元素<{}>失败!".format(img_doc, loc))
            self.save_screenshot(img_doc)
            raise e
        else:
            return ele

    def input_text(self, text, loc, img_doc, timeout=20, frequency=0.5):
        '''
        对输入框输入文本内容
        :param text: 输入的文本内容
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :param timeout: 等待的超时时间
        :param frequency: 轮询频率
        :return:
        '''
        try:
            case_logger.info("在{}中输入元素<{}>的内容为{}".format(img_doc, loc, text))
            self.wait_element_to_be_visible(loc, img_doc, timeout, frequency)
            self.get_element(loc, img_doc).send_keys(text)
        except Exception as e:
            case_logger.error("在元素<{}>中输入内容{}失败!".format(loc, text))
            self.save_screenshot(img_doc)
            raise e

    def clear_text(self, loc, img_doc, timeout=20, frequency=0.5):
        '''
        清除文本框的内容
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :param timeout: 等待的超时时间
        :param frequency: 轮询频率
        :return:
        '''
        try:
            case_logger.info("在{}中清除元素<{}>的文本内容".format(img_doc, loc))
            self.wait_element_to_be_click(loc, img_doc, timeout, frequency)
            self.get_element(loc, img_doc).clear()
        except Exception as e:
            case_logger.error("在{}中清除元素<{}>的文本内容失败!".format(img_doc, loc))
            self.save_screenshot(img_doc)
            raise e

    def click_button(self, loc, img_doc, timeout=20, frequency=0.5):
        '''
        点击按钮
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :param timeout: 等待的超时时间
        :param frequency: 轮询频率
        :return:
        '''
        try:
            case_logger.info("在{}中点击元素<{}>".format(img_doc, loc))
            self.wait_element_to_be_click(loc, img_doc, timeout, frequency)
            self.get_element(loc, img_doc).click()
        except Exception as e:
            case_logger.error("在{}中点击元素<{}>失败!".format(img_doc, loc))
            self.save_screenshot(img_doc)
            raise e

    def get_element_text(self, loc, img_doc, timeout=20, frequency=0.5):
        '''
        获取WebElement对象的文本值
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :param timeout: 等待的超时时间
        :param frequency: 轮询频率
        :return: WebElement对象的文本值
        '''
        try:
            case_logger.info("在{}中获取元素<{}>的文本值".format(img_doc, loc))
            self.wait_element_to_be_click(loc, img_doc, timeout, frequency)
            text = self.get_element(loc, img_doc).text
        except Exception as e:
            case_logger.error("在{}中获取元素<{}>的文本值失败!".format(img_doc, loc))
            self.save_screenshot(img_doc)
            raise e
        else:
            case_logger.info("获取到的元素文本值为:{}".format(text))
            return text

    def get_element_attr(self, attr_name, loc, img_doc, timeout=20, frequency=0.5):
        '''
        获取WebElement对象的属性值
        :param attr_name: 属性名称
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :param timeout: 等待的超时时间
        :param frequency: 轮询频率
        :return: WebElement对象的属性值
        '''
        try:
            case_logger.info("在{}中获取元素<{}>的属性{}的值".format(img_doc, loc, attr_name))
            self.wait_element_to_be_exist(loc, img_doc, timeout, frequency)
            value = self.get_element(loc, img_doc).get_attribute(attr_name)
        except Exception as e:
            case_logger.error("在{}中获取元素<{}>的属性{}的值失败!".format(img_doc, loc, attr_name))
            self.save_screenshot(img_doc)
            raise e
        else:
            case_logger.info("获取到的元素属性{}的值为{}".format(attr_name, value))
            return value

    def switch_to_frame(self, loc, img_doc, timeout=20, frequency=0.5):
        '''
        切换iframe页面
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :param timeout: 等待的超时时间
        :param frequency: 轮询频率
        :return:
        '''
        try:
            case_logger.info("在{}中根据元素<{}>进行iframe切换".format(img_doc, loc))
            start_time = time.time()
            WebDriverWait(self.driver, timeout, frequency).until(EC.frame_to_be_available_and_switch_to_it(loc))
        except Exception as e:
            case_logger.error("在{}中根据元素<{}>进行iframe切换失败!".format(img_doc, loc))
            self.save_screenshot(img_doc)
            raise e
        else:
            end_time = time.time()
            case_logger.info("在{}中根据元素<{}>进行iframe切换,等待时间:{}秒".
                             format(img_doc, loc, round(end_time - start_time, 1)))

    def switch_to_default_content(self, img_doc):
        '''
        切换iframe到main页面
        :param img_doc: 截图说明
        :return:
        '''
        try:
            case_logger.info("切换iframe到main页面")
            self.driver.switch_to.default_content()
        except Exception as e:
            case_logger.error("切换iframe到main页面失败!")
            self.save_screenshot(img_doc)
            raise e

    def upload_file(self, filename, img_doc, browser_type="chrome"):
        '''
        非input标签的文件上传
        :param filename: 文件名(绝对路径)
        :param img_doc: 截图说明
        :param browser_type: 浏览器类型
        :return:
        '''
        try:
            case_logger.info("上传文件({})".format(filename))
            upload(filePath=filename, browser_type=browser_type)
        except Exception as e:
            case_logger.error("上传文件({})失败!".format(filename))
            self.save_screenshot(img_doc)
            raise e

    def suspend_mouse(self, loc, img_doc, timeout=20, frequency=0.5):
        '''
        鼠标悬浮
        :param loc: 元素定位的XPATH元组表达式
        :param img_doc: 截图说明
        :param timeout: 等待的超时时间
        :param frequency: 轮询频率
        :return:
        '''
        try:
            case_logger.info("在{}上根据元素<{}>进行悬浮".format(img_doc, loc))
            self.wait_element_to_be_click(loc, img_doc, timeout, frequency)
            ele = self.get_element(loc, img_doc)
            ActionChains(self.driver).move_to_element(ele).perform()
        except Exception as e:
            case_logger.error("在{}上根据元素<{}>进行悬浮失败!".format(img_doc, loc))
            self.save_screenshot(img_doc)
            raise e

窗口切换、alert弹框关闭、select选择等还未进行二次封装,未完待续......

转载于:https://www.cnblogs.com/xiaogongjin/p/11608762.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程介绍你是否在寻找机会进入自动化测试领域? 你是否渴望学习selenium webdriver + Java以及最新的框架和技术进行web自动化测试? 你是否感兴趣学习Selenium如何用在你现有的项目里的? 这门课带你从Selenium搭建环境开始讲起,然后学习selenium,TestNG, logback, maven, jenkins。 我们假设学员没有任何自动化经验,来设计的这套课程。每个课题都从最基础的开始讲起。Selenium相关的该覆盖的课题都覆盖了。 例子都是来自于真实的web应用项目,帮助你理解不同的组件怎么用上自动化,这将展示给你一个行业层面的框架,增加自信心。 全网没有其他课程像这门课涵盖到如此之深的细节。 您将会学到什么 学完课程以后,你将拥有完整的Selenium Webdriver知识 你将具备从头开始设计Page Object、Page Factory、DATADRIVEN等搭建自动化框架的能力 用100多个实例对Selenium现实场景应用进行深入理解 全面了解TestNG, Maven, Jenkins, HTML报告,多浏览器并行测试 了解数据库测试和使用Selenium进行性能测试 你将彻底了解testNG框架 你从网上随便选择一个网站,都可以实现自动化,用所有可能的测试用例进行自动化测试 将提高你的编码技能,以编写最优化的自动化测试用例代码 你基本可以搞定任何Selenium面试,并能从设计阶段开始领导整个Selenium自动化项目 你应该能够使用应用程序的GUI来验证数据完整性 你将能够创建漂亮的报告来打动客户或领导 更深入地理解自动化指南和代码质量标准 会附带一个练习网站,可以用上所有可用的WebDriver功能,实现自动化 【适合人群】 软件手动测试人员想转为自动化测试的人员 自动化软件测试人员想加强专业技能的 刚毕业学生想从事软件行业 QA 组长或项目经理 【课程优势】 学完课程以后,你将拥有完整的Selenium Webdriver知识 【讲师介绍】 资质介绍: 12年以上软件测试工作经验,其中7年以上自动化测试开发经验 新书“Python3+Selenium3自动化测试项目实战”作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值