博客系统实现自动化测试

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

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

目录
相关文章
|
15天前
|
机器学习/深度学习 人工智能 算法
探索软件测试的未来:自动化与人工智能的融合
在数字时代的浪潮中,软件测试作为保障软件质量的重要环节,正面临前所未有的挑战与机遇。本文深入探讨了自动化测试和人工智能技术如何共同推动软件测试领域的革新,以及这一变革对测试人员技能要求的影响。通过分析当前行业趋势、案例研究和技术发展,文章旨在为读者揭示未来软件测试的可能走向,并激发对持续学习和适应新技术重要性的认识。
31 7
|
3天前
|
人工智能 自然语言处理 测试技术
基于LangChain手工测试用例转接口自动化测试生成工具
本文介绍利用大语言模型自动生成接口自动化测试用例的方法。首先展示传统通过HAR文件生成测试用例的方式及其局限性,随后提出结合自然语言描述的测试需求与HAR文件来生成更全面的测试脚本。通过LangChain框架,设计特定的提示词模板,使模型能够解析测试需求文档和HAR文件中的接口信息,并据此生成Python pytest测试脚本。示例展示了正常请求、非法请求及无效路径三种测试场景的自动化脚本生成过程。最终,整合流程形成完整代码实现,帮助读者理解如何利用大模型提高测试效率和质量。
14 2
|
12天前
|
人工智能 搜索推荐 测试技术
软件测试的未来:自动化与人工智能的融合
随着技术的快速发展,软件测试领域也迎来了翻天覆地的变化。传统的手动测试方法正在逐渐被自动化测试所取代,而人工智能(AI)的加入则为软件测试带来了新的革命。本文将探讨自动化测试的现状、挑战以及AI如何改变软件测试的未来。
|
8天前
|
人工智能 安全 物联网
智能家居系统的未来:从自动化到人工智能的演进
随着科技的迅速发展,智能家居系统正经历着一场革命性的变革。本文将深入探讨智能家居技术的最新进展,包括物联网(IoT)、大数据和人工智能(AI)如何塑造未来家居生活。通过分析当前技术趋势,本文旨在揭示智能家居系统如何从简单的自动化向智能化转变,并预测未来可能的发展方向。
22 3
|
13天前
|
jenkins 测试技术 持续交付
探索软件测试的新天地:自动化与持续集成的融合
【8月更文挑战第6天】在这篇文章中,我们深入探讨了软件测试领域内两大趋势——自动化测试和持续集成——如何相互融合,共同推动软件开发过程的效率和质量。文章将通过实际案例分析,展示这一融合对现代软件开发实践的影响,并讨论其对测试人员角色的深远意义。
33 9
|
9天前
|
机器学习/深度学习 人工智能 自然语言处理
软件测试的未来之路:自动化与智能化的融合之旅
随着技术的飞速发展,软件测试领域正经历着一场革命。传统的手动测试方法逐渐让位于更加高效、智能的自动化测试策略。本文将探讨自动化测试工具的演进,以及人工智能如何赋能未来的软件测试实践,提升测试效率和准确性。我们将通过实例分析,了解自动化测试工具的现状,探索AI技术在测试中的应用,并展望未来软件测试的趋势。
21 2
|
12天前
|
机器学习/深度学习 人工智能 边缘计算
软件测试的未来:自动化与AI的融合
在数字化时代的浪潮中,软件测试作为确保产品质量的关键步骤,正经历着前所未有的变革。随着技术的发展,自动化测试和人工智能(AI)的结合不仅提升了测试的效率和准确性,还极大地扩展了测试的范围和深度。本文将探讨自动化测试的最新趋势、AI如何重塑测试流程,以及未来软件测试可能达到的新高度。
|
14天前
|
机器学习/深度学习 人工智能 自然语言处理
探索软件测试的未来:AI与自动化的交汇
随着人工智能(AI)和机器学习技术的迅猛发展,软件测试领域正迎来一场革命。本文将探讨如何通过结合AI与自动化技术来提升测试效率、准确性,以及预测潜在缺陷的能力。我们将深入分析AI在软件测试中的应用案例,并讨论其对测试工程师角色的潜在影响。
36 5
|
12天前
|
机器学习/深度学习 人工智能 安全
智能家居系统的未来:从自动化到自主
在这篇文章中,我们将探讨智能家居系统如何从简单的自动化工具演变为能够自主决策的智能平台。通过分析当前技术趋势和未来预测,我们揭示了智能家居技术背后的创新动力,并讨论了这一变革如何影响我们的日常生活。
28 3
|
14天前
|
机器学习/深度学习 人工智能 自然语言处理
探索软件测试的未来:自动化与人工智能的融合
在数字化时代,软件测试作为确保产品质量和用户满意度的关键步骤,正经历着前所未有的变革。本文将深入探讨自动化测试的最新趋势,以及人工智能如何重塑测试流程,提升效率并减少人为错误。通过分析当前的挑战和未来的机遇,我们将揭示这一领域即将迎来的创新浪潮。