博客系统实现自动化测试

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

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

目录
相关文章
|
5天前
|
Java 测试技术 Python
《手把手教你》系列基础篇(八十)-java+ selenium自动化测试-框架设计基础-TestNG依赖测试-番外篇(详解教程)
【6月更文挑战第21天】本文介绍了TestNG中测试方法的依赖执行顺序。作者通过一个实际的自动化测试场景展示了如何设计测试用例:依次打开百度、搜索“selenium”、再搜索“selenium+java”。代码示例中,`@Test`注解的`dependsOnMethods`属性用于指定方法间的依赖,确保执行顺序。如果不设置依赖,TestNG会按方法名首字母排序执行。通过运行代码,验证了依赖关系的正确性。
27 4
|
7天前
|
机器人 测试技术 持续交付
现代软件测试中的自动化工具与策略
随着软件开发的快速发展,自动化测试成为提高软件质量和加快发布速度的关键。本文探讨了现代软件测试中自动化工具和策略的重要性及其应用,旨在帮助开发团队更有效地实施自动化测试,提升整体开发效率和产品质量。
|
3天前
|
传感器 人工智能 自然语言处理
AI智能家居系统如何实现自动化控制?
【6月更文挑战第23天】AI智能家居系统如何实现自动化控制?
22 7
|
3天前
|
机器学习/深度学习 人工智能 算法
探索软件测试的未来:AI与自动化的交汇点
【6月更文挑战第22天】随着技术的不断进步,软件测试领域正迎来一场革命。人工智能(AI)和自动化技术的结合不仅提高了测试的效率和准确性,还为测试人员开辟了新的职业道路。本文将深入探讨AI和自动化如何改变软件测试的未来,并分析这些变化对测试专业人员的意义。
|
4天前
|
机器学习/深度学习 数据采集 人工智能
探索软件测试的未来:AI与自动化的融合之路
【6月更文挑战第21天】在软件测试领域,人工智能(AI)和自动化技术的结合被广泛认为是未来发展的关键。本文旨在深入探讨这一趋势,分析AI如何增强自动化测试的效率和有效性,同时指出实施过程中可能遇到的挑战和解决方案。通过具体案例分析,文章将展示AI在自动化测试中的应用,以及它如何改变软件测试工程师的工作方式。
28 6
|
6天前
|
Java 测试技术 Python
《手把手教你》系列基础篇(七十九)-java+ selenium自动化测试-框架设计基础-TestNG依赖测试-下篇(详解教程)
【6月更文挑战第20天】TestNG是一个Java测试框架,提供两种测试方法依赖机制:强依赖(所有前置方法成功后才运行)和弱依赖(即使前置方法失败,后置方法仍运行)。文中通过代码示例展示了这两种依赖如何实现,并解释了当依赖方法失败时,如何影响后续方法的执行。文章还包含了TestNG Suite的运行结果截图来辅助说明。
30 8
|
6天前
|
jenkins 测试技术 持续交付
软件测试的自动化:工具与策略的探索
【6月更文挑战第20天】探索软件测试自动化:强调效率、一致性与持续集成。Selenium、Appium、Postman和Jenkins等工具助力自动化测试,策略包括明确测试目标、选对工具、编写优质用例和持续集成。自动化测试优化软件质量,提升团队能力,适应快速发展的行业需求。
|
7天前
|
关系型数据库 MySQL Shell
【权限提升】Linux系统&环境变量&定时任务&权限配置不当&MDUT自动化
【权限提升】Linux系统&环境变量&定时任务&权限配置不当&MDUT自动化
|
7天前
|
机器学习/深度学习 人工智能 运维
探索软件测试的演变:从手动到自动化的旅程
【6月更文挑战第18天】在软件开发的广阔天地中,测试一直是确保质量和可靠性的关键步骤。随着技术的不断进步,软件测试领域经历了从完全手动到部分自动化,再到全面自动化的根本转变。本文将探讨这一演变过程,分析自动化测试带来的优势和挑战,并展望未来可能的发展方向。
|
8天前
|
Java 测试技术 Python
《手把手教你》系列基础篇(七十七)-java+ selenium自动化测试-框架设计基础-TestNG依赖测试- 上篇(详解教程)
【6月更文挑战第18天】TestNG是一个Java测试框架,它允许在测试方法间定义执行顺序和依赖关系。当不指定依赖时,TestNG默认按方法名首字母排序执行。`@Test`注解的`dependsOnMethods`属性用于指定方法依赖,如`test1`依赖`test4`,则实际执行顺序为`test4`、`test2`、`test3`、`test1`。如果依赖的方法失败,后续依赖的方法将被跳过。此外,`dependsOnGroups`属性通过组名指定依赖,方便管理多个相关测试方法。通过`groups`定义方法所属组,然后在其他方法中用`dependsOnGroups`引用这些组。
21 5

热门文章

最新文章