实训总结

经过本次实训,认识到了软件测试的多种技术,相比之前学过的,了解到了Charles这个抓包工具和app的测试方法。

第一天是写了个登陆的测试用例,主要用到了黑盒测试的等价类划分,边界值分析,场景法,错误推断等方法。

第二天是写了个简单的python程序,进行pytest测试。

第三天主要就是自动化的web测试,写了selenium的测试脚本,实现allure的测试报告生成。

下面是一段python的自动化测试代码:

import time

import allure
import pytest
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


class TestAddAndDelete:

    def setup_class(self):
        self.driver = webdriver.Chrome()
        # 隐式等待,全局等待时间
        self.driver.implicitly_wait(10)
        # 打开页面
        self.driver.get("https://litemall.hogwarts.ceshiren.com/#/login")
        self.driver.maximize_window()

    def teardown_class(self):
        # driver 退出
        self.driver.quit()

    @allure.title("登录进入列表界面")
    def test_before(self):
        # 点击登录
        with allure.step("登录按钮点击"):
            self.driver.find_element(By.XPATH, "/html/body/div[1]/div/form/button").click()
            time.sleep(2)
            # 点击商品管理
        with allure.step("点击商品管理"):
            self.driver.find_element(By.XPATH, "/html/body/div[1]/div/div[1]/div[1]/div/ul/div[3]/li/div").click()
            # 点击商品列表
        with allure.step("点击商品列表"):
            self.driver.find_element(By.XPATH,
                                     "/html/body/div[1]/div/div[1]/div[1]/div/ul/div[3]/li/ul/div[1]/a/li").click()

    @allure.title("正确地添加三次")
    @pytest.mark.parametrize("num,name", [
        ["syh123", "syh123"],
        ["syh456", "syh456"],
        ["syh789", "syh789"]
    ])
    def test_add(self, num, name):
        # 点击添加
        with allure.step("点击添加"):
            self.driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/section/div/div[1]/button[2]").click()
        # 添加编号
        with allure.step("添加编号"):
            self.driver.find_element(By.XPATH,
                                     "/html/body/div[1]/div/div[2]/section/div/div[1]/div/form/div[1]/div/div[1]/input").send_keys(
                num)
        # 填写名称
        with allure.step("填写名称"):
            self.driver.find_element(By.XPATH,
                                     "/html/body/div[1]/div/div[2]/section/div/div[1]/div/form/div[2]/div/div[1]/input").send_keys(
                name)
        # 点击上架
        with allure.step("点击上架"):
            self.driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/section/div/div[5]/button[2]").click()
        time.sleep(2)
        # 显示等待获取对象地文本
        with allure.step("断言"):
            wait = WebDriverWait(self.driver, 10)
        element = wait.until(EC.presence_of_element_located(
            (By.CSS_SELECTOR, "h2.el-notification__title")))
        assert element.text == "成功"

    @pytest.mark.parametrize("num,name", [
        ["syh123", "syh123"],
        ["syh456", ""],
        ["", "syh789"]
    ])
    def test_add_err(self, num, name):
        # 点击商品列表
        with allure.step("点击商品列表"):
            self.driver.find_element(By.XPATH,
                                     "/html/body/div[1]/div/div[1]/div[1]/div/ul/div[3]/li/ul/div[1]/a/li").click()
        # 点击添加
        with allure.step("点击添加"):
            self.driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/section/div/div[1]/button[2]").click()
        time.sleep(2)
        # 输入编号
        with allure.step("输入编号"):
            self.driver.find_element(By.XPATH,
                                     "/html/body/div[1]/div/div[2]/section/div/div[1]/div/form/div[1]/div/div[1]/input").send_keys(
                num)
        # 输入名称
        with allure.step("输入名称"):
            self.driver.find_element(By.XPATH,
                                     "/html/body/div[1]/div/div[2]/section/div/div[1]/div/form/div[2]/div/div[1]/input").send_keys(
                name)
        # 点击上架
        with allure.step("点击上架"):
            self.driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/section/div/div[5]/button[2]").click()
        # 点击确定
        with allure.step("点击确定"):
            self.driver.find_element(By.CSS_SELECTOR,
                                     ".el-button.el-button--default.el-button--small.el-button--primary").click()
        # 查看是否添加成功
        with allure.step("查看是否添加成功"):
            wait = WebDriverWait(self.driver, 10)
        element = wait.until(EC.presence_of_element_located(
            (By.CSS_SELECTOR, "h2.el-notification__title")))
        # 断言
        assert element.text == "成功"

    @allure.title("正确地删除")
    @pytest.mark.parametrize("num", ["syh123", "syh456", "syh789"])
    def test_del(self, num):
        # 点击商品列表
        with allure.step("点击商品列表"):
            self.driver.find_element(By.XPATH,
                                     "/html/body/div[1]/div/div[1]/div[1]/div/ul/div[3]/li/ul/div[1]/a/li").click()
        # 获取编号输入框
        with allure.step("获取编号输入框"):
            input = self.driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/section/div/div[1]/div[2]/input")
        # 填入编号
        with allure.step("填入编号"):
            input.send_keys(num)
        # 点击查询按钮
        with allure.step("点击查询按钮"):
            self.driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/section/div/div[1]/button[1]").click()
        # 点击删除按钮
        with allure.step("点击删除按钮"):
            self.driver.find_element(By.XPATH,
                                     "/html/body/div[1]/div/div[2]/section/div/div[2]/div[3]/table/tbody/tr[1]/td[12]/div/button[2]").click()
        input.clear()
        time.sleep(2)
        with allure.step("断言"):
            wait = WebDriverWait(self.driver, 10)
        element = wait.until(EC.presence_of_element_located(
            (By.CSS_SELECTOR, "h2.el-notification__title")))
        assert element.text == "成功"

 

posted @ 2024-06-29 15:59  一个小虎牙  阅读(6)  评论(0编辑  收藏  举报