博客系统实现自动化测试

简介: 博客系统实现自动化测试

6c8c508410de4e898007369afa1d5330.png 一、设计博客系统的测试用例

二、利用测试用例进行测试

测试的文件放在maven项目的test文件夹下,需要在之前的maven项目中添加一些自动化测试的依赖:

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.0.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.8.2</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <version>1.8.2</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-reporting -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-reporting</artifactId>
            <version>1.8.2</version>
        </dependency>

测试登录页面

首先定义start方法和close方法,并利用相关注解使其在测试之前和测试之后都执行一次。

    @BeforeAll
    public void start(){
        driver = getDriver();
        driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
        //使用隐式等待渲染页面完成
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
    }
    @AfterAll
    public void close(){
        driver.quit();
    }

界面测试

首先来测试界面的文字信息以及页面的元素布局是否正确。

public class InterfaceTest {
    public static ChromeDriver driver;
    public static ChromeDriver getDriver(){
        if(driver == null){
            synchronized (PrepareTest.class){
                if(driver == null){
                    driver = new ChromeDriver();
                }
            }
        }
        return driver;
    }
    @BeforeAll
    public  static void start(){
        driver = getDriver();
        driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
        //使用隐式等待渲染页面完成
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
    }
    @AfterAll
    public static  void close(){
        driver.quit();
    }
    /**
     * 测试登陆文字
     */
    @Test
    public void testDengLu(){
       String dengLu =  driver.findElement(By.xpath("/html/body/div[2]/div/h3")).getText();
        Assertions.assertEquals(dengLu,"登录");
    }
 
    /**
     * 测试提交按钮的文字
     */
    @Test
    public void testTiJiao(){
        String tiJiao =  driver.findElement(By.xpath("//*[@id=\"submit\"]")).getAttribute("value");
        Assertions.assertEquals(tiJiao,"提交");
    }
 
    /**
     * 测试用户名输入框
     */
    @Test
    public void testUserInput(){
        WebElement webElement =  driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input"));
        Assertions.assertNotNull(webElement);
    }
    /**
     * 测试密码输入框
     */
    @Test
    public void testPasswordInput(){
        WebElement webElement =  driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input"));
        Assertions.assertNotNull(webElement);
    }
    /**
     * 测试提交按钮
     */
    @Test
    public void testSubmit(){
        WebElement webElement =  driver.findElement(By.xpath("//*[@id=\"submit\"]"));
        Assertions.assertNotNull(webElement);
    }
}


功能测试

测试输入正确的用户名和密码、错误的用户名或密码以及空的用户名或密码来查看是否会跳转到博客列表页。

测试正确的用户名和密码:

/**
     * 测试正确登录
     */
    @ParameterizedTest
    @CsvSource(value = {"zhangsan,1234","zhangyi,1234"})
    public void testLoginTrue(String user,String password){
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys(user);
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys(password);
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
       String url = driver.getCurrentUrl();
        Assertions.assertEquals(url,"http://43.143.208.132:8080/blog_system/blog-list.html");
        driver.navigate().back();
    }

 

测试用户名或密码为空:

/**
     * 测试用户名或密码为空
     */
    @ParameterizedTest
    @CsvSource(value = {"zhangyi,",",1234",","})
    public void testLoginNull(String user,String password){
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
        if(user != null){
            driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys(user);
        }
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
        if(password != null){
            driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys(password);
        }
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        String tips = driver.findElement(By.xpath("/html/body")).getText();
        Assertions.assertEquals("用户名或密码不能为空",tips);
        driver.navigate().back();
    }

测试用户名或密码错误:

/**
     * 测试用户名或密码错误
     */
    @ParameterizedTest
    @CsvSource(value = {"zhangyi,6781","liuyy,1234"})
    public void testLoginFalse(String user,String password){
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys(user);
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys(password);
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        String tips = driver.findElement(By.xpath("/html/body")).getText();
        Assertions.assertEquals("用户名或密码错误",tips);
        driver.navigate().back();
    }


测试博客列表页

界面测试

主要测试页面的文字,个人信息以及查看全文按钮是否正常显示。

public class InterfaceTest {
    public static ChromeDriver driver;
    public static ChromeDriver getDriver(){
        if(driver == null){
            synchronized (PrepareTest.class){
                if(driver == null){
                    driver = new ChromeDriver();
                }
            }
        }
        return driver;
    }
    @BeforeAll
    public  static void start(){
        driver = getDriver();
        driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
        //使用隐式等待渲染页面完成
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys("zhangsan");
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys("1234");
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
    }
    @AfterAll
    public static  void close(){
        driver.quit();
    }
    /**
     * 测试个人信息
     */
    @Test
    public void testInfo(){
        String dengLu =  driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/a")).getText();
        Assertions.assertEquals(dengLu,"gitee地址");
        String user = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/h3")).getText();
        Assertions.assertEquals(user,"zhangsan");
    }
 
    /**
     * 测试查看全文按钮的文字
     */
    @Test
    public void testQuanWen(){
        String tiJiao =  driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).getText();
        Assertions.assertEquals("查看全文",tiJiao);
    }
 
    /**
     * 测试个人信息的头像是否正常
     */
    @Test
    public void testUserInput(){
        WebElement webElement =  driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/img"));
        Assertions.assertNotNull(webElement);
    }
    /**
     * 测试文章标题是否正常
     */
    @Test
    public void testPasswordInput(){
        WebElement webElement =  driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]"));
        Assertions.assertNotNull(webElement);
    }
 
}

功能测试

查看全文按钮的功能是否正常。

    /**
     * 查看全文按钮是否能正确跳转
     */
    @Test
    public void testQuanWen(){
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
       String url =  driver.getCurrentUrl();
        Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-detail.html?blogId=5",url);
        driver.navigate().back();
    }


写博客按钮是否正常。

    /**
     * 写博客超链接是否正常
     */
    @Test
    public void testXieBoKe(){
        driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
        String url = driver.getCurrentUrl();
        Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-edit.html",url);
        driver.navigate().back();
    }


测试注销超链接是否正常。

/**
     * 注销超链接是否正常
     */
    @Test
    public void testZhuXiao(){
        driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
        String url = driver.getCurrentUrl();
        Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-login.html",url);
        driver.navigate().back();
    }

测试博客详情页

界面测试

测试博客的详情信息是否都正确显示。

public class InterfaceTest {
    public static ChromeDriver driver;
    public static ChromeDriver getDriver(){
        if(driver == null){
            synchronized (PrepareTest.class){
                if(driver == null){
                    driver = new ChromeDriver();
                }
            }
        }
        return driver;
    }
    @BeforeAll
    public  static void start(){
        driver = getDriver();
        driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
        //使用隐式等待渲染页面完成
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys("zhangsan");
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys("1234");
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
    }
    @AfterAll
    public static  void close(){
        driver.quit();
    }
    /**
     * 测试个人信息
     */
    @Test
    public void testInfo(){
        String dengLu =  driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/a")).getText();
        Assertions.assertEquals(dengLu,"gitee地址");
        String user = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/h3")).getText();
        Assertions.assertEquals(user,"zhanger");
        WebElement webElement =  driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/img"));
        Assertions.assertNotNull(webElement);
    }
    /**
     * 测试文章标题
     */
    @Test
    public void testTitle(){
        String title =  driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/h3")).getText();
        Assertions.assertNotNull(title);
    }
    /**
     * 测试文章发表日期
     */
    @Test
    public void testDate(){
        String date =  driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[1]")).getText();
        Assertions.assertNotNull(date);
    }
    /**
     *测试文章正文
     */
    public void testText(){
        String text =  driver.findElement(By.xpath("//*[@id=\"desc\"]/p")).getText();
        Assertions.assertNotNull(text);
    }
 
}


功能测试

博客详情页的功能测试与博客列表页相似,主要是对超链接进行测试。

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FunctionTest {
    public static ChromeDriver driver;
    public static ChromeDriver getDriver(){
        if(driver == null){
            synchronized (PrepareTest.class){
                if(driver == null){
                    driver = new ChromeDriver();
                }
            }
        }
        return driver;
    }
    @BeforeAll
    public  static void start(){
        driver = getDriver();
        driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
        //使用隐式等待渲染页面完成
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys("yiyi");
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys("1234");
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
    }
    @AfterAll
    public static  void close(){
        driver.quit();
    }
    /**
     * 写博客超链接是否正常
     */
    @Test
    @Order(1)
    public void testXieBoKe(){
        driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
        String url = driver.getCurrentUrl();
        Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-edit.html",url);
        driver.navigate().back();
    }
    /**
     *测试主页超链接是否正常
     */
    @Test
    @Order(2)
    public void testZguYe(){
        driver.findElement(By.xpath("/html/body/div[1]/a[1]")).click();
        String url = driver.getCurrentUrl();
        Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-list.html",url);
        driver.navigate().back();
    }
    /**
     * 注销超链接是否正常
     */
    @Test
    @Order(3)
    public void testZhuXiao(){
        driver.findElement(By.xpath("/html/body/div[1]/a[3]")).click();
        String url = driver.getCurrentUrl();
        Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-login.html",url);
        driver.navigate().back();
    }
 
}

博客编辑页测试

界面测试

查看页面的元素能否正确展示。

public class InterfaceTest {
    public static ChromeDriver driver;
    public static ChromeDriver getDriver(){
        if(driver == null){
            synchronized (PrepareTest.class){
                if(driver == null){
                    driver = new ChromeDriver();
                }
            }
        }
        return driver;
    }
    @BeforeAll
    public  static void start(){
        driver = getDriver();
        driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
        //使用隐式等待渲染页面完成
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input")).sendKeys("zhangsan");
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).clear();
        driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input")).sendKeys("1234");
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        driver.findElement(By.xpath("/html/body/div[1]/a[2]")).click();
    }
    @AfterAll
    public static  void close(){
        driver.quit();
    }
    @Test
    public  void testEdit(){
        WebElement webElement = driver.findElement(By.xpath("//*[@id=\"editor\"]/div[1]/div[6]"));
        Assertions.assertNotNull(webElement);
    }
    @Test
    public  void testFaBu(){
        String str = driver.findElement(By.xpath("//*[@id=\"submit\"]")).getAttribute("value");
        Assertions.assertEquals("发布文章",str);
    }
    @Test
    public void testInputTitle(){
        WebElement webElement = driver.findElement(By.xpath("//*[@id=\"title\"]"));
        Assertions.assertNotNull(webElement);
    }
}

