web自动化测试

调查问卷

企业微信截图_16969835086792

ppt

笔记格式:markdown

相关链接

代码

打开浏览器

from time import sleep
from selenium import webdriver

# 创建一个 Chromdriver 的实例
driver = webdriver.Chrome()
# 打开网址
driver.get("https://www.baidu.com/")

sleep(10)

搜狗搜索

# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class TestSougou():
  def setup_method(self, method):
    self.driver = webdriver.Chrome()
    self.vars = {}
  
  def teardown_method(self, method):
    self.driver.quit()
  
  def test_sougou(self):
    self.driver.get("https://www.sogou.com/")
    self.driver.set_window_size(995, 579)
    self.driver.find_element(By.ID, "query").click()
    self.driver.find_element(By.ID, "query").send_keys("霍格沃兹测试开发")
    self.driver.find_element(By.ID, "stb").click()

报告生成

执行测试,生成报告
pytest test_sougou.py --alluredir=results
根据报告结果进行展示
allure serve results

ceshiren搜索

from selenium import webdriver
from selenium.webdriver.common.by import By


class TestCeshiren:
    # 测试前置动作
    def setup(self):
        base_url = "https://ceshiren.com/"
        # 打开测试人首页
        self.driver = webdriver.Chrome()
        self.driver.get(base_url)
        # 设置隐式等待时间
        self.driver.implicitly_wait(10)

    # 搜索功能测试
    def test_search(self):
        search_btn = (By.CSS_SELECTOR,".d-icon-search")
        search_input = (By.ID,"search-term")
        adv_search_btn = (By.CSS_SELECTOR,".show-advanced-search")
        search_result_title = (By.CSS_SELECTOR,".search-link")
        search_value = "Selenium"
        # 点击查询按钮
        self.driver.find_element(*search_btn).click()
        # 输入查询值
        self.driver.find_element(*search_input).send_keys(search_value)
        # 点击高级查询按钮
        self.driver.find_element(*adv_search_btn).click()

        # 判断查询结果是否包含关键字
        res = self.driver.find_element(*search_result_title)
        assert search_value in res.text

po封装

  1. 将帖子中(自己已成功调通)面向过程的代码复制到项目中
  2. 根据po设计(提取页面元素、值、动作)封装IndexPage、SearchPage、BasePage
  3. 使用po对象编写测试类

作业

使用po的封装方式判断帖子详情页面的标题和内容内容是否包含关键字

202041412102 甘胜华
base_page.py:

from selenium import webdriver


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, element):
        #找元素
        return self.driver.find_element(*element)


index_page.py:

from selenium.webdriver.common.by import By

from page_object.pages.base_page import BasePage
from page_object.pages.search_page import SearchPage


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_url(self):
        #打开首页
        self.driver.get(self.base_url)

    def click_search_btn(self):
        #点击搜索按钮
        self.find(self.search_btn).click()

    def send_keys(self, search_value):
        #往查询框输入待查内容
        self.find(self.search_input).send_keys(search_value)

    def click_adv_btn(self):
        #点击高级查询按钮
        self.find(self.adv_search_btn).click()
        return SearchPage(self.driver)

search_page.py:

from selenium.webdriver.common.by import By

from page_object.pages.base_page import BasePage
from page_object.pages.topic_page import TopicPage


