【UI自动化测试】7_TPshop项目实战 _前台-门户网站

文章目录

一、门户网站

bash 复制代码
page:(包) PO封装
- buyer_page:(包)
-- ①index_page.py 代表页面类,搜索关键字
-- ②goods_page.py 包含搜索结果页和加入购物车

script:(包) 测试案例
- buyer_script:(包)
--  test_begin.py
-- ①test_goods.py 搜索商品
-- ②test_cart.py  加入购物车
--  test_end.py

二、测试案例

2.1 搜索商品案例

2.1.1 PO封装

python 复制代码
from selenium.webdriver.common.by import By
from base.base_page import BuyerBasePage

# 定义代表页面类
class IndexPage(BuyerBasePage):
    # 定义实例属性:管理本次用例在该页面操作元素定位信息
    def __init__(self):
        super().__init__()
        # 搜索输入框
        self.search_box = (By.ID, "q")
        # 搜索按钮
        self.search_btn = (By.CLASS_NAME, "ecsc-search-button")

    # 定义业务方法:组织测试用例在该页面所执行的操作步骤【excel用例:测试步骤】
    def query_goods(self, key_word):
        # 1、在首页输入搜索关键字
        self.input_text(self.find_el(self.search_box), key_word)
        # 2、点击【搜索】
        self.find_el(self.search_btn).click()

2.1.2 用例编写

python 复制代码
import time
import allure
import pytest

import config
from config import BASE_PATH
from page.buyer_page.index_page import IndexPage
from utils import DriverUtils, el_is_exist_by_text

@pytest.mark.run(order=103)
# 定义测试类
class TestGoods :

    def setup_class(self):
        # 1、打开浏览器
        self.driver = DriverUtils.get_buyer_driver()
        # 打开测试网址
        self.driver.get("http://192.168.189.139")

    def teardown_class(self):
        # 5、关闭浏览器
        DriverUtils.quit_buyer_driver()

    # 定义测试方法 【对应测试案例:标题】
    def test_query_goods(self):
        # 2、在首页输入搜索关键字
        # 3、点击【搜索】
        IndexPage().query_goods(config.GOODS_NAME)
        # 4、断言
        try:
            # 调用根据文本判断当前页面是否存在的函数
            assert el_is_exist_by_text(self.driver, False, config.GOODS_NAME)
        except Exception as e:
            # 将错误截图保存到allure的测试报告中
            allure.attach(self.driver.get_screenshot_as_png(),
                          BASE_PATH + "/img/query_goods.png",
                          allure.attachment_type.PNG)
            raise e   # 继续抛出异常(如果不写,则表示捕获到异常,但是不跑出来)

2.2 加入购物车案例

2.2.1 PO封装

python 复制代码
from selenium.webdriver.common.by import By
import config
from base.base_page import BuyerBasePage

# 包含搜索结果页+商品详情页
class GoodsPage(BuyerBasePage):

    def __init__(self):
        super().__init__()
        # 商品名称
        self.goods_name = (By.XPATH, "//*[contains(text(),'{} ')]")
        # 加入购物车
        self.add_cart_btn = (By.ID, "join_cart")
        # iframe标签【加入购物车弹窗在iframe标签中】
        self.frame = (By.CSS_SELECTOR, "[id*='layui-layer-iframe1']")

    # 加入购物车方法
    def add_goods_cart(self):
        # 点击商品名称
        # self.find_el(self.goods_name).click()
        self.driver.find_element(self.goods_name[0], self.goods_name[1].format(config.GOODS_NAME)).click()

        # 点击加入购物车
        self.find_el(self.add_cart_btn).click()
        # frame切换,调用父类切换frame的方法
        self.switch_frame(self.find_el(self.frame))

2.2.2 用例编写

python 复制代码
import time

import allure
import pytest

import config
from config import BASE_PATH
from page.buyer_page.goods_page import GoodsPage
from page.buyer_page.index_page import IndexPage
from utils import DriverUtils, el_is_exist_by_text, get_el_text

# 定义测试类
@pytest.mark.run(order=104)
class TestGoods :

    def setup_class(self):
        # 1、打开浏览器
        self.driver = DriverUtils.get_buyer_driver()
        # 打开测试网址
        self.driver.get("http://192.168.189.139")

    def teardown_class(self):
        # 5、关闭浏览器
        DriverUtils.quit_buyer_driver()

    # 定义测试方法 【对应测试案例:标题】
    def test_add_cart(self):
        # 2、搜索商品
        IndexPage().query_goods(config.GOODS_NAME)
        # 3、点击商品标题进入详情页后,再点击加入购物车
        GoodsPage().add_goods_cart()
        time.sleep(5)
        # 断言
        try:
            # 获取弹出框提示信息
            msg = get_el_text(self.driver, "//*[@class='conect-title']/span")
            assert "添加成功" in msg
        except Exception as e:
            # 将错误截图保存到allure的测试报告中
            allure.attach(self.driver.get_screenshot_as_png(),
                          BASE_PATH + "/img/test_add_cart.png",
                          allure.attachment_type.PNG)
            raise e   # 继续抛出异常(如果不写,则表示捕获到异常,但是不跑出来)

三、追加关闭驱动对象开关和排序

批量运行所有测试案例:运行后台的所有测试用例(添加商品),再运行前端门户网站的测试用例(搜索商品并添加购物车)。

自动化:可以重复无限使用。

1、确保前端门户网站也是按顺序执行的

2、门户网站测试用例也只打开一次关闭一次浏览器

3、后台发布的商品是前台搜索商品的条件

3.1 门户网站 驱动对象修改

3.2 门户网站排序

四、商品名称全局参数

后台发布商品,作为前台门户网站搜索以及加入购物车的一个动态商品的参数。(后台发布什么商品,前台查什么商品)


五、总结

相关推荐
鹤卿1235 小时前
(OC)UI学习——网易云仿写
ui·ios·objective-c
一个被程序员耽误的厨师10 小时前
04-实践篇-让AI生成可视化页面-ai-json-ui的落地实践
人工智能·ui·json
秋雨梧桐叶落莳10 小时前
iOS——QQ音乐仿写项目总结
学习·macos·ui·ios·mvc·objective-c·xcode
俏皮小混子14 小时前
山东大学软件学院项目实训-创新实训-计科智伴(六)——个人博客(后端运行后真实调整)
人工智能·笔记·学习·ui
默子昂1 天前
ollama 自定义ui
开发语言·python·ui
星栈独行1 天前
Makepad 应用如何读文件、调接口、保存数据
前端·程序人生·ui·rust·github
wuhuhuan1 天前
playwright-getByAltText
ui·playwright
狼哥16861 天前
《新闻资讯》四、视频模块实现指南
ui·华为·音视频·harmonyos
为何创造硅基生物1 天前
LVGL
c++·ui
星栈独行2 天前
Rust + Makepad 应用怎么打包发布:Windows、macOS、Linux 全平台交付
windows·程序人生·macos·ui·rust