博客系统自动化测试项目实战(测试系列9)

news/2024/5/19 3:19:24 标签: Junit5, 自动化测试

目录

前言:

1.博客前端页面测试用例图

2.测试用例的代码实现

2.1登录页面的测试

2.2博客列表页面的测试

2.3写博客测试

2.4博客详情页面的测试

2.5已发布博客的标题和时间的测试

2.6注销用户的测试

结束语:


前言:

之前小编给大家讲解了有关于Selenium和Junit5自动化测试的一些基础知识,那么下面我们就针对于我们自己做的一个项目来使用Junit来进行一下自动化测试

博客项目的前后端博客链接:前端☞http://t.csdn.cn/jZkQd  后端☞http://t.csdn.cn/sN1Uq

博客项目的源码Gitee链接☞https://gitee.com/YAUGAOLELE/project

1.博客前端页面测试用例图

2.测试用例的代码实现

代码侧边的展示:

我们一共创建两个类一个是BlogCase另一个是InitAndEnd。具体代码的创建请看下边。

初始化代码的实现:

package BlogTest;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

//用来放置初始化的操作以及最后的收尾工作
public class InitAndEnd {
    //创建驱动
    static WebDriver webDriver;
    //初识化操作,打开浏览器
    @BeforeAll
    static void SetUp() {
        webDriver = new ChromeDriver();
    }
    //关闭浏览器
    @AfterAll
    static void TearDown() {
        webDriver.quit();
    }
}

2.1登录页面的测试

代码展示:

package BlogTest;

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

import java.util.concurrent.TimeUnit;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{
    /**
     * 输入正确的账号,密码,登录成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打开博客登录页面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号:zhangsan /lisi
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码:123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到列表页
        //获取到当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表页展示的是zhangsan/lisi
        //用户名是zhangsan测试通过,否则测试不通过
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }
}


结果展示:

2.2博客列表页面的测试

代码展示:

package BlogTest;

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

import java.util.concurrent.TimeUnit;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{
    /**
     * 输入正确的账号,密码,登录成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打开博客登录页面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号:zhangsan /lisi
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码:123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到列表页
        //获取到当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表页展示的是zhangsan/lisi
        //用户名是zhangsan测试通过,否则测试不通过
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }

    /**
     *博客列表页面的博客数量不为0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打开博客列表页
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //获取页面上的所有博客标题对应的元素,如果这个元素的数量不为0,则认为测试通过
        //加上只能等待,如果不加的话可能前端页面没有渲染出来,就可能导致获取元素失败
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //断言:如果这个元素的数量不为0,则认为测试通过
        Assertions.assertNotEquals(0,title_num);
    }
}


结果展示:

2.3写博客测试

代码展示:

package BlogTest;

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

import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{
    /**
     * 输入正确的账号,密码,登录成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打开博客登录页面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号:zhangsan /lisi
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码:123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到列表页
        //获取到当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表页展示的是zhangsan/lisi
        //用户名是zhangsan测试通过,否则测试不通过
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }

    /**
     *博客列表页面的博客数量不为0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打开博客列表页
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //获取页面上的所有博客标题对应的元素,如果这个元素的数量不为0,则认为测试通过
        //加上只能等待,如果不加的话可能前端页面没有渲染出来,就可能导致获取元素失败
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //断言:如果这个元素的数量不为0,则认为测试通过
        Assertions.assertNotEquals(0,title_num);
    }

    /**
     * 写博客
     */
    @Order(3)
    @Test
    void EditBlog() throws InterruptedException {
        //找到写博客按钮,点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //找到对应的输入框,输入对应的标题。
        webDriver.findElement(By.cssSelector("#title-input"));
        //通过JS进行标题输入
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自动化测试\"");
        sleep(3000);
        //点击提交
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //校验
        //获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);

    }
}


结果展示:

2.4博客详情页面的测试

代码展示:

