简体   繁体   English

在Spring MVC应用程序中使用Selenium WebDriver进行前端测试

[英]Front-End Testing using Selenium WebDriver in Spring MVC Application

I am working on spring mvc application and trying to test my front end as well as integration test between controller and front end using selenium web driver taking a reference from Spring MVC test with WebDriver . 我正在研究Spring MVC应用程序,并尝试使用硒Web驱动程序测试我的前端以及控制器和前端之间的集成测试,并从带有WebDriver的Spring MVC测试中获取参考。 This blog suggest to use MockMvcHtmlUnitDriver as a web driver implementation in order to run front end test with spring mvc without deploying to server. 该博客建议使用MockMvcHtmlUnitDriver作为Web驱动程序实现,以便在不部署到服务器的情况下使用spring mvc运行前端测试。 The MVC works totally fine and MockMvcHtmlUnitDriver also handles request perfectly, but it doesn't gets the front end page. MVC完全可以正常工作, MockMvcHtmlUnitDriver也可以完美地处理请求,但它没有获取前端页面。

Here, is a code i wrote for a test: 这是我为测试编写的代码:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-context.xml")
@WebAppConfiguration
public class WebDriverDemoTest {

@Autowired
private WebApplicationContext context;

private WebDriver driver;

@Before
public void setup() throws Exception {
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(context).build();

    this.driver = new MockMvcHtmlUnitDriver(mockMvc, true);
}

@Test
public void testFormCreation() {

    this.driver.get("http://localhost:8080/wddemo/demo");
    System.out.println(" - Page Title: " + this.driver.getTitle());
}}

Here, you can see I am trying to get the page title, but these results as null. 在这里,您可以看到我正在尝试获取页面标题,但是这些结果为null。

Log of test case: 测试案例日志:

10:55:59,031 DEBUG TestDispatcherServlet:1218 - Rendering view      [org.springframework.web.servlet.view.JstlView: name 'spring'; URL [/spring.jsp]] in DispatcherServlet with name ''
10:55:59,038 DEBUG JstlView:207 - Forwarding to resource [/spring.jsp] in InternalResourceView 'spring'
10:55:59,039 DEBUG MockRequestDispatcher:67 - MockRequestDispatcher: forwarding to [/spring.jsp]
10:55:59,039 DEBUG TestDispatcherServlet:991 - Successfully completed request
 - Page Title: null

In this log, you can see mvc is handled perfectly, but web driver is not getting page. 在此日志中,您可以看到mvc处理得很好,但是Web驱动程序未获取页面。 However, deploying in server and using HtmlUnitDriver works perfectly as expected. 但是,在服务器中部署并使用HtmlUnitDriver可以按预期完美地工作。

Do you guys see any problem with this? 你们看到这个有什么问题吗?

I can see from your debug that you are using JSPs. 从调试中可以看到您正在使用JSP。

From the part 1 of the blog article you referenced: 在您引用的博客文章的第1部分中

NOTE: Just as with Spring MVC Test, the HtmlUnit integration will work with templating technologies that do not rely on a Servlet Container (ie Thymeleaf, Freemarker, Velocity, etc). 注意:与Spring MVC测试一样,HtmlUnit集成将与不依赖Servlet容器的模板技术一起工作(例如Thymeleaf,Freemarker,Velocity等)。 It does not work with JSPs since they rely on the Servlet Container. 它不适用于JSP,因为它们依赖Servlet容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM