Java 在SpringMVC应用程序中使用SeleniumWebDriver进行前端测试

Java 在SpringMVC应用程序中使用SeleniumWebDriver进行前端测试,java,spring-mvc,selenium-webdriver,tdd,Java,Spring Mvc,Selenium Webdriver,Tdd,我正在开发SpringMVC应用程序,并试图测试我的前端,以及使用SeleniumWeb驱动程序测试控制器和前端之间的集成,这是我的参考。本博客建议使用MockMvcHtmlUnitDriver作为web驱动程序实现,以便在不部署到服务器的情况下使用SpringMVC运行前端测试。MVC工作得非常好,MockMvcHtmlUnitDriver也能完美地处理请求,但它无法获得前端页面 下面是我为测试编写的代码: @RunWith(SpringJUnit4ClassRunner.class) @Co

我正在开发SpringMVC应用程序,并试图测试我的前端,以及使用SeleniumWeb驱动程序测试控制器和前端之间的集成,这是我的参考。本博客建议使用MockMvcHtmlUnitDriver作为web驱动程序实现,以便在不部署到服务器的情况下使用SpringMVC运行前端测试。MVC工作得非常好,MockMvcHtmlUnitDriver也能完美地处理请求,但它无法获得前端页面

下面是我为测试编写的代码:

@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());
}}
在这里,您可以看到我正在尝试获取页面标题,但这些结果为空

测试用例日志:

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
在这个日志中,您可以看到mvc得到了完美的处理,但web驱动程序并没有获得页面。然而,在服务器中部署并使用HtmlUnitDriver可以像预期的那样完美地工作


你们觉得这有什么问题吗?

我可以从你们的调试中看出你们在使用JSP

从您引用的博客文章的第页:

注意:与SpringMVC测试一样,HtmlUnit集成将使用不依赖Servlet容器(即Thymeleaf、Freemarker、Velocity等)的模板技术。它不适用于JSP,因为它们依赖于Servlet容器


谢谢Jim,我不知道Servlet容器。