目录
一,项目背景
二,项目功能
三,测试计划
3.1 测试用例的设计
3.2 功能测试
1.正常登录
2.正常写博客测试 (输入完整的标题和内容)
3.发布博客之后跳转到详情页观察是否有刚发布的博客
4.删除博客观察列表的博客数量是否减少
5.修改博客
6.注销,注销成功后回到登陆页面
四,自动化测试
4.1 使用Selenium进行测试的步骤
1.根据项目的核心功能编写测试用例
2.创建自动化项目,根据测试用例书写测试脚本
4.2 编写脚本进行自动化测试
1.登录功能测试
2.博客列表页数量不为0测试
3.添加博客功能测试
4.博客详情页功能测试
5.删除博客功能测试
6.博客注销功能测试
测试代码链接:spring: spring学习中的相关代码测试 - Gitee.com
项目访问链接(已部署):登陆页面(账号:admin 密码:123)
一,项目背景
- BlogPark采用了前后端分离的方法来实现的,同时使用了MySQL、Redis进行数据的存储,并将其部署到了云服务器上。前端有8个页面构成:登陆注册、添加博客、博客编辑、博客详情、热榜博客、全部博客列表、个人博客列表以及个人中心。
 - 在一般的博客项目上进行了许多的扩展: 
  
- 使用邮箱注册以及验证码登录
 - 使用Redis缓存共享session及热榜博客信息
 - 使用线程池定时进行热榜的更新
 - 使用@Async实现异步进行数据鞥带读写操作
 - 使用扫描线程进行敏感词的过滤并记录日志
 - 使用Hutool工具进行密码的加盐机密处理
 - 使用资源路径映射访问本地资源
 - 使用正则表达式对返回给前端的MD格式的数据去除关键字
 - ......
 
 
二,项目功能
针对BlogPark里面的诸多功能,这里对其一些核心功能进行了功能测试以及自动化测试,涉及到的功能有:登录、注销、写博客以及删除博客等功能。
- 登录功能:用户名以及密码已经在后端写入了数据库,没有实现账户注册功能,即:用户名以及密码是已经存在的。登录成功后就会跳转到列表页面。
 - 列表页面:可以在列表页查看有限数量的博客简介,其包括博客标题、发布时间以及内容概要。在左侧可以看到登录的用户以及文章数、分类数等的模块。在右上角有主页、写博客和注销三个功能:主页即列表页,写博客即博客编辑页,注销即注销用户,回到登录页面。
 - 详情页面:在列表页面点击“查看全文”按钮就会跳转到详情页,此时就可以看到该篇博客的完整内容。在右上角同样有主页、写博客、删除和注销四个功能:删除即删除该篇博客,删除之后就会跳转到列表页面,该篇博客就被成功删除。
 - 写博客:在登录之后的任意界面点击“写博客”之后就会进入博客编辑页面,此时就可以进行博客的编写,点击“发布文章”后就可以成功发布文章,此时就会跳转到列表页。
 
三,测试计划
3.1 测试用例的设计

3.2 功能测试
1.正常登录

2.正常写博客测试 (输入完整的标题和内容)

3.发布博客之后跳转到详情页观察是否有刚发布的博客

4.删除博客观察列表的博客数量是否减少

5.修改博客

6.注销,注销成功后回到登陆页面

四,自动化测试
4.1 使用Selenium进行测试的步骤
1.根据项目的核心功能编写测试用例

2.创建自动化项目,根据测试用例书写测试脚本
前提:如果使用的是Java语言进行自动化测试的话,需要创建一个Maven的工程,并添加相关的测试依赖:
<!--        添加selenium依赖-->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.0.0</version>
        </dependency>
 
4.2 编写脚本进行自动化测试
1.登录功能测试
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "/LoginSuccess.csv")
    void LoginSuccess(String username,String password,String blog_list_url) throws InterruptedException {
        //打开博客登录页面
        webDriver.get("http://localhost:8080/login.html");
        Thread.sleep(1000);
        //输入账号admin
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        //输入密码123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        //点击提交按钮跳转到列表页
        webDriver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(1000);
        //获取列表页的url
        String curUrl = webDriver.getCurrentUrl();//获取当前页的url是不是博客列表页,获取到则代表测试通过,否则测试不通过
        Thread.sleep(1000);
        Assertions.assertEquals(blog_list_url,curUrl);
        Thread.sleep(1000);
    } 
