我的博客系统——自动化测试

        我的博客系统采用前后端分离的方法实现,使用数据库存储相关的数据,通过Tomcat进行项目部署。主要包含用户登录与退出、博客查询与浏览、博客编写与删除、博客数量统计以及强制登陆的功能。

        本文针对我的博客系统项目主要功能,编写测试用例,实现自动化测试,“验证”系统没有bug。

一、项目界面展示

 二、测试用例设计

        首先分析业务流程,和需要测试的业务场景,搭建一个大的测试框架,优先确保用户使用软件的全局功能都能走通,达成软件目标。接着针对各个功能模块设计详细的测试用例,实现从"全局->局部" 去分析测试用例,寻找一条测试的最优路径。

        我的博客系统项目,测试路径为 用户登录——> 博客列表展示——>用户查看具体的博客内容——>写博客&发布博客——>删除博客——>注销账户。针对各个具体的功能模块,拆分测试点,设计出全面、有效的测试用例。

三、自动化测试 

        在进行自动化测试时,将对应的测试用例编写成代码,进行实现。博客系统中设置了强制登陆功能,在进行单个模块功能测试时需要去掉该强制登陆功能,确保每个页面都能正常打开。在最后进行完整的自动化测试时,可以恢复该功能,因为添加了测试顺序,确保每个模块进行测试时都处于登录状态。 

1、测试目的

        利用软件测试工具自动实现全部或部分测试,它是软件测试的一个重要组成部分,能完成许多手工测试无法实现或难以实现的测试。 手工测试的目的在于通过“破坏”发现系统有bug;自动化测试的目的在于“验证”系统没有bug。

2、准备工作

1)创建项目,在 pom.xml 文件中引入依赖

2)代码结构设计

        将整个测试过程分为两大类:第一类:进行初始化和结束操作,第二类:具体的自动化测试过程。InitAndEnd类中包含测试初期,在@BeforeAll进行初始化操作,需要创建webDriver驱动,解析这些自动化测试的代码,解析后把它们发送给浏览器;测试结束时,在@AfterAll中进行关闭浏览器操作。BlogCases类继承InitAndEn类,实现InitAndEn类的方法,以及编写各个页面功能的测试代码并执行。

3、自动化测试实施

1)登录界面测试

        针对登录选择了两组测试数据,一组是正确的用户名和密码,一组是错误的用户名和密码。

 /**
     * 输入错误的账号,密码
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginFail.csv")
    void LoginFail(String username,String password,String blog_list_url) throws InterruptedException {
    sleep(3000);
        //打开博客登陆页面
        webDriver.get("http://127.0.0.1:8080/BlogSystem2/login.html");
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //输入账号李明
        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);
        sleep(2000);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(2000);

        //跳转到博客列表页
        //获取当前页面url,如果url=http://127.0.0.1:8080/BlogSystem2/blog_list.html 测试通过
        String cur_url = webDriver.getCurrentUrl();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //用户账号或密码错误,点击登录,没有进入博客列表页,测试成功
        Assertions.assertNotEquals(blog_list_url,cur_url);

    }
   
 /**
     * 输入正确的账号,密码
     */
    @Order(2)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username,String password,String blog_list_url) throws InterruptedException {
        //打开博客登陆页面
        webDriver.get("http://127.0.0.1:8080/BlogSystem2/login.html");
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //输入账号李明
        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);
        sleep(2000);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(2000);
        //跳转到博客列表页
        //获取当前页面url,如果url=http://127.0.0.1:8080/BlogSystem2/blog_list.html 测试通过
        String cur_url = webDriver.getCurrentUrl();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        Assertions.assertEquals(blog_list_url,cur_url);
        //列表页展示的用户名是李明,测试通过
        String cur_name = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();

        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表页展示用户信息是李明
        Assertions.assertEquals(username,cur_name);

    }

2)博客列表页测试

        通过获取博客列表页的博客标题总数量,与已统计的博客数量进行比对,检验博客列表页是否将所有博客都进行展示。

 /**
     * 博客列表页所有博客数量等于统计数
     */
    @Order(3)
    @Test
    void BlogList(){
        //不能打开博客列表页,因为设置了强制登陆
        //webDriver.get("http://127.0.0.1:8080/BlogSystem2/blog_list.html");
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取页面上所有博客总数
        int title_num = webDriver.findElements(By.cssSelector(".blog")).size();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取已统计的总博客数量
        String blog_num = webDriver.findElement(By.cssSelector(".num")).getText();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果两者数量相等,则测试通过
        Assertions.assertEquals(Integer.parseInt(blog_num),title_num);
    }

3)博客详情页测试

        校验点击查看全文按钮后对应的url,以及查看的博客标题与博客详情页展示的博客标题是否一样,最后点击主页,检验对应的url是否为博客列表页,如果是,则测试通过,说明对应的功能都能正常实现。

