我正在开发spring应用程序,并试图测试我的前端以及控制器和前端之间的集成测试,使用selenium驱动程序,参考基于WebDriver的Spring测试。这个博客建议使用MockMvcHtmlUnitDriver作为一个web驱动程序实现,以便在没有部署到服务器的情况下使用spring运行前端测试。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());
}}在这里,您可以看到我正在尝试获取页面标题,但这些结果为null。
测试用例日志:
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就像预期的一样完美。
你们觉得这有什么问题吗?
发布于 2015-04-11 18:09:18
从您的调试中可以看出,您正在使用JSP。
来自您所引用的博客文章的第1部分:
注意:与Spring测试一样,HtmlUnit集成将使用不依赖Servlet容器的模板技术(例如Thymeleaf、Freemarker、流速等)。它不适用于JSP,因为它们依赖Servlet容器。
https://stackoverflow.com/questions/23751500
复制相似问题