class SearchPage(BasePage):
    search_result_title = (By.CSS_SELECTOR, ".search-link")

    def get_search_result_text(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 selenium.webdriver.common.by import By

from page_object.pages.base_page import BasePage


class TopicPage(BasePage):
    topic_title = (By.CSS_SELECTOR, ".fancy-title")
    topic_content = (By.CSS_SELECTOR, ".cooked.inline-footnotes")

    def get_title_text(self):
        #获取帖子标题
        return self.find(self.topic_title).text

    def get_content_text(self):
        #获取帖子内容
        return self.find(self.topic_content).text



test_ceshiren.py

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

from page_object.pages.index_page import IndexPage


class TestCeshiren:
    #测试前置条件
    def setup(self):
        self.index_page = IndexPage()
        self.index_page.open_url()

    #搜索功能测试
    # @pytest.mark.parametrize("search_value", ["Selenium", "测试"])
    def test_search(self):
        search_value = "Selenium"

        #点击查询按钮
        self.index_page.click_search_btn()
        #输入查询值
        self.index_page.send_keys(search_value)
        #点击高级查询按钮
        search_page = self.index_page.click_adv_btn()

        #判断查询页面结果是否包含关键字
        res = search_page.get_search_result_text()
        assert search_value in res

        #点击进入帖子详情页面
        topic_page = search_page.click_search_result()
        #判断帖子详情页面title是否包含关键字
        assert search_value in topic_page.get_title_text()

        #判断帖子详情内容是否包含关键字
        assert search_value in topic_page.get_content_text()

202041412218 倪灿烽
topic_page.py

from selenium.webdriver.common.by import By
from web_obj.page.base_page import BasePage


class TopicPage(BasePage):
    topic_title = (By.CSS_SELECTOR, ".fancy-title")
    topic_content = (By.CSS_SELECTOR, ".inline-footnotes")

    def get_title_text(self):
        # 获取帖子标题
        return self.find(self.topic_title).text

    def get_content_text(self):
        # 获取帖子内容
        return self.find(self.topic_content).text

test_ceshiren_po.py

import pytest

from web_obj.page.index_page import IndexPage


class TestCeshiren:
    # 测试前置动作
    def setup(self):
        self.index_page = IndexPage()
        self.index_page.open_index_page()

    # 搜索功能测试
    # @pytest.mark.parametrize("search_value", ["Selenium", "测试"])
    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_text()
        assert search_value in res
        # 点击进入帖子详情页面
        topic_page = search_page.click_search_result()
        # 判断帖子详情页面title是否包含关键字
        assert search_value in topic_page.get_title_text()
        # todo:判断帖子详情内容是否包含关键字
        assert search_value in topic_page.get_content_text()

202041412213 林梓豪
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:
        """

        :param loc:
        :return:
        """

        return self.driver.find_element(*loc)

index_page.py

from selenium.webdriver.common.by import By

from page_obj.page.base_page import BasePage
from page_obj.page.search_page import SearchPage


class IndexPage(BasePage):
    base_url = "http://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 selenium.webdriver.common.by import By

from page_obj.page.base_page import BasePage
from page_obj.page.topic_page import TopicPage


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 selenium.webdriver.common.by import By

from page_obj.page.base_page import BasePage


class TopicPage(BasePage):
    topic_title = (By.CSS_SELECTOR, ".fancy-title")
    topic_content = (By.CSS_SELECTOR, ".inline-footnotes")

    def get_title_text(self):
        return self.find(self.topic_title).text

    def get_topic_content(self):
        return self.find(self.topic_content).text

test_ceshiren_po.py

import time

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

from page_obj.page.index_page import IndexPage


class TestCeshirenPo:
    def setup(self):
        # base_url = "http://ceshiren.com/"
        # 打开测试人首页
        # self.driver = webdriver.Chrome()
        # self.driver.get(base_url)
        self.index_page = IndexPage()
        self.index_page.open_index_page()
    # @pytest.mark.parametrize('search_value',["Selenium","测试"])
    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
        time.sleep(2)
        #点击进入帖子
        topic_page =  search_page.click_search_result()

        #判断是否包含关键字
        assert search_value in topic_page.get_title_text()

        # todo: 判断帖子详情内容是否包含关键字
        assert search_value in topic_page.get_topic_content()

        time.sleep(2)

202041412126 于智康
base_page.py
image
index_page.py
image
search_page.py
image
test_ceshiren_po.py
image
topic_page.py
image

202041404203 段杰东
test_ceshiren_po.py
from seleniumtest.index_page import IndexPage
from seleniumtest.search_page import SearchPage

class TestCesshiren:
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()

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:

    # :param
    # :return

    return self.driver.find_element(*loc)

search_page.py
from selenium.webdriver.common.by import By

from seleniumtest.basepage import BasePage
from seleniumtest.topic_page import TopicPage

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)

index_page.py
from seleniumtest.basepage import BasePage
from selenium.webdriver.common.by import By

from seleniumtest.search_page import SearchPage

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)

202041412208蒋冠岳
index_page.py

from selenium.webdriver.common.by import By

from page_obj.pages.base_page import BasePage
from page_obj.pages.search_page import SearchPage


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)

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)

search_page.py

from selenium.webdriver.common.by import By

from page_obj.pages.base_page import BasePage
from page_obj.pages.topic_page import TopicPage


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 selenium.webdriver.common.by import By

from page_obj.pages.base_page import BasePage


class TopicPage(BasePage):
    topic_title = (By.CSS_SELECTOR, ".fancy-title")
    page_title = (By.CSS_SELECTOR, ".inline-footnotes")

    def get_title_text(self):
        return self.find(self.topic_title).text

    def get_page_text(self):
        return self.find(self.page_title).text

