美文网首页
52. Page Object模式 - 3 page类继承基类-

52. Page Object模式 - 3 page类继承基类-

作者: 薛东弗斯 | 来源:发表于2024-03-26 06:22 被阅读0次
继续分离
project > test_po > conftest.py
project > test_po > test_baidu.py
project  > page > page_baidu.py
project  > base > base_page.py
# project  > base > base_page.py
from selenium import webdriver

class BasePage:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        self.driver.get("https://www.baidu.com/")
# project > test_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 > test_po > test_baidu.py
from time import sleep
from page.page_baidu import PageBaidu

class TestBaidu:
    def test_baidu1(self):
        page = PageBaidu()
        page.search_keyword("UI自动化")
        sleep(2)
# project  > page > page_baidu.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 PageBaidu:
    # 新闻
    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)
        self.driver.find_element(*self.button).click()

相关文章

网友评论

      本文标题:52. Page Object模式 - 3 page类继承基类-

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