/**
     * 1、查看全文
     *2、博客详情页校验:
     * 校验url、博客标题
     * 3、主页按钮校验
     */
    @Order(4)
    @Test
    void BlogDetail() throws InterruptedException {

        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //获取博客列表页第一篇博客的标题
        String title = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]")).getText();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //点击第一篇博客的查看全文按钮
        webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(2000);
        //获取跳转后的页面的url
        String cur_url = webDriver.getCurrentUrl();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

        //获取当前页面的博客标题
        String cur_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div > h2")).getText();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果当前页面的url=博客详情页的url,则测试成功
        Assertions.assertEquals("http://127.0.0.1:8080/BlogSystem2/blog_detail.html?blogId=19",cur_url);
        //如果博客列表页第一篇博客标题与当前页面标题相等,测试成功
        Assertions.assertEquals(title,cur_title);

        //找到主页按钮,点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)")).click();
        //得到跳转后的页面的url
        String last_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:8080/BlogSystem2/blog_list.html",last_url);
        sleep(2000);

    }

4)博客编辑页测试

        分别对写博客和发布博客进行测试,点击写博客,页面跳转到博客编辑页,校验对应的url;找到标题输入框,在输入框输入“自动化测试”。点击发布博客,浏览器保存博客并跳转到博客列表页,获取第一篇博客的标题和发布时间进行校验。

/**
     * 博客编辑页
     * 校验写博客功能:点击写博客功能,校验对应跳转页面
     * 校验发布博客功能:
     * 标题框输入”自动化测试“,点击提交,页面跳转,校验url
     * 校验博客列表页第一篇博客标题与发布博客标题
     * 校验第一篇博客发布时间
     */

    @Order(5)
    @ParameterizedTest
    @CsvFileSource(resources = "blog.csv")
    void BlogEdit(String edit_url,String blog_title,String list_url) throws InterruptedException {
        //找到写博客按钮并点击
        webDriver.findElement(By.xpath("/html/body/div[1]/a[1]")).click();
        sleep(2000);
        //页面跳转到博客编辑页,获取url
        String cur_url = webDriver.getCurrentUrl();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果edit_url就是博客编辑页url,则写博客功能正常,测试通过
        Assertions.assertEquals(edit_url,cur_url);
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);


        //找到标题输入框,输入”自动化测试“
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title\").value=\"自动化测试\"");
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(2000);
       // webDriver.findElement(By.cssSelector("#title")).sendKeys(blog_title);
        //点击发布文章按钮,页面跳转
        webDriver.findElement(By.cssSelector("#submit")).click();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(2000);
        //获取当前页面url
        String url = webDriver.getCurrentUrl();
        //获取当前页面的第一篇博客标题
        String cur_blog_title = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]")).getText();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果点击发布后,获得的url等于博客列表页url;博客列表页第一篇博客的标题等于输入的blog_title,则测试通过
        Assertions.assertEquals(list_url,url);
        Assertions.assertEquals(blog_title,cur_blog_title);

        //获取第一篇博客的发布时间
        String first_blog_time= webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();
        if (first_blog_time.contains("2023-11")){
            System.out.println("测试通过");
        }else {
            System.out.println("当前时间是:"+first_blog_time);
            System.out.println("测试不通过");
        }
    }

5)博客删除功能测试

        删除功能是设置了权限的,如果当前登录的用户是作者本人,则有权进行删除。博客列表页第一篇博客是上个测试过程中用户发布的博客,所以用户有权删除。查看第一篇博客,并点击删除按钮,页面跳转,获取当前列表页第一篇博客标题,如果标题不是“自动化测试”,则测试通过。

/**
     * 删除博客
     * 删除博客列表页第一篇博客(当前登录的用户发布的博客)
     */
    @Order(6)
    @Test
    void DeleteAuthorBlog() throws InterruptedException {
        //点击第一篇博客的查看全文按钮
        webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
        sleep(2000);
        //找到删除按钮进行点击
        webDriver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(2000);
        //得到博客列表页第一篇博客标题
        String first_blog_title = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]")).getText();
        //如果博客标题不是”自动化测试“,则测试通过
        Assertions.assertNotEquals("自动化测试",first_blog_title);
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }

        先获取到博客列表页的博客总数,登录用户为:笑笑, 选择博客列表页最后一篇博客,其对应的作者为李明,点击删除,页面跳转,此时获取当前博客列表页博客总数,若前后数据一致,说明并未删除成功,则测试通过。

/**
     * 删除其他作者的博客
     * 删除失败
     */
    @Order(7)
    @Test()
    void Delete_OtherAuthorBlog() throws InterruptedException {
        //当前登录的用户为笑笑,所有选择不是笑笑写的博客
        //获得当前页面博客总数量
        String blog_num = webDriver.findElement(By.cssSelector(".num")).getText();
        //点击最后一篇博客的查看全文按钮 ,该作者为:李明
        webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[2]/a")).click();
        //找到删除按钮进行点击
        webDriver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(2000);
        //在列表页找到最后一篇博客的标题
        String cur_last_blognum = webDriver.findElement(By.cssSelector(".num")).getText();

        Assertions.assertEquals(blog_num,cur_last_blognum);

    }

6)博客退出功能测试

        点击注销按钮,用户退出,页面跳转到博客登录页,校验对应的url以及提交按钮是否存在,如果符合预期,则测试通过. 

 /**
     * 注销
     */
    @Order(8)
    @Test
    void Logout() throws InterruptedException {
        //找到注销按钮,点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        //显示等待
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        sleep(2000);
        //校验url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://127.0.0.1:8080/BlogSystem2/login.html",cur_url);
        //校验提交按钮 是否存在
        WebElement webElement=  webDriver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull(webElement);
    }

4 、测试结果

四、自动化测试过程展示

自动化测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值