目錄
前言:
1.博客前端頁面測試用例圖
2.測試用例的代碼實現(xiàn)
2.1登錄頁面的測試
2.2博客列表頁面的測試
2.3寫博客測試
2.4博客詳情頁面的測試
2.5已發(fā)布博客的標題和時間的測試
2.6注銷用戶的測試
結束語:
前言:
之前小編給大家講解了有關于Selenium和Junit5自動化測試的一些基礎知識,那么下面我們就針對于我們自己做的一個項目來使用Junit來進行一下自動化測試。
博客項目的前后端博客鏈接:前端?http://t.csdn.cn/jZkQd? 后端?http://t.csdn.cn/sN1Uq
博客項目的源碼Gitee鏈接?https://gitee.com/YAUGAOLELE/project
1.博客前端頁面測試用例圖
2.測試用例的代碼實現(xiàn)
代碼側邊的展示:
我們一共創(chuàng)建兩個類一個是BlogCase另一個是InitAndEnd。具體代碼的創(chuàng)建請看下邊。
初始化代碼的實現(xiàn):
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 {
//創(chuàng)建驅動
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);
}
/**
*博客列表頁面的博客數(shù)量不為0
*/
@Order(2)
@Test
void BlogList() {
//打開博客列表頁
webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
//獲取頁面上的所有博客標題對應的元素,如果這個元素的數(shù)量不為0,則認為測試通過
//加上只能等待,如果不加的話可能前端頁面沒有渲染出來,就可能導致獲取元素失敗
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
int title_num = webDriver.findElements(By.cssSelector(".title")).size();
//斷言:如果這個元素的數(shù)量不為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);
}
/**
*博客列表頁面的博客數(shù)量不為0
*/
@Order(2)
@Test
void BlogList() {
//打開博客列表頁
webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
//獲取頁面上的所有博客標題對應的元素,如果這個元素的數(shù)量不為0,則認為測試通過
//加上只能等待,如果不加的話可能前端頁面沒有渲染出來,就可能導致獲取元素失敗
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
int title_num = webDriver.findElements(By.cssSelector(".title")).size();
//斷言:如果這個元素的數(shù)量不為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);
}
/**
*博客列表頁面的博客數(shù)量不為0
*/
@Order(2)
@Test
void BlogList() {
//打開博客列表頁
webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
//獲取頁面上的所有博客標題對應的元素,如果這個元素的數(shù)量不為0,則認為測試通過
//加上只能等待,如果不加的話可能前端頁面沒有渲染出來,就可能導致獲取元素失敗
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
int title_num = webDriver.findElements(By.cssSelector(".title")).size();
//斷言:如果這個元素的數(shù)量不為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已發(fā)布博客的標題和時間的測試
代碼展示:
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);
}
/**
*博客列表頁面的博客數(shù)量不為0
*/
@Order(2)
@Test
void BlogList() {
//打開博客列表頁
webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
//獲取頁面上的所有博客標題對應的元素,如果這個元素的數(shù)量不為0,則認為測試通過
//加上只能等待,如果不加的話可能前端頁面沒有渲染出來,就可能導致獲取元素失敗
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
int title_num = webDriver.findElements(By.cssSelector(".title")).size();
//斷言:如果這個元素的數(shù)量不為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
/**
* 校驗已發(fā)布博客的標題
* 校驗已發(fā)布博客時間
*/
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();
//獲取第一篇博客的發(fā)布時間
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發(fā)布的,測試通過
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);
}
/**
*博客列表頁面的博客數(shù)量不為0
*/
@Order(2)
@Test
void BlogList() {
//打開博客列表頁
webDriver.get("http://43.138.29.216:8080/blog_system/blog_list.html");
//獲取頁面上的所有博客標題對應的元素,如果這個元素的數(shù)量不為0,則認為測試通過
//加上只能等待,如果不加的話可能前端頁面沒有渲染出來,就可能導致獲取元素失敗
webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
int title_num = webDriver.findElements(By.cssSelector(".title")).size();
//斷言:如果這個元素的數(shù)量不為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
/**
* 校驗已發(fā)布博客的標題
* 校驗已發(fā)布博客時間
*/
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();
//獲取第一篇博客的發(fā)布時間
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發(fā)布的,測試通過
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);
}
}
結果展示:
結束語:
好了這節(jié)小編就給大分享到這里啦,希望這節(jié)對大家有關于使用Junit5的自動化測試有一定幫助,想要學習的同學記得關注小編和小編一起學習吧!如果文章中有任何錯誤也歡迎各位大佬及時為小編指點迷津(在此小編先謝過各位大佬啦?。?mark hidden color="red">文章來源:http://www.zghlxwxcb.cn/news/detail-693328.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-693328.html
到了這里,關于博客系統(tǒng)自動化測試項目實戰(zhàn)(測試系列9)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!