功能测试

测试能否正确发表文章。

 /**
     * 测试发表文章是否正常
     */
    @Test
    public void submit(){
        driver.findElement(By.xpath("//*[@id=\"editor\"]/div[1]/div[6]")).sendKeys("自动化测试的流程:");
        driver.findElement(By.xpath("//*[@id=\"title\"]")).sendKeys("自动化测试");
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        String url = driver.getCurrentUrl();
        Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-list.html",url);
        driver.navigate().back();
    }

标题为空时,无法发表。

/**
     * 标题为空无法发表
     */
    @Test
    public void submitNull(){
        driver.findElement(By.xpath("//*[@id=\"title\"]")).clear();
        driver.findElement(By.xpath("//*[@id=\"submit\"]")).click();
        String url = driver.getCurrentUrl();
        Assertions.assertNotEquals("http://43.143.208.132:8080/blog_system/blog-list.html",url);
        driver.navigate().back();
    }


6c8c508410de4e898007369afa1d5330.png

目录
相关文章
|
8月前
|
前端开发 测试技术 API
测试金字塔:别再只盯着UI自动化了
测试金字塔:别再只盯着UI自动化了
735 116
|
8月前
|
敏捷开发 测试技术 API
测试金字塔:构建高效自动化测试策略的基石
测试金字塔:构建高效自动化测试策略的基石
624 116
|
8月前
|
人工智能 自然语言处理 测试技术
从人工到AI驱动:天猫测试全流程自动化变革实践
天猫技术质量团队探索AI在测试全流程的落地应用,覆盖需求解析、用例生成、数据构造、执行验证等核心环节。通过AI+自然语言驱动,实现测试自动化、可溯化与可管理化,在用例生成、数据构造和执行校验中显著提效,推动测试体系从人工迈向AI全流程自动化,提升效率40%以上,用例覆盖超70%,并构建行业级知识资产沉淀平台。
从人工到AI驱动:天猫测试全流程自动化变革实践
|
8月前
|
人工智能 自然语言处理 JavaScript
利用MCP Server革新软件测试:更智能、更高效的自动化
MCP Server革新软件测试:通过标准化协议让AI实时感知页面结构,实现自然语言驱动、自适应维护的自动化测试,大幅提升效率,降低脚本开发与维护成本,推动测试左移与持续测试落地。
|
8月前
|
测试技术 API 数据库
测试金字塔:构建高效自动化测试策略的基石
测试金字塔:构建高效自动化测试策略的基石
643 114
|
10月前
|
存储 人工智能 算法
AI测试平台实战:深入解析自动化评分和多模型对比评测
在AI技术迅猛发展的今天,测试工程师面临着如何高效评估大模型性能的全新挑战。本文将深入探讨AI测试平台中自动化评分与多模型对比评测的关键技术与实践方法,为测试工程师提供可落地的解决方案。
|
11月前
|
JSON JavaScript 测试技术
用Postman玩转电商API:一键测试+自动化请求教程
Postman 是电商 API 测试的高效工具,涵盖基础配置、自动化测试、环境管理与请求自动化,助你快速提升开发效率。
|
数据采集 算法 数据管理
频标频稳比对测试系统重新定义测量边界
在上海张江实验室的超净间里,一束激光正以每秒 30 万公里的速度穿越真空腔,与原子跃迁频率进行着纳米级的较量。而在千里之外的西安高新区,一台黑色金属机箱内,SYN5609A 型频标比对测量系统正以同样的精度,为这场量子级的时间竞赛提供着基准坐标。这台看似普通的仪器,正在用双混频时差技术,将人类对时间的掌控精度推向新的维度。
|
9月前
|
机器学习/深度学习 人工智能 测试技术
EdgeMark:嵌入式人工智能工具的自动化与基准测试系统——论文阅读
EdgeMark是一个面向嵌入式AI的自动化部署与基准测试系统,支持TensorFlow Lite Micro、Edge Impulse等主流工具,通过模块化架构实现模型生成、优化、转换与部署全流程自动化,并提供跨平台性能对比,助力开发者在资源受限设备上高效选择与部署AI模型。
784 9
EdgeMark:嵌入式人工智能工具的自动化与基准测试系统——论文阅读
|
8月前
|
存储 人工智能 自然语言处理
拔俗AI自动化评价分析系统:让数据说话,让决策更智能
在用户体验为核心的时代,传统评价分析面临效率低、洞察浅等痛点。本文基于阿里云AI与大数据技术,构建“数据-算法-应用”三层智能分析体系,实现多源数据实时接入、情感与主题精准识别、跨模态融合分析及实时预警,助力企业提升运营效率、加速产品迭代、优化服务质量,并已在头部电商平台成功落地,显著提升用户满意度与商业转化。
695 0