上例,以下两句放到类里面,还是无法给其它文件调用。
news = (By.CSS_SELECTOR, 'a[href="http://news.baidu.com"]')
button = (By.ID, 'su')
新建一个page文件夹,用于存放元素定位的内容,此时代码结构
project > page > page_baidu.py
project > PO > test_baidu.py
project > PO > conftest.py
# 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(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 > test_baidu.py
# from selenium.webdriver.common.by import By
from page.page_baidu import PageBaidu
from utils.assert_util import assert_compare
# from utils.log_util import logger
class TestBaidu:
def test_baidu1(self, driver):
page = PageBaidu(driver)
# 未登录测试
driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(*page.news).text
button_text = driver.find_element(*page.button).accessible_name
assert title == "百度一下,你就知道"
assert url == "https://www.baidu.com/"
assert text == "新闻"
assert button_text == "百度一下"
def test_baidu2(self, driver):
page = PageBaidu(driver)
# 登录测试
# driver.get("https://www.baidu.com/")
title = driver.title
url = driver.current_url
text = driver.find_element(*page.news).text
button_text = driver.find_element(*page.button).accessible_name
# assert title == "百度一下,你就知道"
assert_compare(title, "==", "百度一下,你就知道")
# assert url == "https://www.baidu.com/"
assert_compare(url, "==", "https://www.baidu.com/")
assert text == "新闻"
assert button_text == "百度一下"
# 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("我是后置步骤")
网友评论