使用Selenium进行Spring Boot集成测试

Web集成测试允许对Spring Boot应用程序进行集成测试,而无需进行任何模拟。 通过使用@WebIntegrationTest@SpringApplicationConfiguration我们可以创建加载应用程序并在普通端口上侦听的测试。 Spring Boot的这一小增加使使用Selenium WebDriver创建集成测试变得更加容易。

测试依赖

我们将要测试的应用程序是一个简单的Spring Boot / Thymeleaf应用程序,具有spring-boot-starter-webspring-boot-starter-thymeleafspring-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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/360490.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

begin ~~~

是时候需要用博客记录自己的code life了~~~ 大二暑假~~~ 需要的tech&#xff1a;php linux j2se ssh asp.net c#winform python htmlcssjsajax svngit转载于:https://www.cnblogs.com/tonyluj/p/3252185.html

在Spring MVC流中使用rx-java Observable

Spring MVC现在已经支持异步请求处理流程了一段时间&#xff0c;该支持内部利用了Tomcat / Jetty等容器的Servlet 3异步支持。 Spring Web Async支持 考虑一下需要花一点时间处理的服务呼叫&#xff0c;该服务呼叫具有延迟&#xff1a; public CompletableFuture<Message&…

宝塔php gd库,宝塔面板安装 EasyImag – 一款最简单图床的安装体验

近日闲逛&#xff0c;发现了一款图床&#xff0c;一款开箱即食的简单图床程序。因为没有数据库所以安装起来也是异常简单&#xff0c;我们看看功能&#xff1a;支持设置图片质量支持仅登录后上传支持QQ截图&#xff0c;剪切板上传支持在线管理(增删改查)支持上传图片转换为指定…

php order by where,无合适where条件过滤时尽量选择order by后的字段以驱动表进行查询...

后台查询语句SELECTo.orders_id,s.orders_status_name,ot.text,af.affiliate_idFROMordersoLEFTJOINorders_totalotON(o.orders_idot.orders_id)LEFTJOINaffilia后台查询语句SELECT o.orders_id, s.orders_status_name, ot.text ,af.affiliate_idFROM orders oLEFT JOIN orders…

零垃圾创建数百万个对象

如性能优化第一规则中所述&#xff0c;垃圾是快速代码的敌人。 通过使用垃圾收集器的服务&#xff0c;它不仅会破坏任何形式的确定性性能&#xff0c;而且我们开始在CPU高速缓存中填充垃圾&#xff0c;这将导致程序的高速缓存未命中。 那么&#xff0c;我们可以在不创建垃圾的…

[算法]单链表专题

如何判断链表环的入口位置&#xff1f; 一个指针从头开始单步走&#xff0c;一个指针从第一次相遇位置开始单步走&#xff0c;再相遇的位置就是环入口&#xff0c;证明如下&#xff1a; 设链表头到环入口位置距离为a&#xff0c;入口位置到第一次相遇位置为b&#xff0c;相遇位…

批准Oracle IDM中的特定Web服务

关于Web服务端点的快速发布&#xff0c;OIM和SOA在与批准有关的场景中使用了Web服务端点- 基本内容&#xff0c;但对于初学者可能有用 。 Oracle IDM与SOA套件集成并利用其提供与批准相关的功能&#xff08;说实话&#xff0c;SOA相当丰富&#xff0c;并且也被用作Web服务连接…

Oracle15001,Oracle11gR2RAC环境DBCA创建数据库报错ORA-15055ORA-15001

在Oracle 11gR2 GridInfrastructure和Database软件安装完成之后&#xff0c;执行DBCA创建数据库到30%的时候报如下错误&#xff0c;点击OK后提示忽略并问题现象:在Oracle 11gR2 GridInfrastructure和Database软件安装完成之后&#xff0c;执行DBCA创建数据库到30%的时候报如下错…

linux 下访问mysql

1&#xff1a;先进到root:/# /usr/local/mysql/bin/2&#xff1a;root:/# mysql -u root -p Enter password: 转载于:https://www.cnblogs.com/gaoyinghui/p/3255148.html

针对新手的Java EE7和Maven项目–第8部分

第1部分 &#xff0c; 第2部分 &#xff0c; 第3部分 &#xff0c; 第4部分 &#xff0c; 第5部分 &#xff0c; 第6部分 &#xff0c; 第7部分 第8部分 自上一篇文章以来&#xff0c;这一系列教程已经有很长时间了。 是时候恢复并在我们的简单项目中添加新功能了。 正…