package BlogTest;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{
    private static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments(
                "http://43.138.29.216:8080/blog_system/blog_detail.html",
                "博客详情页",
                "自动化测试"
        ));
    }
    /**
     * 输入正确的账号,密码,登录成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打开博客登录页面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号:zhangsan /lisi
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码:123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到列表页
        //获取到当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表页展示的是zhangsan/lisi
        //用户名是zhangsan测试通过,否则测试不通过
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }

    /**
     *博客列表页面的博客数量不为0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打开博客列表页
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //获取页面上的所有博客标题对应的元素,如果这个元素的数量不为0,则认为测试通过
        //加上只能等待,如果不加的话可能前端页面没有渲染出来,就可能导致获取元素失败
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //断言:如果这个元素的数量不为0,则认为测试通过
        Assertions.assertNotEquals(0,title_num);
    }

    /**
     * 写博客
     */
    @Order(3)
    @Test
    void EditBlog() throws InterruptedException {
        //找到写博客按钮,点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //找到对应的输入框,输入对应的标题。
        webDriver.findElement(By.cssSelector("#title-input"));
        //通过JS进行标题输入
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自动化测试\"");
        sleep(3000);
        //点击提交
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //校验
        //获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);

    }

    /**
     * 博客详情页面的校验
     * 1.校验url
     * 2.校验博客标题
     * 3.页面title是“博客详情页”的测试
     */
    @Order(4)
    @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("/html/body/div[2]/div[2]/div[2]/a")).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("body > div.container > div.container-right > h3")).getText();
        //校验
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        Assertions.assertEquals(expected_url,cur_url);
        Assertions.assertEquals(expected_title,cur_title);
        Assertions.assertEquals(expected_blog_title,cur_blog_title);
        if (cur_url.contains(expected_url)) {
            System.out.println("测试通过");
        }else {
            System.out.println(cur_url);
            System.out.println("测试不通过");
        }
    }
}


结果展示:

2.5已发布博客的标题和时间的测试

代码展示:

package BlogTest;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{
    private static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments(
                "http://43.138.29.216:8080/blog_system/blog_detail.html",
                "博客详情页",
                "自动化测试"
        ));
    }
    /**
     * 输入正确的账号,密码,登录成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打开博客登录页面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号:zhangsan /lisi
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码:123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到列表页
        //获取到当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表页展示的是zhangsan/lisi
        //用户名是zhangsan测试通过,否则测试不通过
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }

    /**
     *博客列表页面的博客数量不为0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打开博客列表页
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //获取页面上的所有博客标题对应的元素,如果这个元素的数量不为0,则认为测试通过
        //加上只能等待,如果不加的话可能前端页面没有渲染出来,就可能导致获取元素失败
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //断言:如果这个元素的数量不为0,则认为测试通过
        Assertions.assertNotEquals(0,title_num);
    }

    /**
     * 写博客
     */
    @Order(3)
    @Test
    void EditBlog() throws InterruptedException {
        //找到写博客按钮,点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //找到对应的输入框,输入对应的标题。
        webDriver.findElement(By.cssSelector("#title-input"));
        //通过JS进行标题输入
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自动化测试\"");
        sleep(3000);
        //点击提交
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //校验
        //获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);

    }

    /**
     * 博客详情页面的校验
     * 1.校验url
     * 2.校验博客标题
     * 3.页面title是“博客详情页”的测试
     */
    @Order(4)
    @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("/html/body/div[2]/div[2]/div[2]/a")).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("body > div.container > div.container-right > h3")).getText();
        //校验
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        Assertions.assertEquals(expected_url,cur_url);
        Assertions.assertEquals(expected_title,cur_title);
        Assertions.assertEquals(expected_blog_title,cur_blog_title);
        if (cur_url.contains(expected_url)) {
            System.out.println("测试通过");
        }else {
            System.out.println(cur_url);
            System.out.println("测试不通过");
        }
    }

    @Order(5)
    @Test
    /**
     * 校验已发布博客的标题
     * 校验已发布博客时间
     */
    void BlogInfoChecked() {
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //获取第一篇博客的标题
        String first_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title")).getText();
        //获取第一篇博客的发布时间
        String first_blog_time = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();
        //校验博客标题是不是自动化测试
        Assertions.assertEquals("自动化测试",first_blog_title);
        //如果是2023-09-03发布的,测试通过
        if (first_blog_time.contains("2023-09-03")) {
            System.out.println("测试通过");
        }else {
            System.out.println("当前时间是:" + first_blog_time);
            System.out.println("测试不通过");
        }
    }
    
}