LoginSuccess.csv文件内容:admin,123,http://localhost:8080/myblog_list.html 
2.博客列表页数量不为0测试
    @Order(2)
    @Test
    void BlogList() throws InterruptedException {
        //打开博客列表页
        webDriver.get("http://localhost:8080/myblog_list.html");
        Thread.sleep(1000);
        //获取页面上所有博客标题对应的元素
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //如果元素数量不为0,测试通过
        Assertions.assertNotEquals(0,title_num);
    } 
3.添加博客功能测试
    @Order(3)
    @Test
    void EditBlog() throws InterruptedException {
        //找到写博客按钮,点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        Thread.sleep(1000);
        //找到输入框输入标题
        //通过JS脚本进行输入
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        Thread.sleep(1000);
        //点击发布
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        Thread.sleep(1000);
        //使用弹窗操作
        Alert alert = webDriver.switchTo().alert();
        alert.dismiss();
        Thread.sleep(1000);
    } 
4.博客详情页功能测试
    @Order(4)
    @ParameterizedTest
    @MethodSource("generator")
    void BlogDetail(String expected_url,String expected_title,String expected_blogTitle) throws InterruptedException {
        //找到第一篇博客对应的查看全文按钮
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(4)")).click();
        Thread.sleep(1000);
        //获取当前页面url
        String curUrl = webDriver.getCurrentUrl();
        Thread.sleep(1000);
        //获取当前页面title
        String curTitle = webDriver.getTitle();
        Thread.sleep(1000);
        //获取博客标题
        String curBlogTitle = webDriver.findElement(By.cssSelector("#title")).getText();
        Thread.sleep(1000);
        Assertions.assertEquals(expected_title,curTitle);
        Assertions.assertEquals(expected_blogTitle,curBlogTitle);
        if(curUrl.contains(expected_url)) {
            System.out.println("测试通过");
        } else {
            System.out.println(curUrl);
            System.out.println("测试不通过");
        }
    }
    public static Stream<Arguments> generator() {
        return Stream.of(Arguments.arguments("http://localhost:8080/blog_content.html?id=","博客正文","自动化测试"));
    @Order(5)
    @Test
    void BlogInfoChecked() {
        webDriver.get("http://localhost:8080/myblog_list.html");
        // 获取第一篇博客标题
        String first_blog_title = webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > div.title")).getText();
        // 获取第一篇博客发布时间
        String first_blog_time = webDriver.findElement(By.xpath("//*[@id=\"artlist\"]/div[1]/div[2]")).getText();
        // 校验博客标题是不是自动化测试
        Assertions.assertEquals("自动化测试", first_blog_title);
        // 如果时间是2023-6-12年发布的,测试通过
        if(first_blog_time.contains("2023-06-12")) {
            System.out.println("测试通过");
        } else {
            System.out.println("当前时间是:" + first_blog_time);
            System.out.println("测试不通过");
        }
    }
    } 
5.删除博客功能测试
    @Order(6)
    @Test
    void DeleteBlog() throws InterruptedException {
        //打开博客列表页
        webDriver.get("http://localhost:8080/myblog_list.html");
        Thread.sleep(1000);
        //点击删除按钮
        webDriver.findElement(By.cssSelector("#artlist > div:nth-child(1) > a:nth-child(6)")).click();
        Alert alert = webDriver.switchTo().alert();
        alert.accept();
        Thread.sleep(1000);
        Alert alert1 = webDriver.switchTo().alert();
        alert1.accept();
        //博客列表页第一篇博客标题不是"自动化测试"
        String first_blog_title = webDriver.findElement(By.xpath("//*[@id=\"artlist\"]/div[1]/div[1]")).getText();
        Thread.sleep(1000);
        //校验第一篇博客的标题不等于"自动化测试"
        Assertions.assertNotEquals(first_blog_title,"自动化测试");
    } 
6.博客注销功能测试
    @Order(7)
    @Test
    void logout() throws InterruptedException {
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        Alert alert = webDriver.switchTo().alert();
        alert.accept();
        Thread.sleep(1000);
        //进行url校验
        String curUrl = webDriver.getCurrentUrl();
        Thread.sleep(1000);
        Assertions.assertEquals("http://localhost:8080/blog_list.html",curUrl);
    } 
                


















