锋盈数科-知识库 Logo
首页
软件开发
计算机基础
Hello Halo
新手必读
关于本知识库
登录 →
锋盈数科-知识库 Logo
首页 软件开发 计算机基础 Hello Halo 新手必读 关于本知识库
登录
  1. 首页
  2. 软件开发
  3. 自动化测试项目实战

自动化测试项目实战

0
  • 软件开发
  • 发布于 2024-08-19
  • 0 次阅读
黄健
黄健

目录

1.熟悉项目

2.针对核心流程设计手工测试用例

3.手工测试用例转换为自动化测试用例

前置工作

测试工作

登陆界面

博客列表页数量

博客详情页检验

写博客并发布

校验标题,时间

删除博客

注销博客



针对博客系统进行自动化测试

1.熟悉项目

2.针对核心流程设计手工测试用例

3.手工测试用例转换为自动化测试用例

前置工作

初始化工作:@BeforeAll 创建驱动

退出工作:@AfterAll 退出浏览器

public class InitAndEnd {
    static WebDriver webDriver;
    @BeforeAll
    static void SetUp(){
        webDriver = new ChromeDriver();
    }
    @AfterAll
    static void TearDown(){
        webDriver.quit();
    }
}

测试工作

登陆界面

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

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

public class BlogCases extends InitAndEnd{
    /*
    输入正确的账号密码,登陆成功
     */
    @ParameterizedTest
    @CsvFileSource(resources = "LoginCSv.csv")
    void LoginSuccess(String username,String password,String blogList_URL){
        //打开博客登录页面
        webDriver.get("http://localhost:8080/login.html");
//        sleep(3000);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号zhangsan
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        //输入密码123456
        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();
        //跳转到列表页,获取URL,如果url为列表页面的,测试通过,否则不通过
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals(blogList_URL,cur_url);
    }
}

错误情况

博客列表页数量

  /*
    博客数量
     */
    @Test
    void BlogList() {
        //打开博客列表页
        webDriver.get("http://localhost:8080/myblog_list.html");
        //获取页面所有博客标题对应的元素
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //元素数量为0测试不通过
        Assertions.assertNotEquals(0,title_num);
    }

博客详情页检验

url 博客标题 详情页title是否是博客详情页

    /*
       博客详情页检验
       url
       博客标题
       详情页title是否是博客详情页
     */
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String expected_url,String expected_title,String expected_blog_title){
        //找到第一篇博客的查看全文按钮
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        webDriver.findElement(By.xpath("//*[@id=\"articleList\"]/div[1]/a[1]")).click();
        //获取url
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        //获取当前页面title
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_title = webDriver.getTitle();
        //获取博客标题
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_blog_title = webDriver.findElement(By.cssSelector("#title")).getText();
        //断言
        Assertions.assertEquals(expected_url,cur_url);
        Assertions.assertEquals(expected_title,cur_title);
        Assertions.assertEquals(expected_blog_title,cur_blog_title);


    }
    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://localhost:8080/blog_content.html?id=10",
                "博客详情页","Java数据结构基础------泛型、通配符"));
    }

写博客并发布

    @Order(3)
    @Test
    void EditBolg(){
        //点击写博客按钮
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        //输入标题,通过Jscript也能输入
        webDriver.findElement(By.cssSelector("#title")).sendKeys("软件测试");
        //点击发布
        webDriver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
    }

校验标题,时间

    @Order(3)
    @Test
    void BlogInfoChecked() throws InterruptedException {
        webDriver.get("http://localhost:8080/blog_list.html");
        sleep(3000);
        //获取第一篇文章标题
        String cur_blog_title =  webDriver.findElement(By.cssSelector("#articleList > div:nth-child(1) > div.title")).getText();
        //获取博客发布时间
        sleep(3000);
        String cur_blog_time= webDriver.findElement(By.xpath("//*[@id=\"articleList\"]/div[1]/div[2]")).getText();
        //判断
        Assertions.assertEquals("软件测试",cur_blog_title);
        if(cur_blog_time.equals("2023-07-20")){
            System.out.println("测试通过");
        }else {
            System.out.println("测试不通过");
        }
    }

删除博客

并确认是否删除成功,第一篇文章是否已经改变