test_ceshiren_po.py

import pytest

from page_obj.pages.index_page import IndexPage


class TestCeShiRen:
    def setup(self):
        self.index_page = IndexPage()
        self.index_page.open_index_page()

    # @pytest.mark.parametrize('search_value', ["selenium", "测试"])
    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()

        # 获取帖子内容
        page_text = topic_page.get_page_text()
        assert search_value in page_text

翁开锋 202041412223
base_page.py

from selenium import webdriver
from selenium.webdriver.remote import webelement
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,temp) -> WebElement:
        return self.driver.find_element(*temp)

index.py

from selenium.webdriver.common.by import By

from page_obj.pages.base_page import BasePage
from page_obj.pages.search_page import SearchPage


class IndexPage(BasePage):
    search_btn = (By.CSS_SELECTOR, ".d-icon-search")
    search_input = (By.ID, "search-term")
    svg_search_btn = (By.CSS_SELECTOR, ".d-icon-sliders-h")
    url="http://www.ceshiren.com"

    def index_open(self):
        self.driver.get(self.url)

    def click_search_btn(self):
        self.find(self.search_btn).click()

    def set_search_input(self,text):
        self.find(self.search_input).send_keys(text)

    def click_svg_search_btn(self):
        self.find(self.svg_search_btn).click()
        return SearchPage(self.driver)

search_page.py

from selenium.webdriver.common.by import By

from page_obj.pages.base_page import BasePage
from page_obj.pages.topic_page import TopicPage


class SearchPage(BasePage):
    search_link = (By.CSS_SELECTOR, ".search-link")

    def text_search_link(self):
        return self.find(self.search_link)

    def click_search_link(self):
        self.find(self.search_link).click()
        return TopicPage(self.driver)

topic_page.py

from selenium.webdriver.common.by import By

from page_obj.pages.base_page import BasePage


class TopicPage(BasePage):
    tite = (By.CSS_SELECTOR,".fancy-title")

    content = (By.CSS_SELECTOR, ".cooked.inline-footnotes")

    def text_tite(self):
        return self.find(self.tite).text

    def text_content(self):
        return self.find(self.content).text

test_ceshiren_po.py

import webbrowser

from selenium import webdriver
from selenium.webdriver.common.by import By

from page_obj.pages.index_page import IndexPage


class TestCeshirenPO:
    def setup(self):
        # self.driver = webdriver.Chrome()
        # self.driver.implicitly_wait(10)
        # self.driver.get("http://www.ceshiren.com")
        self.index = IndexPage()
        self.index.index_open()

    def test_search(self):
        search_text = "selenium"

        self.index.click_search_btn()
        self.index.set_search_input(search_text)
        search_page = self.index.click_svg_search_btn()
        res = search_page.text_search_link()
        assert search_text in res.text
        # 进入详情页
        topic_page = search_page.click_search_link()

        # 判断标题是否含有关键字
        assert search_text in topic_page.text_tite()

        # todo:判断内容是否含有关键字
        assert search_text in topic_page.text_content()



姓名:胡福生 学号:202041404204
test_ceshiren_po.py

import allure
import pytest
from index_page import IndexPage

@allure.feature("测试搜索功能-202041404204胡福生")
class TestCeshiren:
    @allure.story("打开搜索页面")
    def setup(self):
        self.index_page=IndexPage()
        self.index_page.open_index_page()

    @allure.story("搜索")
    def test_search(self):
        search_value="Selenium"
        with allure.step("点击查询输入框"):
            self.index_page.click_search_btn()
        with allure.step("输入查询值"):
            self.index_page.input_search_value(search_value)
        with allure.step("点击查询按钮"):
            search_page=self.index_page.click_adv_search_btn()
        res=search_page.get_search_result_text()
        assert search_value in res
        with allure.step("点击查询结果"):
            topic_page=search_page.click_search_result()
        with allure.step("判断查询结果的标题是否包含关键字"):
            assert search_value in topic_page.get_title_text()
        with allure.step("判断查询结果的内容是否包含关键字"):
            assert search_value in topic_page.get_content_text()

topic_page.py

from selenium.webdriver.common.by import By
from base_page import BasePage


class TopicPage(BasePage):
    topic_title=(By.CSS_SELECTOR,".fancy-title")
    topic_content = (By.CSS_SELECTOR, ".cooked.inline-footnotes")

    def get_title_text(self):
        return self.find(self.topic_title).text

    def get_content_text(self):
        return self.find(self.topic_content).text

search_page.py