oracle_home path,ORACLE_HOME迁移后需要设置LD_LIBRARY_PATH环境变量

而设置LD_LIBRARY_PATH后&#xff0c;问题解决&#xff1a;[orat3hpserver2 ~]$ export LD_LIBRARY_PATH$ORACLE_HOME/lib[orat3hpserver2 ~]$ sqlplus / AS sysdbaSQL*Plus: Release 10.2.0.4.0 - Production ON Sun Mar 18 16:10:57 2012Copyright (c) 1982, 2007, Oracle. A…

栈的链式存储及其基本运算

#include <stdio.h> #include <stdlib.h> #define M 10typedef struct stnode {char data;struct stnode *next; }LinkStack;void InitStack(LinkStack *&ls) //初始化栈 {lsNULL; }void PushStack(LinkStack *&ls,char x)//进栈 {LinkStack *p;p(LinkSta…

oracle的导出参数statistic,使用expdp导出时评估所需存储容量大小

我们在使用expdp进行数据导出时&#xff0c;可以事先评估需要存储大小容量(bytes)&#xff0c;Oracle可以通过两种方式进行容量估算:[more]1)、通过数据块数量2)、通过统计信息中记录的内容估算具体是通过制定参数estimate_only和estimate来评估导出的性能参数estimate_onlyy|n…

玩Weld-Probe –一站式查看CDI的所有方面

焊接3.0.0.Alpha4被释放 &#xff0c;而我一直坐在在DevConf.CZ一间会议室。 Jozef Hartinger&#xff08; jozefhartinger &#xff09;或多或少地在几分钟前告诉我有关此最新版本的新功能的信息。 有一个特别的功能真正引起了我的注意&#xff0c;它是新的焊接探针机制。 什…

排列、组合问题(递归)

这里主要介绍字符串排列组合问题,高中数学常见的题目,不用详细介绍&#xff0c;看例子就可以解决问题 "1212" 全排列结果为 1212&#xff0c;1221&#xff0c;1122&#xff0c;2112&#xff0c;2121&#xff0c;2211 组合结果是 1,2,12 我所理解的排列组合结果是…

oracle日志文件大小规则,修改oracle日志文件大小

1、创建2个新的日志组alter database add logfile group 4 (D:\ORACLE\ORADATA\ORADB\REDO04_1.LOG) size 1024k;alter database add logfile group 5 (D:\ORACLE\ORADATA\ORADB\REDO05_1.LOG) size 1024k;2、切换当前日志到新的日志组alter system switch logfile;alter syste…

Java开发工具可以促进编程!

Java开发人员通常尝试找到快速有效地编写高质量Java代码的方法&#xff0c;以使他们的编程工作更轻松。 由于情况发生了变化&#xff0c;因此出现了越来越多的工具。 因此&#xff0c;下面列出了大多数开发人员已经使用&#xff0c;将来使用或一定会使用的有用工具。 该列表包括…

linux cmake装在自己目录下,如何在Linux下安装cmake

全部展开OpenCV 2.2和更高版本需要使用Cmake生成生成文件&#xff0c;因此需要先安装cmake. 还有其他需要先安装cmake的软件1. 在Linux环境中打开Web浏览器&#xff0c;输入URL:mac cmake gui&#xff0c;找到最新版本的位置. 通常&#xff0c;发布了两个版本的开源软件: “源分…

Java Bootstrap:Dropwizard与Spring Boot

如何在尽可能短的时间内使准备就绪的Java应用程序投入生产&#xff1f; 我不是一个早起的人&#xff0c;所以有时需要一些时间才能启动“所有系统”提示。直到不久之前&#xff0c;这对于Java应用程序来说都是正确的&#xff0c;但是与发明贪睡功能不同闹钟&#xff0c;我们将在…

linux 查看libusb版本,linux / libusb获取usb设备路径

我使用libusb来枚举一些usb设备.现在我想获得“设备路径”.我认为这不是usb device-path,因为我没有成功使用谷歌.如果我用linux连接usb设备,我会在dmesg中收到一条消息,这里有一些带有usb温度传感器的“设备路径”的例子(类似于this)&#xff1a;H_301_3直接到usb端口&#xf…