web自动化-电子商务实战

一、产品分析

http://litemall.hogwarts.ceshiren.com/#/dashboard

二、测试用例分析

用例标题 前提条件 用例步骤 预期结果 实际结果
添加商品类目 1. 登录并进入用户管理后台;2. 登录账号有商场管理的权限 1. 点击增加;2. 输入类目名称;3. 点击确定 1. 跳转商品类目列表;2. 新增在最后一行,新增成功
删除商品类目 1. 进入用户管理后台;2. 商品列表里面有已存在的商品(新增) 1. 点击删除按 1. 是否有删除成功提示;2. 被删除商品不在商品类目列表展示

三、脚本编写

3.1、编写脚本思路

image

    1. 编写与业务无关的前置后置动作
    1. 初步实现功能
    • 编写登录步骤
    • 分别实现两条用例,保证单步调通
    • 执行测试类,保证一起执行也能成功
    1. 代码优化:使用显式等待替换强制等待
    1. 完善细节: 日志、报告、截图

3.2、前置后置

  • 在setup_class打开浏览器
  • teardown_class关闭浏览器进程
  • 添加隐式等待配置

3.3、初步实现功能

  • 登录功能
  • 新增功能
  • 删除功能
  • 整体调试

四、脚本优化

"""
 -*- coding: utf-8-*-
 @File: test_good_manage.py
 @Author: chen
 @time: 2023/09/01/ 7:21
"""
import time
import allure
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from ui_base import UIBase


class TestMgtGoodList(UIBase):
    def get_screenshot(self):
        timestamp = int(time.time())
        picture_path = f"./pictures/screenshot_{timestamp}.PNG"
        self.driver.save_screenshot(picture_path)
        allure.attach.file(picture_path,name="picture",attachment_type=allure.attachment_type.PNG)

    def test_add(self):
        product_name = "IQ02"
        self.driver.find_element(By.XPATH, '//*[@class="el-icon-edit"]').click()
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable(
                (By.XPATH, '//*[@class="el-icon-plus avatar-uploader-icon"]')
            )
        )
        self.driver.find_elements(By.XPATH, '//*[@class="el-input__inner"]')[0].send_keys("CC090102")
        self.driver.find_elements(By.XPATH, '//*[@class="el-input__inner"]')[1].send_keys(product_name)
        ele = self.driver.find_element(By.XPATH, '//*[text()="上架"]')
        self.action.scroll_to_element(ele)
        self.action.scroll_by_amount(0, 20)
        ele.click()
        self.get_screenshot()
        # 断言1:可以找到创建成功字样的元素证明创建成功
        assert self.driver.find_element(By.XPATH, '//*[text()="创建成功"]')
        # 断言2:在商品列表查找新增商品的名字,如果不为空,则断言成功
        assert self.driver.find_elements(By.XPATH, f'//*[text()="{product_name}"]')
        # 清理脏数据
        self.driver.find_element(By.XPATH, f'//*[text()="{product_name}"]/../..//*[text()="删除"]').click()

    def test_update(self):
        product_name = "IQ02"
        # 构造测试数据
        self.driver.find_element(By.XPATH, '//*[@class="el-icon-edit"]').click()
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable(
                (By.XPATH, '//*[@class="el-icon-plus avatar-uploader-icon"]')
            )
        )
        self.driver.find_elements(By.XPATH, '//*[@class="el-input__inner"]')[0].send_keys("CC090102")
        self.driver.find_elements(By.XPATH, '//*[@class="el-input__inner"]')[1].send_keys(product_name)
        ele = self.driver.find_element(By.XPATH, '//*[text()="上架"]')
        self.action.scroll_to_element(ele)
        self.action.scroll_by_amount(0, 20)
        ele.click()

        self.driver.find_element(By.XPATH, f'//*[text()="{product_name}"]/../..//*[text()="编辑"]').click()
        ele = self.driver.find_element(By.XPATH, '//*[text()="更新商品"]')
        self.action.scroll_to_element(ele)
        # self.action.scroll_by_amount(0, 20)
        ele.click()
        self.get_screenshot()
        # 断言1:可以找到编辑成功字样的元素证明编辑成功
        assert self.driver.find_element(By.XPATH, '//*[text()="编辑成功"]')
        # 清理脏数据
        self.driver.find_element(By.XPATH, f'//*[text()="{product_name}"]/../..//*[text()="删除"]').click()

    def test_find(self):
        product_name = "IQ02"
        # 构造测试数据
        self.driver.find_element(By.XPATH, '//*[@class="el-icon-edit"]').click()
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable(
                (By.XPATH, '//*[@class="el-icon-plus avatar-uploader-icon"]')
            )
        )
        self.driver.find_elements(By.XPATH, '//*[@class="el-input__inner"]')[0].send_keys("CC090102")
        self.driver.find_elements(By.XPATH, '//*[@class="el-input__inner"]')[1].send_keys(product_name)
        ele = self.driver.find_element(By.XPATH, '//*[text()="上架"]')
        self.action.scroll_to_element(ele)
        self.action.scroll_by_amount(0, 20)
        ele.click()

        self.driver.find_element(By.XPATH, f'//*[text()="{product_name}"]/../..//*[text()="查看"]').click()
        self.get_screenshot()
        # 断言1:可以找到【商品详情】字样的元素证明查看成功
        assert self.driver.find_element(By.XPATH, '//*[text()="商品详情"]')
        self.driver.refresh()
        # 清理脏数据
        self.driver.find_element(By.XPATH, f'//*[text()="{product_name}"]/../..//*[text()="删除"]').click()

    def test_delete(self):
        product_name = "IQ02"
        # 构造测试数据
        self.driver.find_element(By.XPATH, '//*[@class="el-icon-edit"]').click()
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable(
                (By.XPATH, '//*[@class="el-icon-plus avatar-uploader-icon"]')
            )
        )
        self.driver.find_elements(By.XPATH, '//*[@class="el-input__inner"]')[0].send_keys("CC090102")
        self.driver.find_elements(By.XPATH, '//*[@class="el-input__inner"]')[1].send_keys(product_name)
        ele = self.driver.find_element(By.XPATH, '//*[text()="上架"]')
        self.action.scroll_to_element(ele)
        self.action.scroll_by_amount(0, 20)
        ele.click()

        self.driver.find_element(By.XPATH, f'//*[text()="{product_name}"]/../..//*[text()="删除"]').click()
        self.get_screenshot()
        # 断言1:可以找到【删除成功】字样的元素证明【删除成功】
        assert self.driver.find_element(By.XPATH, '//*[text()="删除成功"]')
        # 断言2:在商品列表查找删除商品的名字,如果为空,则断言成功
        assert not self.driver.find_elements(By.XPATH, f'//*[text()="{product_name}"]')

五、输出测试报告