目录
前言:
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;//初识化操作,打开浏览器@BeforeAllstatic void SetUp() {webDriver = new ChromeDriver();}//关闭浏览器@AfterAllstatic 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 /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString 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 /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString 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)@Testvoid 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 /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString 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)@Testvoid 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)@Testvoid 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);//校验//获取当前页面的urlString 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 /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString 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)@Testvoid 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)@Testvoid 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);//校验//获取当前页面的urlString 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();//获取当前页面的urlwebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_url = webDriver.getCurrentUrl();//获取当前页面的titlewebDriver.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 /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString 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)@Testvoid 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)@Testvoid 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);//校验//获取当前页面的urlString 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();//获取当前页面的urlwebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_url = webDriver.getCurrentUrl();//获取当前页面的titlewebDriver.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 /lisiwebDriver.findElement(By.cssSelector("#username")).sendKeys(username);webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);//输入密码:123webDriver.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);//跳转到列表页//获取到当前页面的urlString 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)@Testvoid 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)@Testvoid 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);//校验//获取当前页面的urlString 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();//获取当前页面的urlwebDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);String cur_url = webDriver.getCurrentUrl();//获取当前页面的titlewebDriver.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)@Testvoid 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的自动化测试有一定帮助,想要学习的同学记得关注小编和小编一起学习吧!如果文章中有任何错误也欢迎各位大佬及时为小编指点迷津(在此小编先谢过各位大佬啦!)