简朴博客系统测试报告

一. 项目简介

简朴博客系统是采用前后端分离的方式来实现的,是基于 SpringFrameWork 和 MyBatis 框架实现的一个简易的博客发布网站,同时将其部署到了云服务器上。

目前博客系统主要实现了户的注册登录,文章的编写、发布,以及对自己文章的查看、修改、删除操作,个人文章列表及文章数统计这些;还可以分页显示所有作者汇总的文章列表,显示文章阅读量等。

使用 IDEA 开发,项目用到的技术有,SpringBoot, SpringMVC, MyBatis, MySQL, Redis, Lombok,HTML,CSS,JavaScript,jQuery 等。

二. 测试概要

测试对象:基于 SSM 项目的博客系统。

测试目的:校验博客项目功能是否符合自己的预期。

测试点:主要针对常用的主流程功能进行测试,如:注册、登录、汇总博客列表页、博客编辑页、个人博客列表页、导航栏组件等涉及到的功能。

测试方法和工具:主要是黑盒测试,自动化工具使用 Selenium 和 Junit。

三. 测试环境

硬件:Lenovo Yoga 14S 2021(R7-5800H/16GB/512GB/集显)。

浏览器:Google Chrome 版本 119.0.6045.160(正式版本) (64 位)。

操作系统:Windows 11。

测试工具:Selenium3 和 Junit5。

四. 测试执行概况及功能测试

1. 手工测试

1.1 手动测试用例编写

♨️注册页

在这里插入图片描述

♨️登录页

在这里插入图片描述

♨️个人博客列表页
在这里插入图片描述

♨️博客详情页

在这里插入图片描述

♨️博客编辑页
在这里插入图片描述

1.2 执行的部分测试用例

  1. 🍂登录页:界面能否正常加载,输入正确 / 错误的账号、密码是否能得到预期的响应。
    1️⃣界面能否正常加载。img
    2️⃣账号正确,密码错误。
    预期结果:弹窗提示:“出错了: 登录失败, 请重新操作! 用户名或密码错误! ”。
    实际结果如下:
    img
    3️⃣账号正确,密码为空。
    预期结果:弹窗提示:“请输入密码! ”。
    实际结果如下:
    img
    4️⃣账号正确,密码正确。
    预期结果:页面跳转至个人博客列表页。
    实际结果如下:
    img
  2. 个人博客列表页:检测界面是否符合预期,点击“查看全文”按钮是否能跳转到对应的博客详情页,点击“修改”按钮是否能跳转到博客编辑页并获取到待修改的标题和内容,点击“删除”按钮是否能成功删除文章,点击“注销”是否能退出登录。
    1️⃣界面显示符合预期。
    img
    2️⃣点击“查看全文”按钮是否能跳转到对应的博客详情页。
    预期结果:进入到对应的博客详情页,且能够正确加载文章内容。
    实际结果如下:
    img
    3️⃣点击“修改”。
    预期结果:点击修改后跳转到文章编辑页。
    实际结果如下:
    img
    4️⃣点击“删除”。
    预期结果:点击删除后文章被删除。
    实际结果如下:
    img5️⃣点击“注销”是否能退出登录。
    预期结果:点击注销后退出跳转到登录页面。
    实际结果如下:
    img

2. 自动化测试Selenium

2.1 编写测试用例

在这里插入图片描述

2.2 自动化测试代码

🍂引入依赖:seleniumcommons-iojunitsuiteengine

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.9.2</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>5.9.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <version>1.9.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.9.1</version>
    </dependency>
</dependencies>

🍂初始化工具类InitAndEnd

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class InitAndEnd {
    static WebDriver webDriver;

    @BeforeAll
    static void SetUp() {
        // 创建 web 驱动
        webDriver = new ChromeDriver();
    }

    @AfterAll
    static void TearDown() {
        // 关闭 web 驱动
        webDriver.quit();
    }

    // 获取当前时间戳将截图按照时间保存
    public List<String> getTime() {
        // 文件夹以当天日期保存
        // 截图以当天日期-时间保存
        SimpleDateFormat sim1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sim2 = new SimpleDateFormat("yyyyMMdd-HHmmssSS");
        String dirname = sim1.format(System.currentTimeMillis());
        String filename = sim2.format(System.currentTimeMillis());
        List<String> list = new ArrayList<>();
        list.add(dirname);
        list.add(filename);
        return list;
    }

    // 获取屏幕截图,把所有的用例执行的结果保存下来
    public void getScreenShot(String str) throws IOException {
        List<String> list = getTime();
        String filename = "D:\\bit\\software_testing\\software-testing\\test-blog\\src\\main\\java\\com\\blog\\test" + list.get(0) + "\\" + str + "_" + list.get(1) + ".png";
        File srcfile = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
        // 把屏幕截图生成的文件放到指定的路径
        FileUtils.copyFile(srcfile, new File(filename));
    }
}

