博客系统实现自动化测试

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

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

目录
相关文章
|
4月前
|
数据采集 算法 数据管理
频标频稳比对测试系统重新定义测量边界
在上海张江实验室的超净间里,一束激光正以每秒 30 万公里的速度穿越真空腔,与原子跃迁频率进行着纳米级的较量。而在千里之外的西安高新区,一台黑色金属机箱内,SYN5609A 型频标比对测量系统正以同样的精度,为这场量子级的时间竞赛提供着基准坐标。这台看似普通的仪器,正在用双混频时差技术,将人类对时间的掌控精度推向新的维度。
|
2月前
|
运维 Prometheus 监控
系统崩了怪运维?别闹了,你该问问有没有自动化!
系统崩了怪运维?别闹了,你该问问有没有自动化!
100 9
|
2月前
|
Java 测试技术 Spring
简单学Spring Boot | 博客项目的测试
本内容介绍了基于Spring Boot的博客项目测试实践,重点在于通过测试驱动开发(TDD)优化服务层代码,提升代码质量和功能可靠性。案例详细展示了如何为PostService类编写测试用例、运行测试并根据反馈优化功能代码,包括两次优化过程。通过TDD流程,确保每项功能经过严格验证,增强代码可维护性与系统稳定性。
133 0
|
8月前
|
人工智能 自然语言处理 数据挖掘
企业数字化转型的关键:如何利用OA系统实现自动化与智能决策
在数字化时代,传统办公系统已无法满足现代企业的需求。通过将RPA(机器人流程自动化)和AI(人工智能)技术与OA系统结合,企业能实现业务流程自动化、智能决策支持,大幅提升工作效率和资源配置优化,推动数字化转型。RPA可自动处理重复任务,如审批、数据同步等;AI则提供智能数据分析、预测和决策支持,两者协同作用,助力财务管理、人力资源管理、项目管理和客户服务等多个领域实现智能化升级。未来,智能化OA系统将进一步提升个性化服务、数据安全和协作能力,成为企业发展的关键驱动力。
|
3月前
|
测试技术 Python
Python接口自动化测试中Mock服务的实施。
总结一下,Mock服务在接口自动化测试中的应用,可以让我们拥有更高的灵活度。而Python的 `unittest.mock`库为我们提供强大的支持。只要我们正确使用Mock服务,那么在任何情况下,无论是接口是否可用,都可以进行准确有效的测试。这样,就大大提高了自动化测试的稳定性和可靠性。
149 0
|
5月前
|
jenkins 测试技术 Shell
利用Apipost轻松实现用户充值系统的API自动化测试
API在现代软件开发中扮演着连接不同系统与模块的关键角色,其测试的重要性日益凸显。传统API测试面临效率低、覆盖率不足及难以融入自动化工作流等问题。Apipost提供了一站式API自动化测试解决方案,支持零代码拖拽编排、全场景覆盖,并可无缝集成CI/CD流程。通过可视化界面,研发与测试人员可基于同一数据源协作,大幅提升效率。同时,Apipost支持动态数据提取、性能压测等功能,满足复杂测试需求。文档还以用户充值系统为例,详细介绍了从创建测试用例到生成报告的全流程,帮助用户快速上手并提升测试质量。
|
6月前
|
存储 人工智能 API
OWL:告别繁琐任务!开源多智能体系统实现自动化协作,效率提升10倍
OWL 是基于 CAMEL-AI 框架开发的多智能体协作系统,通过智能体之间的动态交互实现高效的任务自动化,支持角色分配、任务分解和记忆功能,适用于代码生成、文档撰写、数据分析等多种场景。
1427 13
OWL:告别繁琐任务!开源多智能体系统实现自动化协作,效率提升10倍
|
7月前
|
JSON 前端开发 API
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
295 5
以项目登录接口为例-大前端之开发postman请求接口带token的请求测试-前端开发必学之一-如果要学会联调接口而不是纯写静态前端页面-这个是必学-本文以优雅草蜻蜓Q系统API为实践来演示我们如何带token请求接口-优雅草卓伊凡
|
8月前
|
监控 运维
HTTPS 证书自动化运维:https证书管理系统- 自动化监控
本文介绍如何设置和查看域名或证书监控。步骤1:根据证书状态选择新增域名或证书监控,线上部署推荐域名监控,未部署选择证书监控。步骤2:查询监控记录详情。步骤3:在详情页查看每日定时检测结果或手动测试。
HTTPS 证书自动化运维:https证书管理系统- 自动化监控
|
8月前
|
Linux 持续交付 调度
HTTPS 证书自动化运维:https证书管理系统-自动化部署
本指南介绍如何部署Linux服务器节点。首先复制生成的Linux脚本命令,然后将其粘贴到目标服务器上运行。接着刷新页面查看节点记录,并点击“配置证书”选择证书以自动部署。最后,节点部署完成,后续将自动调度,无需人工干预。
HTTPS 证书自动化运维:https证书管理系统-自动化部署