美文网首页
62. Page Object模式 - 打开、关闭、刷新浏览器

62. Page Object模式 - 打开、关闭、刷新浏览器

作者: 薛东弗斯 | 来源:发表于2024-03-31 07:22 被阅读0次

    在project > base > base_page.py中新增如下代码

        def get_url(self, url=''):
            """
            请求url
            :param url: 网址
            :return:
            """
            self.driver.get(url)
            logger.info(f"打开网址{url}")
    
        def close_driver(self):
            """
            关闭浏览器
            :return:
            """
            logger.info("关闭浏览器")
            self.driver.close()
    
        def quit_driver(self):
            """
            退出浏览器
            :return:
            """
            logger.info("退出浏览器")
            self.driver.quit()
    
        def refresh(self):
            """
            刷新浏览器
            :return:
            """
            self.driver.refresh()
            logger.info("刷新浏览器")
    

    在project > PO > test_baidu.py中新增如下函数

        def test_baidu4(self, driver):
            page = PageBaidu(driver)
            page.get_url("http://www.bilibili.com")
            page.refresh()
            page.close_driver()
            page.quit_driver()
    

    在project > PO > conftest.py中注释下面两行,否则 conftest里面driver quit,project > PO > test_baidu.py 中的page.close_driver()/page.quit_driver()无法继续操作

        # driver.close()
        # driver.quit()
    

    完整代码如下

    project > base > base_page.py
    project > utils > log_util.py
    project > utils > get_filepath.py
    project > utils > read.py
    project > PO > test_baidu.py
    project > PO > conftest.py
    project > page > page_baidu.py
    
    # project > base > base_page.py
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from utils.log_util import logger
    
    class BasePage:
        def __init__(self,driver):     # 此处的driver是形参,最终会调用conftest里面的driver
            self.driver = driver
            # self.driver.maximize_window()
            self.driver.implicitly_wait(10)
            self.wait = WebDriverWait(self.driver, 10)  # 显式等待
            self.driver.get("https://www.baidu.com/")
    # 为了让page也有find_element功能,此处进行定义
    # 这是基础的find_element封装,一般不用
    #     def find_element(self,locator):
    #         logger.info(f"定位元素{locator}")
    #         return self.driver.find_element(*locator)
    
        def find_element(self, locator, condition='visibility', retry=1):
            """
    
            :param locator: 元素定位信息
            :param condition: 默认是visibility
            :param retry: 重试次数,默认是1,重试一次
            :return:
            """
            for time in range(retry + 1):   # for循环第一个是0,retry+1 可以取到1
                try:
                    logger.info(f"定位元素{locator}")
                    if condition == 'visibility':
                        node = self.wait.until(EC.visibility_of_element_located(locator))
                    else:
                        node = self.wait.until(EC.presence_of_element_located(locator))
                    return node
                except Exception as e:
                    error_info = f"{locator}定位失败,错误信息{e}"
                    logger.error(error_info)
                    if time < retry:     # 第一次
                        logger.info(f"正在重新定位,当前重试次数:{time + 1}")
                    else:
                        raise Exception(error_info)
    
        def send_keys(self, locator, value, enter=False):
            """
            封装输入内容函数
            :param locator: 元素定位信息
            :param value: 输入项的内容
            :return:
            """
            # 1. 先定位元素
            node = self.find_element(locator)
            # 2. 清空输入框
            node.clear()
            # 3. 输入内容
            node.send_keys(value)
            logger.info(f"输入内容为:{value}")
            if enter:
                # 调用键盘的回车键
                node.send_keys(Keys.ENTER)
                logger.info("点击回车键")
    
        def click_button(self, locator):
            """
            定位元素并点击
            :param locator: 元素定位信息
            :return:
            """
            # 1. 先定位元素
            node = self.find_element(locator)
            # 2. 点击
            node.click()
            logger.info("点击按钮")
    
        def get_url(self, url=''):
            """
            请求url
            :param url: 网址
            :return:
            """
            self.driver.get(url)
            logger.info(f"打开网址{url}")
    
        def close_driver(self):
            """
            关闭浏览器
            :return:
            """
            logger.info("关闭浏览器")
            self.driver.close()
    
        def quit_driver(self):
            """
            退出浏览器
            :return:
            """
            logger.info("退出浏览器")
            self.driver.quit()
    
        def refresh(self):
            """
            刷新浏览器
            :return:
            """
            self.driver.refresh()
            logger.info("刷新浏览器")
    
    # project > utils > log_util.py
    import logging
    import os
    import time
    
    from utils.get_filepath import get_log_path
    
    log_path = get_log_path()
    
    if not os.path.exists(log_path):
        os.mkdir(log_path)
    
    
    class Logger:
    
        def __init__(self):
            # 定义日志位置和文件名
            self.logname = os.path.join(log_path, "{}.log".format(time.strftime("%Y-%m-%d")))
            # 定义一个日志容器
            self.logger = logging.getLogger("log")
            # 设置日志打印的级别,大于DEBUG都可以被打印出来
            self.logger.setLevel(logging.DEBUG)
            # 创建日志输入的格式
            self.formater = logging.Formatter(
                '[%(asctime)s][%(filename)s %(lineno)d][%(levelname)s]: %(message)s')
            # 创建日志处理器,用来存放日志文件
            self.filelogger = logging.FileHandler(self.logname, mode='a', encoding="UTF-8")
            # 文件存放日志级别
            self.filelogger.setLevel(logging.DEBUG)
            # 文件存放日志格式
            self.filelogger.setFormatter(self.formater)
            # 创建日志处理器,在控制台打印
            self.console = logging.StreamHandler()
            # 设置控制台打印日志界别
            self.console.setLevel(logging.DEBUG)
            # 控制台打印日志格式
            self.console.setFormatter(self.formater)
            # 将日志输出渠道添加到日志收集器中
            self.logger.addHandler(self.filelogger)
            self.logger.addHandler(self.console)
    
    
    logger = Logger().logger
    
    if __name__ == '__main__':
        logger.debug("我打印DEBUG日志")
        logger.info("我打印INFO日志")
        logger.warning("我打印WARNING日志")
        logger.error("我打印ERROR日志")
     
    
    # project > utils > get_filepath.py
    import os
    import time
    
    
    def get_report_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "allure-report/export",
                            'prometheusData.txt')
        return path
    
    
    def get_screen_shot_path():
        file_name = "截图{}.png".format(time.strftime("%Y-%m-%d_%H-%M-%S"))
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file", file_name)
        return path
    
    
    def get_logo_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file", "logo.jpg")
        return path
    
    
    def download_file_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "file")
        return path
    
    
    def get_yaml_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "data", "data.yaml")
        return path
    
    
    def get_ini_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "config", "settings.ini")
        return path
    
    
    def get_log_path():
        path = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "log")
        return path
    
    
    if __name__ == '__main__':
        print(get_report_path())
    
    
    # project > utils > read.py
    import configparser
    
    import yaml
    from utils.get_file_path import get_yaml_path, get_ini_path
    
    path = get_yaml_path()
    ini_path = get_ini_path()
    
    
    def read_yaml():
        with open(path, encoding="utf8") as f:
            data = yaml.safe_load(f)
            return data
    
    
    def read_ini():
        config = configparser.ConfigParser()
        config.read(ini_path, encoding='utf8')
        return config
    
    
    if __name__ == '__main__':
        print(read_yaml())
        # print(read_ini()['mysql']['HOST'])
    
    # project > PO > test_baidu.py
    from time import sleep
    from page.page_baidu import PageBaidu
    
    class TestBaidu:
        def test_baidu3(self, driver):
            page = PageBaidu(driver)     # driver实例化
            # # # driver.find_element(*page.input).send_keys("UI自动化")   # 之前只能用driver.find_element,希望通过page.find_element方法来做
            # page.find_element(page.input).send_keys("UI自动化")
            # # # driver.find_element(*page.button).click()   # 用page.find_element替代driver.find_element
            # page.find_element(page.button).click()
            # page.find_element(page.input).send_keys("UI自动化")   
            page.send_keys(page.input,"RDMA")   # 用page.send_keys,取代page.find_element
            # page.find_element(page.button).click()
            page.click_button(page.button)
            sleep(2)
    
        def test_baidu4(self, driver):
            page = PageBaidu(driver)
            page.get_url("http://www.bilibili.com")
            page.refresh()
            page.close_driver()
            page.quit_driver()
    
    # project > page > page_baidu.py
    from selenium.webdriver.common.by import By
    from base.base_page import BasePage
    from utils.log_util import logger
    
    
    class PageBaidu(BasePage):
        # 新闻
        news = (By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]')
        # 百度一下按钮
        button = (By.ID, 'su')
        # 百度输入框
        input = (By.ID, 'kw')
    
        def search_keyword(self, keyword):    # 把操作封装到方法里面去。
            logger.info("查找元素并输入内容")
            self.driver.find_element(*self.input).send_keys(keyword)
            logger.info("点击按钮")
            self.driver.find_element(*self.button).click()
    
    # project > PO > conftest.py
    import allure
    import pytest
    from selenium import webdriver
    
    
    @pytest.fixture(scope="session")
    def driver():
        driver = webdriver.Chrome()
        driver.maximize_window()
        print("打开浏览器")
        yield driver
        print("关闭浏览器")
        # driver.close()
        # driver.quit()
    
    
    @pytest.fixture()
    def fixture():
        print("我是前置步骤")
        yield "老白"
        print("我是后置步骤")
    
    

    相关文章

      网友评论

          本文标题:62. Page Object模式 - 打开、关闭、刷新浏览器

          本文链接:https://www.haomeiwen.com/subject/pvddtjtx.html