目錄
一、設(shè)計(jì)博客系統(tǒng)的測(cè)試用例
二、利用測(cè)試用例進(jìn)行測(cè)試?
測(cè)試登錄頁(yè)面
界面測(cè)試?
功能測(cè)試
測(cè)試博客列表頁(yè)?
界面測(cè)試
功能測(cè)試?
測(cè)試博客詳情頁(yè)?
界面測(cè)試
功能測(cè)試
博客編輯頁(yè)測(cè)試
界面測(cè)試
功能測(cè)試?
一、設(shè)計(jì)博客系統(tǒng)的測(cè)試用例
二、利用測(cè)試用例進(jìn)行測(cè)試?
測(cè)試的文件放在maven項(xiàng)目的test文件夾下,需要在之前的maven項(xiàng)目中添加一些自動(dòng)化測(cè)試的依賴:
<!-- 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>
測(cè)試登錄頁(yè)面
首先定義start方法和close方法,并利用相關(guān)注解使其在測(cè)試之前和測(cè)試之后都執(zhí)行一次。
@BeforeAll
public void start(){
driver = getDriver();
driver.get("http://43.143.208.132:8080/blog_system/blog-login.html");
//使用隱式等待渲染頁(yè)面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
@AfterAll
public void close(){
driver.quit();
}
界面測(cè)試?
首先來(lái)測(cè)試界面的文字信息以及頁(yè)面的元素布局是否正確。
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");
//使用隱式等待渲染頁(yè)面完成
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 測(cè)試登陸文字
*/
@Test
public void testDengLu(){
String dengLu = driver.findElement(By.xpath("/html/body/div[2]/div/h3")).getText();
Assertions.assertEquals(dengLu,"登錄");
}
/**
* 測(cè)試提交按鈕的文字
*/
@Test
public void testTiJiao(){
String tiJiao = driver.findElement(By.xpath("http://*[@id=\"submit\"]")).getAttribute("value");
Assertions.assertEquals(tiJiao,"提交");
}
/**
* 測(cè)試用戶名輸入框
*/
@Test
public void testUserInput(){
WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div/form/div[1]/span[2]/input"));
Assertions.assertNotNull(webElement);
}
/**
* 測(cè)試密碼輸入框
*/
@Test
public void testPasswordInput(){
WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div/form/div[2]/span[2]/input"));
Assertions.assertNotNull(webElement);
}
/**
* 測(cè)試提交按鈕
*/
@Test
public void testSubmit(){
WebElement webElement = driver.findElement(By.xpath("http://*[@id=\"submit\"]"));
Assertions.assertNotNull(webElement);
}
}
?
功能測(cè)試
測(cè)試輸入正確的用戶名和密碼、錯(cuò)誤的用戶名或密碼以及空的用戶名或密碼來(lái)查看是否會(huì)跳轉(zhuǎn)到博客列表頁(yè)。?
測(cè)試正確的用戶名和密碼:
/**
* 測(cè)試正確登錄
*/
@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("http://*[@id=\"submit\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals(url,"http://43.143.208.132:8080/blog_system/blog-list.html");
driver.navigate().back();
}
??
測(cè)試用戶名或密碼為空:?
/**
* 測(cè)試用戶名或密碼為空
*/
@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("http://*[@id=\"submit\"]")).click();
String tips = driver.findElement(By.xpath("/html/body")).getText();
Assertions.assertEquals("用戶名或密碼不能為空",tips);
driver.navigate().back();
}
測(cè)試用戶名或密碼錯(cuò)誤:
/**
* 測(cè)試用戶名或密碼錯(cuò)誤
*/
@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("http://*[@id=\"submit\"]")).click();
String tips = driver.findElement(By.xpath("/html/body")).getText();
Assertions.assertEquals("用戶名或密碼錯(cuò)誤",tips);
driver.navigate().back();
}
測(cè)試博客列表頁(yè)?
界面測(cè)試
主要測(cè)試頁(yè)面的文字,個(gè)人信息以及查看全文按鈕是否正常顯示。
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");
//使用隱式等待渲染頁(yè)面完成
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("http://*[@id=\"submit\"]")).click();
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 測(cè)試個(gè)人信息
*/
@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");
}
/**
* 測(cè)試查看全文按鈕的文字
*/
@Test
public void testQuanWen(){
String tiJiao = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).getText();
Assertions.assertEquals("查看全文",tiJiao);
}
/**
* 測(cè)試個(gè)人信息的頭像是否正常
*/
@Test
public void testUserInput(){
WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div[1]/div/img"));
Assertions.assertNotNull(webElement);
}
/**
* 測(cè)試文章標(biāo)題是否正常
*/
@Test
public void testPasswordInput(){
WebElement webElement = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/div[1]"));
Assertions.assertNotNull(webElement);
}
}
?
功能測(cè)試?
查看全文按鈕的功能是否正常。
/**
* 查看全文按鈕是否能正確跳轉(zhuǎn)
*/
@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();
}
測(cè)試注銷超鏈接是否正常。?
/**
* 注銷超鏈接是否正常
*/
@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();
}
測(cè)試博客詳情頁(yè)?
界面測(cè)試
測(cè)試博客的詳情信息是否都正確顯示。
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");
//使用隱式等待渲染頁(yè)面完成
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("http://*[@id=\"submit\"]")).click();
driver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
}
@AfterAll
public static void close(){
driver.quit();
}
/**
* 測(cè)試個(gè)人信息
*/
@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);
}
/**
* 測(cè)試文章標(biāo)題
*/
@Test
public void testTitle(){
String title = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/h3")).getText();
Assertions.assertNotNull(title);
}
/**
* 測(cè)試文章發(fā)表日期
*/
@Test
public void testDate(){
String date = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/div[1]")).getText();
Assertions.assertNotNull(date);
}
/**
*測(cè)試文章正文
*/
public void testText(){
String text = driver.findElement(By.xpath("http://*[@id=\"desc\"]/p")).getText();
Assertions.assertNotNull(text);
}
}
功能測(cè)試
博客詳情頁(yè)的功能測(cè)試與博客列表頁(yè)相似,主要是對(duì)超鏈接進(jìn)行測(cè)試。
@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");
//使用隱式等待渲染頁(yè)面完成
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("http://*[@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();
}
/**
*測(cè)試主頁(yè)超鏈接是否正常
*/
@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();
}
}
博客編輯頁(yè)測(cè)試
界面測(cè)試
查看頁(yè)面的元素能否正確展示。
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");
//使用隱式等待渲染頁(yè)面完成
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("http://*[@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("http://*[@id=\"editor\"]/div[1]/div[6]"));
Assertions.assertNotNull(webElement);
}
@Test
public void testFaBu(){
String str = driver.findElement(By.xpath("http://*[@id=\"submit\"]")).getAttribute("value");
Assertions.assertEquals("發(fā)布文章",str);
}
@Test
public void testInputTitle(){
WebElement webElement = driver.findElement(By.xpath("http://*[@id=\"title\"]"));
Assertions.assertNotNull(webElement);
}
}
功能測(cè)試?
?測(cè)試能否正確發(fā)表文章。
/**
* 測(cè)試發(fā)表文章是否正常
*/
@Test
public void submit(){
driver.findElement(By.xpath("http://*[@id=\"editor\"]/div[1]/div[6]")).sendKeys("自動(dòng)化測(cè)試的流程:");
driver.findElement(By.xpath("http://*[@id=\"title\"]")).sendKeys("自動(dòng)化測(cè)試");
driver.findElement(By.xpath("http://*[@id=\"submit\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertEquals("http://43.143.208.132:8080/blog_system/blog-list.html",url);
driver.navigate().back();
}
標(biāo)題為空時(shí),無(wú)法發(fā)表。?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-787380.html
/**
* 標(biāo)題為空無(wú)法發(fā)表
*/
@Test
public void submitNull(){
driver.findElement(By.xpath("http://*[@id=\"title\"]")).clear();
driver.findElement(By.xpath("http://*[@id=\"submit\"]")).click();
String url = driver.getCurrentUrl();
Assertions.assertNotEquals("http://43.143.208.132:8080/blog_system/blog-list.html",url);
driver.navigate().back();
}
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-787380.html
到了這里,關(guān)于博客系統(tǒng)實(shí)現(xiàn)自動(dòng)化測(cè)試的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!