结果展示: 

2.6注销用户的测试

代码展示:

package BlogTest;

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCase extends InitAndEnd{
    private static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments(
                "http://43.138.29.216:8080/blog_system/blog_detail.html",
                "博客详情页",
                "自动化测试"
        ));
    }
    /**
     * 输入正确的账号,密码,登录成功
     */
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password, String blog_list_url){
        System.out.println(username + password + blog_list_url);
        //打开博客登录页面
        webDriver.get("http://43.138.29.216:8080/blog_system/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入账号:zhangsan /lisi
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //输入密码:123
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //点击提交按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //跳转到列表页
        //获取到当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //如果url=http://43.138.29.216:8080/blog_system/blog_list.html,测试通过,否则测试不通过
        Assertions.assertEquals(blog_list_url,cur_url);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        //列表页展示的是zhangsan/lisi
        //用户名是zhangsan测试通过,否则测试不通过
        String cur_admin = webDriver.findElement(By.cssSelector("body > div.container > div.container-left > div > h3")).getText();
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertEquals(username,cur_admin);
    }

    /**
     *博客列表页面的博客数量不为0
     */
    @Order(2)
    @Test
    void BlogList() {
        //打开博客列表页
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //获取页面上的所有博客标题对应的元素,如果这个元素的数量不为0,则认为测试通过
        //加上只能等待,如果不加的话可能前端页面没有渲染出来,就可能导致获取元素失败
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        int title_num = webDriver.findElements(By.cssSelector(".title")).size();
        //断言:如果这个元素的数量不为0,则认为测试通过
        Assertions.assertNotEquals(0,title_num);
    }

    /**
     * 写博客
     */
    @Order(3)
    @Test
    void EditBlog() throws InterruptedException {
        //找到写博客按钮,点击
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        //找到对应的输入框,输入对应的标题。
        webDriver.findElement(By.cssSelector("#title-input"));
        //通过JS进行标题输入
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        ((JavascriptExecutor)webDriver).executeScript("document.getElementById(\"title-input\").value=\"自动化测试\"");
        sleep(3000);
        //点击提交
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        //校验
        //获取当前页面的url
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.29.216:8080/blog_system/blog_list.html",cur_url);

    }

    /**
     * 博客详情页面的校验
     * 1.校验url
     * 2.校验博客标题
     * 3.页面title是“博客详情页”的测试
     */
    @Order(4)
    @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("/html/body/div[2]/div[2]/div[2]/a")).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("body > div.container > div.container-right > h3")).getText();
        //校验
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
//        Assertions.assertEquals(expected_url,cur_url);
        Assertions.assertEquals(expected_title,cur_title);
        Assertions.assertEquals(expected_blog_title,cur_blog_title);
        if (cur_url.contains(expected_url)) {
            System.out.println("测试通过");
        }else {
            System.out.println(cur_url);
            System.out.println("测试不通过");
        }
    }

    @Order(5)
    @Test
    /**
     * 校验已发布博客的标题
     * 校验已发布博客时间
     */
    void BlogInfoChecked() {
        webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
        //获取第一篇博客的标题
        String first_blog_title = webDriver.findElement(By.cssSelector("body > div.container > div.container-right > div:nth-child(1) > div.title")).getText();
        //获取第一篇博客的发布时间
        String first_blog_time = webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[2]")).getText();
        //校验博客标题是不是自动化测试
        Assertions.assertEquals("自动化测试",first_blog_title);
        //如果是2023-09-03发布的,测试通过
        if (first_blog_time.contains("2023-09-03")) {
            System.out.println("测试通过");
        }else {
            System.out.println("当前时间是:" + first_blog_time);
            System.out.println("测试不通过");
        }
    }

    /**
     * 注销
     */
    @Order(6)
    @Test
    void Logout() {
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        //校验url(登录)
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        Assertions.assertEquals("http://43.138.29.216:8080/blog_system/login.html",cur_url);
        //校验提交按钮
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        WebElement webElement = webDriver.findElement(By.cssSelector("#submit"));
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        Assertions.assertNotNull(webElement);
    }
}


