SpringBoot集成selenium實現(xiàn)自動化測試的代碼工程
1.什么是selenium?
Selenium 是支持web 瀏覽器自動化的一系列工具和[庫] 它提供了擴(kuò)展來模擬用戶與瀏覽器的交互,用于擴(kuò)展瀏覽器分配的分發(fā)[服務(wù)器] 以及用于實現(xiàn)W3C WebDriver 規(guī)范 的基礎(chǔ)結(jié)構(gòu), 該規(guī)范允許您為所有主要Web 瀏覽器編寫可互換的代碼。 Selenium 不僅僅是一個工具或 API, 它還包含許多工具.
WebDriver
如果您開始使用桌面網(wǎng)站測試自動化, 那么您將使用 WebDriver APIs. WebDriver 使用瀏覽器供應(yīng)商提供的瀏覽器自動化 API 來控制瀏覽器和運行測試. 這就像真正的用戶正在操作瀏覽器一樣. 由于 WebDriver 不要求使用應(yīng)用程序代碼編譯其 API, 因此它本質(zhì)上不具有侵入性. 因此, 您測試的應(yīng)用程序與實時推送的應(yīng)用程序相同.
Selenium IDE
Selenium IDE (Integrated Development Environment 集成[開發(fā)]是用來開發(fā) Selenium 測試用例的工具. 這是一個易于使用的 Chrome 和 Firefox 瀏覽器擴(kuò)展, 通常是開發(fā)測試用例最有效率的方式. 它使用現(xiàn)有的 Selenium 命令記錄用戶在瀏覽器中的操作, 參數(shù)由元素的上下文確定. 這不僅節(jié)省了開發(fā)時間, 而且是學(xué)習(xí) Selenium 腳本語法的一種很好的方法.
Grid
Selenium Grid允許您在不同平臺的不同機(jī)器上運行測試用例. 可以本地控制測試用例的操作, 當(dāng)測試用例被觸發(fā)時, 它們由遠(yuǎn)端自動執(zhí)行. 當(dāng)開發(fā)完WebDriver測試之后, 您可能需要在多個瀏覽器和操作系統(tǒng)的組合上運行測試. 這就是 Grid 的用途所在.
2.代碼工程
實驗?zāi)繕?biāo)
- 打開chrome,自動輸入Google網(wǎng)頁并進(jìn)行搜索
- 對搜索結(jié)果截圖并保存
- 關(guān)閉瀏覽器
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springboot-demo</artifactId>
<groupId>com.et</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Selenium</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<selenium.version>3.141.59</selenium.version>
<webdrivermanager.version>4.3.1</webdrivermanager.version>
<testng.version>7.4.0</testng.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<!-- https://github.com/bonigarcia/webdrivermanager-->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>${webdrivermanager.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
測試主類
package com.et.selenium;
import com.et.selenium.page.google.GooglePage;
import com.et.selenium.util.ScreenShotUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.io.IOException;
public class GoogleSearch1Test extends SpringBaseTestNGTest {
@Autowired
private GooglePage googlePage;
@Lazy // only create the object when needed
@Autowired
private ScreenShotUtil screenShotUtil;
@Test
public void GoogleTest() throws IOException, InterruptedException {
this.googlePage.goToGooglePage();
Assert.assertTrue(this.googlePage.isAt());
this.googlePage.getSearchComponent().search("spring boot");
Assert.assertTrue(this.googlePage.getSearchResult().isAt());
Assert.assertTrue(this.googlePage.getSearchResult().getCount() > 2);
System.out.println("Number of Results: " + this.googlePage.getSearchResult().getCount());
// wait 3 seconds
Thread.sleep(3000);
//take screenshot
this.screenShotUtil.takeScreenShot("Test.png");
this.googlePage.close();
}
}
打開google網(wǎng)頁
package com.et.selenium.page.google;
import com.et.selenium.annotation.Page;
import com.et.selenium.page.Base;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
// this is the main page class that uses search componet and search results componet
@Page // using custom annotation created; src/main/java/com/demo/seleniumspring/annotation/Page.java
public class GooglePage extends Base {
@Autowired
private SearchComponent searchComponent;
@Autowired
private SearchResult searchResult;
@Value("${application.url}")
private String url;
//launch website
public void goToGooglePage(){
this.driver.get(url);
}
public SearchComponent getSearchComponent() {
return searchComponent;
}
public SearchResult getSearchResult() {
return searchResult;
}
@Override
public boolean isAt() {
return this.searchComponent.isAt();
}
public void close(){
this.driver.quit();
}
}
搜索“ Spring Boot”關(guān)鍵字
package com.et.selenium.page.google;
import com.et.selenium.annotation.PageFragment;
import com.et.selenium.page.Base;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import java.util.List;
@PageFragment// using custom annotation created; src/main/java/com/demo/seleniumspring/annotation/PageFragment.java
public class SearchComponent extends Base {
@FindBy(name = "q")
private WebElement searchBox;
@FindBy(name="btnK")
private List<WebElement> searchBtns;
public void search(final String keyword) {
this.searchBox.sendKeys(keyword);
this.searchBox.sendKeys(Keys.TAB);
// CLICK first search button
this.searchBtns
.stream()
.filter(e -> e.isDisplayed() && e.isEnabled())
.findFirst()
.ifPresent(WebElement::click);
}
@Override
public boolean isAt() {
return this.wait.until(driver1 -> this.searchBox.isDisplayed());
}
}
搜索結(jié)果截圖
package com.et.selenium.util;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
@Lazy
@Component
public class ScreenShotUtil {
@Autowired
private TakesScreenshot driver;
// location of screenshot file
@Value("${screenshot.path}")
private String path;
public void takeScreenShot(final String imgName) throws IOException {
// takes screenshot as saves to path in app properties file using given imgName ex. test.png
if (System.getenv("CLOUD_RUN_FLAG") == null) {
try {
File sourceFile = this.driver.getScreenshotAs(OutputType.FILE);
File targetfile = new File(path+"/"+imgName);
FileCopyUtils.copy(sourceFile, targetfile);
System.out.println("Saving screenshot to " + path);
} catch (Exception e) {
System.out.println("Something went wrong with screenshot capture" + e);
}
}
}
}
關(guān)閉chrome瀏覽器
this.googlePage.close();
以上只是一些關(guān)鍵代碼,所有代碼請參見下面代碼倉庫
代碼倉庫
3.測試
啟動測試方法GoogleTest,效果如下面動圖