@Order(3)
    @Test
    void deleteBlog() throws InterruptedException {
        webDriver.get("http://localhost:8080/myblog_list.html");
        //点击第一篇文章删除按钮
        webDriver.findElement(By.xpath("//*[@id=\"articleList\"]/div[1]/a[3]")).click();
        //确认删除
        webDriver.switchTo().alert().accept();
//        webDriver.findElement(By.xpath("//*[@id=\"articleList\"]/div[1]/div[1]")).click();
        //删除成功确认
        sleep(3000);//确认
        webDriver.switchTo().alert().accept();
        //获取第一篇文章是否是Java数据结构基础------泛型、通配符
        String cur_blog_title = webDriver.findElement(By.cssSelector("#articleList > div:nth-child(1) > div.title")).getText();
        //断言
        Assertions.assertEquals("Java数据结构基础------泛型、通配符",cur_blog_title);
    }

注销博客

并判断是否跳转到登陆页面,输入框是否清空

   /*
    注销博客
     */
    @Test
    @Order(3)
    void exit(){
        //注销操作
        webDriver.get("http://localhost:8080/myblog_list.html");
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        webDriver.switchTo().alert().accept();
        //是否跳转到登陆页面
        //获取url
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        //判断
        Assertions.assertEquals("http://localhost:8080/login.html",cur_url);
        //判断输入框是否为空
        String input_name = webDriver.findElement(By.cssSelector("#username")).getText();
        String input_pwd = webDriver.findElement(By.cssSelector("#password")).getText();
        Assertions.assertEquals("",input_name);
        Assertions.assertEquals("",input_pwd);
    }


原文链接: https://blog.csdn.net/chenchenchencl/article/details/131822880

标签: #测试 21
相关文章

万字:支付“核心系统”详解 2024-11-02 15:33

专栏作者:隐墨星辰 \| 主编:陈天宇宙 这篇文章也尝试化繁为简,探寻支付系统的本质,讲清楚在线支付系统最核心的一些概念和设计理念。 虽然支付行业已经过了风头最劲的时光,但跨境支付仍然在蓬勃发展,每年依然有很多新人进入这个行业,这篇文章尝试为这些刚入行的新人提供一点帮助。 文章只介绍一些支付行业十几

资深支付架构师视角:实战从问题定义到代码落地的完整套路 2024-11-02 15:33

前言 今天从一个实际案例入手,介绍站在架构师的角度,如何识别并定义问题,提炼需求,技术方案选型,再到详细设计,最后利用AI的能力协助写出核心的代码,验证与调优。 解决问题存在一定的模式,也可以称之为框架,总结出自己的思考和解题框架,以后再碰到同类型的问题就可以如庖丁解牛一样容易。 很多年前,我写代码

Spring 实现 3 种异步接口 2024-10-18 09:07

大家好,我是苏三~ 如何处理比较耗时的接口? 这题我熟,直接上异步接口,使用 Callable、WebAsyncTask 和 DeferredResult、CompletableFuture等均可实现。 但这些方法有局限性,处理结果仅返回单个值。在某些场景下,如果需要接口异步处理的同时,还持续不断地

重学SpringBoot3-集成Redis(五)之布隆过滤器 2024-10-08 11:24

更多SpringBoot3内容请关注我的专栏:《SpringBoot3》 期待您的点赞👍收藏⭐评论✍ 重学SpringBoot3-集成Redis(五)之布隆过滤器 1. 什么是布隆过滤器? * 基本概念 适用场景 2. 使用 Redis 实现布隆过滤器 * 项目依赖 Redis 配置

设计模式第16讲——迭代器模式(Iterator) 2024-10-08 11:24

一、什么是迭代器模式 迭代器模式是一种行为型设计模式,它提供了一种统一的方式来访问集合对象中的元素,而不是暴露集合内部的表示方式。简单地说,就是将遍历集合的责任封装到一个单独的对象中,我们可以按照特定的方式访问集合中的元素。 二、角色组成 抽象迭代器(Iterator):定义了遍历聚合对象所需的方法

vue2路由和vue3路由区别及原理 2024-10-08 11:24

一、Vue2 与 Vue3 路由的区别 1. 创建路由实例方式的不同 Vue 2 中,通过 Vue.use() 注册路由插件,并通过 new VueRouter() 来创建路由实例。 import Vue from 'vue';import VueRouter from 'vue-router';i

目录

IT 外包服务商

  • 意见投递
  • zyf6619

软件开发应用

主菜单

  • 首页
  • 软件开发
  • 计算机基础
  • Hello Halo
  • 新手必读
  • 关于本知识库
Copyright © 2024 your company All Rights Reserved. Powered by Halo.