结果展示:

结束语:

好了这节小编就给大分享到这里啦,希望这节对大家有关于使用Junit5自动化测试有一定帮助,想要学习的同学记得关注小编和小编一起学习吧!如果文章中有任何错误也欢迎各位大佬及时为小编指点迷津(在此小编先谢过各位大佬啦!)


http://www.niftyadmin.cn/n/4997590.html

相关文章

docker安装redis,并挂载配置文件

1&#xff1a;下载镜像&#xff0c;不添加版本 默认下载最新的 docker pull redis下载成功后如图所示 2&#xff1a;下载redis配置文件&#xff0c;我是在docker中下载的&#xff0c;也可以使用文件上传工具将配置文件上传到自己指定的目录。 首先需要安装wget&#xff0c;否…

大学生攻略:正确的购买和使用你的电脑

笔者是计算机专业在读大学生&#xff0c;从小学开始接触电脑&#xff0c;进行过各种操作(更换硬件维修&#xff0c;换系统&#xff0c;系统命令行&#xff0c;管理员权限&#xff0c;无视风险继续安装&#xff0c;没有这条 )&#xff0c;相对大学生有一定参考价值。 购买 1.买…

MyBatis-Plus —— 初窥门径

前言 在前面的文章中荔枝梳理了MyBatis及相关的操作&#xff0c;作为MyBatis的增强工具&#xff0c;MyBatis-Plus无需再在xml中写sql语句&#xff0c;在这篇文章中荔枝将梳理MyBatis-Plus的基础知识并基于SpringBoot梳理MyBatis-Plus给出的两个接口&#xff1a;BaseMapper和ISe…

无涯教程-JavaScript - CUBERANKEDMEMBER函数

描述 CUBERANKEDMEMBER函数返回集合中的第n个或排序的成员。 使用此功能可返回一组中的一个或多个元素,如销售业绩最好的人或前十名的学生。 语法 CUBERANKEDMEMBER (connection, set_expression, rank, [caption])争论 Argument描述Required/OptionalconnectionThe name …

深度优先遍历(Depth-First Search, DFS)和广度优先遍历(Breadth-First Search, BFS)

深度优先遍历&#xff08;DFS&#xff09; 问题1&#xff1a;什么是深度优先遍历&#xff08;DFS&#xff09;&#xff1f; 答案&#xff1a; 深度优先遍历是一种用于遍历树或图的算法&#xff0c;它从根节点&#xff08;或其他起始节点&#xff09;开始&#xff0c;首先探索…

git 查看当前分支最近一次提交的commit SHA

获取当前分支最近一次commit SHA &#xff08;长度为40个16进制数字的字符&#xff09;命令如下&#xff1a; git rev-parse HEAD 获取简写&#xff08;短&#xff09; commit SHA git rev-parse --short HEAD

【若依框架RuoYi-Vue-Plus 图片回显不显示问题,OSS文件上传或者本地上传】

一、问题 1.设计表 product&#xff08;商品表&#xff09; 有 id &#xff08;id&#xff09; name&#xff08;商品名&#xff09;icon&#xff08;图标&#xff09; 2.使用若依代码生成功能&#xff0c;导入product表&#xff0c;代码生成。 3.将生成的代码导入到项目中得到…

java并发编程 ReentrantReadWriteLock详解

文章目录 1 ReentrantReadWriteLock是什么&#xff1f;2 相关文章3 示例2 ReentrantReadWriteLock结构3 写锁 WriteLock实现原理3.1 WriteLock数据结构 4 读锁 ReadLock实现原理4.1 ReadLock数据结构 5 ReentrantReadWriteLock.Sync实现原理5.1 Sync数据结构5.2 ReadLock详解5.…