以上就是SpringBoot集成selenium實現(xiàn)自動化測試的代碼工程的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot selenium自動化測試的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Jmeter中的timeshift()函數(shù)獲取當(dāng)前時間進(jìn)行加減
這篇文章主要介紹了Jmeter中的timeshift()函數(shù)獲取當(dāng)前時間進(jìn)行加減,TimeShift(格式,日期,移位,語言環(huán)境,變量)可對日期進(jìn)行移位加減操作,本文給大家詳細(xì)講解,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
利用Intellij Idea連接遠(yuǎn)程服務(wù)器實現(xiàn)遠(yuǎn)程上傳部署功能
大家在使用Intellij Idea開發(fā)程序的時候,是不是需要部署到遠(yuǎn)程SSH服務(wù)器運行呢,當(dāng)然也可以直接在idea軟件內(nèi)容實現(xiàn)配置部署操作,接下來通過本文給大家分享利用Intellij Idea連接遠(yuǎn)程服務(wù)器實現(xiàn)遠(yuǎn)程上傳部署功能,感興趣的朋友跟隨小編一起看看吧2021-05-05
springboot+spring?data?jpa實現(xiàn)新增及批量新增方式
這篇文章主要介紹了springboot+spring?data?jpa實現(xiàn)新增及批量新增方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
本章具體介紹了HashMap、TreeMap兩種集合的基本使用方法和區(qū)別,圖解穿插代碼實現(xiàn)。?JAVA成仙路從基礎(chǔ)開始講,后續(xù)會講到JAVA高級,中間會穿插面試題和項目實戰(zhàn),希望能給大家?guī)韼椭?/div> 2022-03-03最新評論

