SpringBoot集成selenium實(shí)現(xiàn)自動(dòng)化測(cè)試的代碼工程
1.什么是selenium?
Selenium 是支持web 瀏覽器自動(dòng)化的一系列工具和[庫(kù)] 它提供了擴(kuò)展來(lái)模擬用戶與瀏覽器的交互,用于擴(kuò)展瀏覽器分配的分發(fā)[服務(wù)器] 以及用于實(shí)現(xiàn)W3C WebDriver 規(guī)范 的基礎(chǔ)結(jié)構(gòu), 該規(guī)范允許您為所有主要Web 瀏覽器編寫可互換的代碼。 Selenium 不僅僅是一個(gè)工具或 API, 它還包含許多工具.
WebDriver
如果您開始使用桌面網(wǎng)站測(cè)試自動(dòng)化, 那么您將使用 WebDriver APIs. WebDriver 使用瀏覽器供應(yīng)商提供的瀏覽器自動(dòng)化 API 來(lái)控制瀏覽器和運(yùn)行測(cè)試. 這就像真正的用戶正在操作瀏覽器一樣. 由于 WebDriver 不要求使用應(yīng)用程序代碼編譯其 API, 因此它本質(zhì)上不具有侵入性. 因此, 您測(cè)試的應(yīng)用程序與實(shí)時(shí)推送的應(yīng)用程序相同.
Selenium IDE
Selenium IDE (Integrated Development Environment 集成[開發(fā)]是用來(lái)開發(fā) Selenium 測(cè)試用例的工具. 這是一個(gè)易于使用的 Chrome 和 Firefox 瀏覽器擴(kuò)展, 通常是開發(fā)測(cè)試用例最有效率的方式. 它使用現(xiàn)有的 Selenium 命令記錄用戶在瀏覽器中的操作, 參數(shù)由元素的上下文確定. 這不僅節(jié)省了開發(fā)時(shí)間, 而且是學(xué)習(xí) Selenium 腳本語(yǔ)法的一種很好的方法.
Grid
Selenium Grid允許您在不同平臺(tái)的不同機(jī)器上運(yùn)行測(cè)試用例. 可以本地控制測(cè)試用例的操作, 當(dāng)測(cè)試用例被觸發(fā)時(shí), 它們由遠(yuǎn)端自動(dòng)執(zhí)行. 當(dāng)開發(fā)完WebDriver測(cè)試之后, 您可能需要在多個(gè)瀏覽器和操作系統(tǒng)的組合上運(yùn)行測(cè)試. 這就是 Grid 的用途所在.
2.代碼工程
實(shí)驗(yàn)?zāi)繕?biāo)
- 打開chrome,自動(dòng)輸入Google網(wǎng)頁(yè)并進(jìn)行搜索
- 對(duì)搜索結(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>
測(cè)試主類
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)頁(yè)
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)鍵代碼,所有代碼請(qǐng)參見(jiàn)下面代碼倉(cāng)庫(kù)
代碼倉(cāng)庫(kù)
3.測(cè)試
啟動(dòng)測(cè)試方法GoogleTest,效果如下面動(dòng)圖
以上就是SpringBoot集成selenium實(shí)現(xiàn)自動(dòng)化測(cè)試的代碼工程的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot selenium自動(dòng)化測(cè)試的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java創(chuàng)建對(duì)象(顯式創(chuàng)建和隱含創(chuàng)建)
本文詳細(xì)介紹對(duì)象的創(chuàng)建,在 Java 語(yǔ)言中創(chuàng)建對(duì)象分顯式創(chuàng)建與隱含創(chuàng)建兩種情況,顯式創(chuàng)建和隱含創(chuàng)建,,需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09基于Java SSM實(shí)現(xiàn)在線點(diǎn)餐系統(tǒng)
本項(xiàng)目基于Java SSM框架實(shí)現(xiàn)在線點(diǎn)餐系統(tǒng),主要實(shí)現(xiàn)系統(tǒng)的在線點(diǎn)餐功能。文中的示例代碼講解詳細(xì),感興趣的可以跟隨小編一起學(xué)習(xí)一下2022-02-02Java數(shù)據(jù)結(jié)構(gòu)-HashMap詳解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)-HashMap,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03BufferedInputStream(緩沖輸入流)詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了BufferedInputStream緩沖輸入流的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05