from selenium.webdriver.common.by import By
from base_page import BasePage
from topic_page import TopicPage


class SearchPage(BasePage):
    search_result_title = (By.CSS_SELECTOR, ".search-link")

    def get_search_result_text(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)

index_page.page.py

from base_page import BasePage
from selenium.webdriver.common.by import By
from search_page import SearchPage

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)

base_page.py

from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.by import By

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)

202041412221 阮凯林

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()

202041412216 刘尚杰
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)

serch_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()

202041404216 聂振枫

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

search_page.py
image

test_ceshiren.py``

topic_page.py

结果:

202041412210 李侨广



202041404121 阮达
basePage

from selenium import webdriver


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, element):
        #找元素
        return self.driver.find_element(*element)

indexPage

from selenium.webdriver.common.by import By

from study3.base_page import BasePage
from study3.search_page import SearchPage


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_url(self):
        #打开首页
        self.driver.get(self.base_url)

    def click_search_btn(self):
        #点击搜索按钮
        self.find(self.search_btn).click()

    def send_keys(self, search_value):
        #往查询框输入待查内容
        self.find(self.search_input).send_keys(search_value)

    def click_adv_btn(self):
        #点击高级查询按钮
        self.find(self.adv_search_btn).click()
        return SearchPage(self.driver)

searchPage

from selenium.webdriver.common.by import By

from study3.base_page import BasePage
from study3.topic_page import TopicPage


class SearchPage(BasePage):
    search_result_title = (By.CSS_SELECTOR, ".search-link")

    def get_search_result_text(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)

test

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

from study3.index_page import IndexPage


class TestCeshiren:
    #测试前置条件
    def setup(self):
        self.index_page = IndexPage()
        self.index_page.open_url()

    #搜索功能测试
    # @pytest.mark.parametrize("search_value", ["Selenium", "测试"])
    def test_search(self):
        search_value = "Selenium"

        #点击查询按钮
        self.index_page.click_search_btn()
        #输入查询值
        self.index_page.send_keys(search_value)
        #点击高级查询按钮
        search_page = self.index_page.click_adv_btn()

        #判断查询页面结果是否包含关键字
        res = search_page.get_search_result_text()
        assert search_value in res

        #点击进入帖子详情页面
        topic_page = search_page.click_search_result()
        #判断帖子详情页面title是否包含关键字
        assert search_value in topic_page.get_title_text()

        #判断帖子详情内容是否包含关键字
        assert search_value in topic_page.get_content_text()

topicPage

from selenium.webdriver.common.by import By

from study3.base_page import BasePage


class TopicPage(BasePage):
    topic_title = (By.CSS_SELECTOR, ".fancy-title")
    topic_content = (By.CSS_SELECTOR, ".cooked.inline-footnotes")

    def get_title_text(self):
        #获取帖子标题
        return self.find(self.topic_title).text

    def get_content_text(self):
        #获取帖子内容
        return self.find(self.topic_content).text
``

姓名 彭子晴 学号202041404120




index_page.py:

from selenium.webdriver.common.by import By

from page_object.pages.base_page import BasePage
from page_object.pages.search_page import SearchPage

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_url(self):
    #打开首页
    self.driver.get(self.base_url)

def click_search_btn(self):
    #点击搜索按钮
    self.find(self.search_btn).click()

def send_keys(self, search_value):
    #往查询框输入待查内容
    self.find(self.search_input).send_keys(search_value)

def click_adv_btn(self):
    #点击高级查询按钮
    self.find(self.adv_search_btn).click()
    return SearchPage(self.driver)

search_page.py:

from selenium.webdriver.common.by import By

from page_object.pages.base_page import BasePage
from page_object.pages.topic_page import TopicPage


