javascript
使用Selenium进行Spring Boot集成测试
Web集成測試允許對Spring Boot應用程序進行集成測試,而無需進行任何模擬。 通過使用@WebIntegrationTest和@SpringApplicationConfiguration我們可以創建加載應用程序并在普通端口上偵聽的測試。 Spring Boot的這一小增加使使用Selenium WebDriver創建集成測試變得更加容易。
測試依賴
我們將要測試的應用程序是一個簡單的Spring Boot / Thymeleaf應用程序,具有spring-boot-starter-web , spring-boot-starter-thymeleaf和spring-boot-starter-actuator依賴性。 請參閱參考資料以獲取GitHub項目的鏈接。
測試依賴項為:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency> <dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>1.5.0</version><scope>test</scope> </dependency> <dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>2.45.0</version><scope>test</scope> </dependency>網絡集成測試
在經典的Spring Test中,使用MockMvc可以創建如下的測試:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration public class HomeControllerClassicTest {@Autowiredprivate WebApplicationContext wac;private MockMvc mockMvc;@Beforepublic void setUp() throws Exception {mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();}@Testpublic void verifiesHomePageLoads() throws Exception {mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isOk());} }@SpringApplicationConfiguration擴展了@ContextConfiguration功能,并加載應用程序上下文以進行集成測試。 要創建沒有@WebIntegrationTest環境的測試,我們應該使用@WebIntegrationTest批注定義測試:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebIntegrationTest(value = "server.port=9000") public class HomeControllerTest {}這將在JUnit測試中啟動完整的應用程序,監聽端口9000 。 有了這樣的測試,我們可以輕松地使用瀏覽器添加Selenium并執行實際的功能測試(除非使用HtmlUnit驅動程序,否則在無頭環境中將無法工作–但這不在本文的討論范圍之內)。
添加硒
將Selenium添加到測試中非常簡單,但是我想實現的目標還不止于此,因此我創建了一個自定義批注,將我的測試標記為Selenium測試。 我還以允許將WebDriver注入測試實例的方式配置了它:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebIntegrationTest(value = "server.port=9000") @SeleniumTest(driver = ChromeDriver.class, baseUrl = "http://localhost:9000") public class HomeControllerTest {@Autowiredprivate WebDriver driver;}@SeleniumTest是一個自定義注釋:
@Documented @Inherited @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @TestExecutionListeners(listeners = SeleniumTestExecutionListener.class,mergeMode = MERGE_WITH_DEFAULTS) public @interface SeleniumTest {Class<? extends WebDriver> driver() default FirefoxDriver.class;String baseUrl() default "http://localhost:8080"; }注釋使用添加了測試執行偵聽器,該偵聽器將創建可在集成測試中使用的WebDriver實例。 TestExecutionListener定義了一個偵聽器API,用于對測試執行事件做出反應。 它可以用于測試。 例如,Spring Test中的示例實現用于支持測試管理的事務或將依賴項注入到測試實例中。
TestExecutionListener
注意:為了更好的可讀性, SeleniumTestExecutionListener的代碼的某些部分被跳過。
SeleniumTestExecutionListener提供了將配置的WebDriver注入測試實例的方法。 該驅動程序實例僅創建一次,并且可以使用@SeleniumTest批注簡單地更改所使用的驅動程序。 最重要的是在Bean Factory中注冊驅動程序。
@Override public void prepareTestInstance(TestContext testContext) throws Exception {ApplicationContext context = testContext.getApplicationContext();if (context instanceof ConfigurableApplicationContext) {SeleniumTest annotation = findAnnotation(testContext.getTestClass(), SeleniumTest.class);webDriver = BeanUtils.instantiate(annotation.driver());// register the bean with bean factory} }在使用WebDriver打開應用程序的每個測試方法的基本URL之前,請執行以下操作:
@Override public void beforeTestMethod(TestContext testContext) throws Exception {SeleniumTest annotation = findAnnotation(testContext.getTestClass(), SeleniumTest.class);webDriver.get(annotation.baseUrl());}另外,在每次失敗時都會生成一個屏幕截圖:
@Override public void afterTestMethod(TestContext testContext) throws Exception {if (testContext.getTestException() == null) {return;}File screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);// do stuff with the screenshot}每次測試后,驅動程序將關閉:
@Override public void afterTestClass(TestContext testContext) throws Exception {if (webDriver != null) {webDriver.quit();} }這只是一個例子。 實現非常簡單。 我們可以擴展注釋和偵聽器的功能。
考試
運行以下測試將啟動Chrome瀏覽器并使用Selenium執行一些簡單的檢查:
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebIntegrationTest(value = "server.port=9000") @SeleniumTest(driver = ChromeDriver.class, baseUrl = "http://localhost:9000") public class HomeControllerTest {@Autowiredprivate WebDriver driver;private HomePage homePage;@Beforepublic void setUp() throws Exception {homePage = PageFactory.initElements(driver, HomePage.class);}@Testpublic void containsActuatorLinks() {homePage.assertThat().hasActuatorLink("autoconfig", "beans", "configprops", "dump", "env", "health", "info", "metrics", "mappings", "trace").hasNoActuatorLink("shutdown");}@Testpublic void failingTest() {homePage.assertThat().hasNoActuatorLink("autoconfig");} }該測試使用帶有自定義AssertJ斷言的簡單頁面對象。 您可以在GitHub中找到完整的源代碼。 請參閱參考資料。
如果發生故障,驅動程序拍攝的屏幕快照將存儲在適當的目錄中。
摘要
@WebIntegrationTest和@SpringApplicationConfiguration批注,可以在常規JUnit測試中對完全加載的Spring Boot應用程序進行集成測試。 使應用程序在測試中運行將為您提供使用Selenium并使用瀏覽器運行功能測試的可能性。 如果將其與Profile和Spring Test的其他功能(例如@Sql , @SqlConfig ) @Sql , @SqlConfig可能會為集成測試提供功能強大而簡單的解決方案。
參考文獻
- 源代碼: https : //github.com/kolorobot/spring-boot-thymeleaf
- Spring Boot測試: http : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-testing
- Spring測試: http : //docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html
翻譯自: https://www.javacodegeeks.com/2015/03/spring-boot-integration-testing-with-selenium.html
總結
以上是生活随笔為你收集整理的使用Selenium进行Spring Boot集成测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 杭州外地驾驶员备案卡怎么办理(杭州外地驾
- 下一篇: linux脚本执行命令(linux脚本执