🍂常用功能主流程测试

🍁LoginSuccess.csv

admin, admin, http://47.113.217.156:8080/myblog_list.html

🍁RegCases

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openqa.selenium.By;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class RegCases extends InitAndEnd {
    @Order(1)
    @ParameterizedTest
    @CsvSource({"zhaoliu, 123, 123, http://47.113.217.156:8080/login.html"})
    void regSuccess(String username, String password, String againpassword, String login_url) throws InterruptedException, IOException {
        // 打开登录页
        webDriver.get(login_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 找到注册按钮并点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入注册的用户名和密码及确认密码
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#password2")).sendKeys(againpassword);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 点击注册按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        // 点击确认弹窗
        webDriver.switchTo().alert().accept();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 用新注册的账号进行登录
        // 输入账号 zhaoliu
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入密码 123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 刚注册的账号登录后没有文章,验证是否有 “创作” 按钮
        String butt = webDriver.findElement(By.cssSelector("#artListDiv > h3 > a")).getText();
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        getScreenShot(methodName);
        Assertions.assertEquals("创作", butt);
    }
}

🍁BlogCases

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.*;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd {
    /**
     * 登录页:输入正确的账号,错误的密码,登录失败
     */
    @Order(1)
    @ParameterizedTest
    @CsvSource({"admin, 123", "zhangsan, 666"})
    void LoginAbnormal(String username, String password) throws InterruptedException, IOException {
        // 打开登录页
        webDriver.get("http://47.113.217.156:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入账号和密码
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(300);
        //登录失败,出现弹窗
        //获取验证弹窗内容
        String text = webDriver.switchTo().alert().getText();
        String except = "出错了: 登录失败, 请重新操作! 用户名或密码错误!";
        webDriver.switchTo().alert().accept();
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        getScreenShot(methodName);
        Assertions.assertEquals(except, text);
    }

    /**
     * 登录页:输入正确的账号,密码,登录成功
     */
    @Order(2)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url) throws IOException, InterruptedException {
        System.out.println(username + " " +  " " +password + " " +  blog_list_url);
        // 打开登录页
        webDriver.get("http://47.113.217.156:8080/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入账号 admin
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 输入密码 admin
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 点击登录按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);
        // 登录成功,跳转到个人列表页
        // 获取到当前页面 url
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        getScreenShot(methodName);
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 如果 url == http://47.113.217.156:8080/myblog_list.html,测试通过,否则测试不通过
        Assertions.assertEquals(blog_list_url, cur_url);
    }

    /**
     * 个人博客列表页:admin 账户登录后博客数量不为 0
     */
    @Order(3)
    @Test
    void BlogList() throws IOException {
        // 打开个人博客列表页
        webDriver.get("http://47.113.217.156:8080/myblog_list.html");
        // 获取页面上所有博客标题对应的元素
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        // 如果元素数量不为 0,测试通过
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        getScreenShot(methodName);
        Assertions.assertNotEquals(0 ,title_num);
    }

    /**
     * 个人博客列表页:查看全文
     * 博客详情页:
     * url
     * 博客标题
     * 页面 title 是 “博客详情”
     */
    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://47.113.217.156:8080/blog_content.html",
                "博客详情", "URL到页面: 探索网页加载的神秘过程"));
    }

    @Order(4)
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url, String expected_title, String expected_blog_title) throws IOException {
        // 打开个人博客列表页
        webDriver.get("http://47.113.217.156:8080/myblog_list.html");
        // 找到第一篇博客对应的查看全文按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(4)")).click();
        // 获取当前页面 url
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 获取当前页面 title
        String cur_title = webDriver.getTitle();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 获取博客标题
        String cur_blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        getScreenShot(methodName);
        Assertions.assertEquals(expected_title, cur_title);
        Assertions.assertEquals(expected_blog_title, cur_blog_title);
        Assertions.assertTrue(cur_url.contains(expected_url));
    }

    /**
     * 博客编辑页:发布文章
     */
    @Order(5)
    @Test
    void EditBlog() throws InterruptedException, IOException {
        // 打开个人博客列表页
        webDriver.get("http://47.113.217.156:8080/myblog_list.html");
        // 找到写博客按钮,点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 通过 Js 进行输入
        ((JavascriptExecutor) webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        sleep(3000);
        // 点击发布文章按钮
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        sleep(3000);
        // 验证发布成功后的弹窗内容
        String cur_text = webDriver.switchTo().alert().getText();
        String expect_text = "文章添加成功! 是否继续添加文章? ";
        // 点击取消弹窗
        webDriver.switchTo().alert().dismiss();
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        getScreenShot(methodName);
        Assertions.assertEquals(expect_text, cur_text);
    }

    /**
     * 汇总列表页:验证博客成功发布
     * 校验第一篇博客标题
     * 校验第一篇博客时间
     */
    @Order(6)
    @Test
    void BlogInfoChecked() throws IOException {
        webDriver.get("http://47.113.217.156:8080/blog_list.html");
        // 获取第一篇博客标题
        String first_blog_title = webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > div.title")).getText();
        // 获取第一篇博客发布时间
        String first_blog_time = webDriver.findElement(By.xpath("//*[@id=\"artListDiv\"]/div[1]/div[2]")).getText();
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        getScreenShot(methodName);
        // 校验博客标题是不是自动化测试
        Assertions.assertEquals("自动化测试", first_blog_title);
        // 如果时间是 2023-11-18 年发布的,测试通过
        Assertions.assertTrue(first_blog_time.contains("2023-11-18"));
    }

    /**
     * 个人列表页:删除刚刚发布的博客
     */
    @Order(7)
    @Test
    void DeleteBlog() throws InterruptedException, IOException {
        // 打开个人博客列表页面
        webDriver.get("http://47.113.217.156:8080/myblog_list.html");
        // 点击删除按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("#artListDiv > div:nth-child(1) > a:nth-child(6)")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(3000);
        webDriver.switchTo().alert().accept();
        // 删除后博客列表页第一篇博客标题不是 “自动化测试”
        String first_blog_title = webDriver.findElement(By.xpath("//*[@id=\"artListDiv\"]/div[1]/div[1]")).getText();
        // 校验当前博客标题不等于 “自动化测试”
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        getScreenShot(methodName);
        Assertions.assertNotEquals(first_blog_title, "自动化测试");
    }

    //注销
    @Order(8)
    @Test
    void Logout() throws IOException {
        // 打开个人博客列表页面
        //点击注销
        webDriver.get("http://47.113.217.156:8080/myblog_list.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        webDriver.switchTo().alert().accept();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 校验 url 注销后进入登录页
        String cur_url = webDriver.getCurrentUrl();
        String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
        getScreenShot(methodName);
        Assertions.assertEquals("http://47.113.217.156:8080/login.html", cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
}

🍂RunSuite,通过 class 运行测试用例。

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({RegCases.class, BlogCases.class,})
public class RunSuite {
}

3. 测试结果

测试通过,整体的主流程业务操作是没有问题的。

img

测试截图如下:

img

img

img

img

img

img

img

img

img

img

img

五. 发现的问题

🎯手工测试过程中发现的问题

🍂问题描述:

博客汇总页在未登录的情况下,点击“我的”按钮,结果不符合预期。

预期结果:直接跳转到登录页。

实际结果:有时候会出现弹窗提示错误,关闭弹窗后也不能直接跳转到登录页,需要刷新页面才能成功跳转。

🍂原因分析:

问题的根本原因可能在于异步请求的特性和后端拦截器的重定向,异步请求是非阻塞的,即在请求发送的过程中,代码会继续往下执行而不会等待请求完成。

在拦截器中使用response.sendRedirect进行重定向时,实际上是在响应中设置了一个重定向的状态,但对于异步请求而言,这个重定向的状态可能无法被正确处理,导致浏览器不会直接跳转到登录页,因为异步请求的结果是在JavaScript中处理的,而不是在浏览器地址栏中执行的。

这就导致了在异步请求中执行重定向时,可能会产生不确定的行为,因为重定向的结果可能无法按照预期顺序执行。

🍂造成问题的代码定位:

img

img

🍂解决方案:

修改前端代码,通过 JS 在 success 回调中判断返回的 res 中的code,如果是未登录状态,则手动跳转到登录页,以此来规避异步请求中可能产生的问题,确保在未登录时能够及时跳转到登录页。

img

🎯自动化程序编写过程碰到的问题

一些自动化操作是不能在弹窗的情况下完成操作的(比如截图),如果在测试程序执行报unexpected alert open: {Alert text : ...}这种异常,那么就是你没有将弹窗关闭掉,可以使用 accept() 方法确认弹窗或者 dismiss() 取消弹窗后再执行相关操作。

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韵秋梧桐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值