base_page.py
from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
class BasePage:
def __init__(self, driver=None):
if driver is None:
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
else:
self.driver = driver
def find(self, loc) -> WebElement:
return self.driver.find_element(*loc)
index_page.py
from base_page import BasePage
from search_page import SearchPage
from selenium.webdriver.common.by import By
class IndexPage(BasePage):
base_url = “https://ceshiren.com/”
search_btn = (By.CSS_SELECTOR, “.d-icon-search”)
search_input = (By.ID, “search-term”)
adv_search_btn = (By.CSS_SELECTOR, “.show-advanced-search”)
def open_index_page(self):
self.driver.get(self.base_url)
def click_search_btn(self):
self.find(self.search_btn).click()
def input_search_value(self, search_value):
self.find(self.search_input).send_keys(search_value)
def click_adv_search_btn(self):
self.find(self.adv_search_btn).click()
return SearchPage(self.driver)
search_page.py
from base_page import BasePage
from topic_page import TopicPage
from selenium.webdriver.common.by import By
class SearchPage(BasePage):
search_result_title = (By.CSS_SELECTOR, “.search-link”)
#搜索结果
def get_search_result(self):
return self.find(self.search_result_title).text
#点击搜索
def click_search_result(self):
self.find(self.search_result_title).click()
return TopicPage(self.driver)
topic_page.py
from base_page import BasePage
from selenium.webdriver.common.by import By
class TopicPage(BasePage):
topic_title = (By.CSS_SELECTOR, “.fancy-title”)
topic_body = (By.CSS_SELECTOR, “.inline-footnotes”)
def get_title_text(self):
return self.find(self.topic_title).text
def get_body_text(self):
return self.find(self.topic_body).text
test_ceshiren.py
from index_page import IndexPage
class TestCeshiren:
def setup(self):
self.index_page = IndexPage()
self.index_page.open_index_page()
def test_search(self):
# 搜索内容
search_value = "Selenium"
# 点击查询按钮
self.index_page.click_search_btn()
# 输入查询值
self.index_page.input_search_value(search_value)
# 点击高级查询按钮
search_page = self.index_page.click_adv_search_btn()
# 进行断言:查询结果是否包含关键字
res = search_page.get_search_result()
assert search_value in res
# 点击标签进入帖子
topic_page = search_page.click_search_result()
# 进行断言:判断查询结果是否包含关键字
assert search_value in topic_page.get_title_text()
assert search_value in topic_page.get_body_text()