美文网首页
68. Page Object模式 - Select操作 二次封

68. Page Object模式 - Select操作 二次封

作者: 薛东弗斯 | 来源:发表于2024-04-03 05:28 被阅读0次

    完整代码如下

    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 > test_sahi.py
    project > PO > conftest.py
    project > page > page_baidu.py
    project > page > page_sahi.py
    
    # project > PO > test_sahi.py
    from time import sleep
    import time
    from page.page_sahi import PageSahi
    
    class TestSahi:
        def test_sahi1(self, driver):
            page = PageSahi(driver)
            page.get_url('http://sahitest.com/demo/dragDropMooTools.htm')
            page.drag_and_drop(page.start, page.end)
            time.sleep(3)
    
        def test_sahi2(self, driver):
            page = PageSahi(driver)
            page.get_url('https://sahitest.com/demo/selectTest.htm')
            page.select_by_index(page.select, 0)
            sleep(3)
    
        def test_sahi3(self, driver):
            page = PageSahi(driver)
            page.get_url('https://sahitest.com/demo/selectTest.htm')
            page.select_by_value(page.select, "48")
            sleep(3)
            assert 1 == 2
    
    # project > base > base_page.py
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver import Keys, ActionChains
    from selenium.webdriver.support.select import Select
    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/")
            self.actions = ActionChains(self.driver)  # 鼠标动作链初始化
    # 为了让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("刷新浏览器")
    
        def switch_to_window(self, to_parent_window=False):
            """
            切换窗口
            :param to_parent_window: 是否回到主窗口
            :return:
            """
            total = self.driver.window_handles
            if to_parent_window:
                # 切换到主窗口
                self.driver.switch_to.window(total[0])
            else:
                # 获取当前窗口
                current_window = self.driver.current_window_handle
                for window in total:
                    if window != current_window:
                        logger.info("切换窗口")
                        self.driver.switch_to.window(window)
    
        def get_title(self):
            """
            获取网页title
            :return:
            """
            return self.driver.title
    
        def get_current_url(self):
            """
            获取当前的URL
            :return:
            """
            return self.driver.current_url
    
        def get_page_source(self):
            """
            获取网页源代码
            :return:
            """
            return self.driver.page_source
    
        def get_text(self, locator):
            """
            获取元素的文本内容
            :param locator: 元素定位信息
            :return:
            """
            ele = self.find_element(locator)
            text = ele.text
            if text == "":
                text = ele.accessible_name
            logger.info(f"元素{locator}的text为{text}")
            return text
    
        def move_to_element(self, locator):
            """
            鼠标移动到指定位置
            :param locator: 指定位置
            :return:
            """
            ele = self.find_element(locator)
            self.actions.move_to_element(ele).perform()
            logger.info(f"鼠标移动到{locator}位置")
    
        def drag_and_drop(self, locator_start, locator_end):
            """
            鼠标拖动元素到另一个元素
            :param locator_start: 元素开始位置
            :param locator_end: 元素结束位置
            :return:
            """
            start = self.find_element(locator_start)
            end = self.find_element(locator_end)
            self.actions.drag_and_drop(start, end).perform()
            logger.info(f"鼠标从{locator_start}拖动到{locator_end}")
    
        def drag_and_drop_by_offset(self, locator, x, y):
            """
            拖动一段距离
            :param locator: 拖动的元素
            :param x: x距离
            :param y: y距离
            :return:
            """
            ele = self.find_element(locator)
            self.actions.drag_and_drop_by_offset(ele, x, y)
            logger.info("鼠标拖动一段距离")
    
        def select_by_index(self, locator, index):
            """
            根据下标获取select
            :param locator: 元素定位信息
            :param index: 下标,从0开始
            :return:
            """
            ele = self.find_element(locator)
            select = Select(ele)
            select.select_by_index(index)
            logger.info(f"根据下表{index}获取select")
    
        def select_by_value(self, locator, value):
            """
            根据value值获取select
            :param locator: 元素定位信息
            :param value: value值
            :return:
            """
            ele = self.find_element(locator)
            select = Select(ele)
            select.select_by_value(value)
            logger.info(f"根据下表{value}获取select")
    
        def select_by_visible_text(self, locator, visible_text):
            """
            根据visible_text值获取select
            :param locator: 元素定位信息
            :param visible_text: visible_text值
            :return:
            """
            ele = self.find_element(locator)
            select = Select(ele)
            select.select_by_visible_text(visible_text)
            logger.info(f"根据下表{visible_text}获取select")
    
    # 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
    import time
    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()
    
        # def test_baidu5(self, driver1):
        #     page = PageBaidu(driver1)
        #     page.click_button(page.news)
        #     page.switch_to_window()
        #     page.click_button(page.help)
        #     page.switch_to_window(to_parent_window=True)
        #     page.send_keys(page.input, "UI自动化")
        #     time.sleep(3)
    
        # def test_baidu6(self, driver):
        #     page = PageBaidu(driver)
        #     print(page.get_title())
        #     print(page.get_page_source())
        #     print(page.get_current_url())
    
        def test_baidu7(self, driver):
            page = PageBaidu(driver)
            print(page.get_text(page.news))
            print(page.get_text(page.button))
    
        def test_baidu8(self, driver):
            page = PageBaidu(driver)
            page.move_to_element(page.more)
            sleep(3)
    
    # 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')
        # 帮助
        help = (By.CSS_SELECTOR, 'a[href="//help.baidu.com"]')
        # 更多
        more = (By.XPATH, '//*[@id="s-top-left"]/div/a')
    
        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("我是后置步骤")
    
    # project > page > page_sahi.py
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    from base.base_page import BasePage
    from utils.log_util import logger
    
    
    class PageSahi(BasePage):
        # 起始位置
        start = (By.ID, 'dragger')
        # 结束位置
        end = (By.XPATH, '/html/body/div[5]')
        # select
        select = (By.ID,"s1")
    
    

    相关文章

      网友评论

          本文标题:68. Page Object模式 - Select操作 二次封

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