class SearchPage(BasePage):
    search_result_title = (By.CSS_SELECTOR, ".search-link")

    def get_search_result_text(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 selenium.webdriver.common.by import By

from page_object.pages.base_page import BasePage


class TopicPage(BasePage):
    topic_title = (By.CSS_SELECTOR, ".fancy-title")
    topic_content = (By.CSS_SELECTOR, ".cooked.inline-footnotes")

    def get_title_text(self):
        #获取帖子标题
        return self.find(self.topic_title).text

    def get_content_text(self):
        #获取帖子内容
        return self.find(self.topic_content).text

test_ceshiren.py

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

from page_object.pages.index_page import IndexPage


class TestCeshiren:
    #测试前置条件
    def setup(self):
        self.index_page = IndexPage()
        self.index_page.open_url()

    #搜索功能测试
    # @pytest.mark.parametrize("search_value", ["Selenium", "测试"])
    def test_search(self):
        search_value = "Selenium"

        #点击查询按钮
        self.index_page.click_search_btn()
        #输入查询值
        self.index_page.send_keys(search_value)
        #点击高级查询按钮
        search_page = self.index_page.click_adv_btn()

        #判断查询页面结果是否包含关键字
        res = search_page.get_search_result_text()
        assert search_value in res

        #点击进入帖子详情页面
        topic_page = search_page.click_search_result()
        #判断帖子详情页面title是否包含关键字
        assert search_value in topic_page.get_title_text()

        #判断帖子详情内容是否包含关键字
        assert search_value in topic_page.get_content_text()
![image|800x339](upload://cpSpWYLmIZebFAqlSGxXtzTQe8j.png)

202041404101 曹岚

202041413209 何宇明




姓名:胡鑫 学号:202041404110
test_ceshiren_po.py

import allure
import pytest
from index_page import IndexPage

@allure.feature("测试搜索功能-202041404110  胡鑫")
class TestCeshiren:
    @allure.story("打开搜索页面")
    def setup(self):
        self.index_page=IndexPage()
        self.index_page.open_index_page()

    @allure.story("搜索")
    def test_search(self):
        search_value="Selenium"
        with allure.step("点击查询输入框"):
            self.index_page.click_search_btn()
        with allure.step("输入查询值"):
            self.index_page.input_search_value(search_value)
        with allure.step("点击查询按钮"):
            search_page=self.index_page.click_adv_search_btn()
        res=search_page.get_search_result_text()
        assert search_value in res
        with allure.step("点击查询结果"):
            topic_page=search_page.click_search_result()
        with allure.step("判断查询结果的标题是否包含关键字"):
            assert search_value in topic_page.get_title_text()
        with allure.step("判断查询结果的内容是否包含关键字"):
            assert search_value in topic_page.get_content_text()

topic_page.py

from selenium.webdriver.common.by import By
from base_page import BasePage


class TopicPage(BasePage):
    topic_title=(By.CSS_SELECTOR,".fancy-title")
    topic_content = (By.CSS_SELECTOR, ".cooked.inline-footnotes")

    def get_title_text(self):
        return self.find(self.topic_title).text

    def get_content_text(self):
        return self.find(self.topic_content).text

search_page.py

from selenium.webdriver.common.by import By
from base_page import BasePage
from topic_page import TopicPage


class SearchPage(BasePage):
    search_result_title = (By.CSS_SELECTOR, ".search-link")

    def get_search_result_text(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)

index_page.page.py

from base_page import BasePage
from selenium.webdriver.common.by import By
from search_page import SearchPage

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)

base_page.py

from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.common.by import By

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)

学号:202041404115
姓名:李升升
image
image
image
image

202041404214刘均豪
base_page
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:
#元素查找
#loc
self.driver.find_element(*loc)

index_page
from selenium.webdriver.common.by import By
from classexam.base_page import BasePage
from classexam.search_page import SearchPage

class IndexPage(BasePage):
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("https://ceshiren.com/")
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()
def input_search_value(self,search_value):
    self.find(self.search_input)
def click_adv_search_btn(self):
    self.find(self.adv_search_btn).click()
    return SearchPage(self.driver)

search_page
from selenium.webdriver.common.by import By

from classexam.base_page import BasePage
from classexam.topic_page import TopicPage

class SearchPage(BasePage):
search_result_title=(By.CSS_SELECTOR,“.search-link”)
#返回查询结果
def get_search_result(self):
return self.find(self.search_result_title)
#点击查询结果
def click_search_result(self):
self.find(self.search_result_title).click()
return TopicPage(self.driver)

topic_page
from turtle import title
from selenium.webdriver.common.by import By
from classexam.base_page import BasePage

class TopicPage(BasePage):
topic_title=(“By.CSS_SELECTOR,”.fancy-title)
def get_title_text(self):
#获取帖子标题
return self.find(self.topic_title).text

test_page
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

from classexam.index_page import IndexPage

class testPage:
def setup(self):
self.index_page=IndexPage()
self.index_page.open_index_page()
@pytest.mark.parametrize(‘search_value’,[“Selenium”,“测试”])
def test_search(self,search_value):
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()
#判断帖子详情页面title是否包含关键字
assert search_value in topic_page.get_title_text()
#todo:判断帖子详情内容是否包含关键字
assert search_value in topic_page.get_content_text()