繁体   English   中英

如何在 Spring 5 中创建 MockMvc 和 WebDriver

[英]How to create a MockMvc and a WebDriver in Spring 5

我编写了以下简单的测试用例来测试 MockMvc 和 WebDriver:

@RunWith(SpringRunner.class)
@WebAppConfiguration("/src/main/resources")
@ContextConfiguration(classes = {MvcConfig.class})
public class exampleTests {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mvc;
    private WebDriver driver;

    @Before
    public void setup() {
        this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
        this.driver = MockMvcHtmlUnitDriverBuilder.webAppContextSetup(this.context).build();
    }

    @Test
    public void mvcTest() throws Exception {
        mvc.perform(get("/")).andExpect(status().isOk());
    }

    @Test
    public void driverTest() {
        this.driver.get("http://localhost:8080/");
        assertEquals("Please log in", this.driver.findElement(By.xpath("/html/body/form/h1")).getText());
    }

}

如果我执行它,我会得到 java.lang.NoClassDefFoundError: org/openqa/selenium/remote/SessionNotFoundException 这是由 MockMvcHtmlUnitBuilder 在 before 方法中抛出的。 如果我删除引发错误和驱动程序测试的行,则 mvcTest 不会成功,因为它得到 404 而不是 200。

所以接下来我做的是删除 @WebAppConfiguration("/src/main/resources") 和 @ContextConfiguration(classes = {MvcConfig.class}) 注释并添加 @SpringBootTest(classes = Application.class) 注释。 现在 mvcTest 可以工作了,但是如果我再次为驱动程序添加代码,它仍然会抛出 SessionNotFoundException。

所以我的问题是,如何在 Spring 5 中正确创建 MockMvc 和 WebDriver?

我找到了解决我的问题的方法。 它在 Spring 文档中提到您必须安装 org.seleniumhq.selenium:selenium-htmlunit-driver 依赖项。 最新版本是 2.52.0。 我现在所做的是添加相同版本的远程驱动程序:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-htmlunit-driver</artifactId>
        <version>2.52.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-remote-driver</artifactId>
        <version>2.52.0</version>
    </dependency>

我也只使用了 @SpringBootTest 注释,所以我的最终测试类锁定了这样的东西:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class exampleTests {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mvc;
    private WebClient webClient;
    private WebDriver driver;

    @Before
    public void setup() {
        this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
        this.webClient = MockMvcWebClientBuilder.webAppContextSetup(context, springSecurity()).build();
        this.driver = MockMvcHtmlUnitDriverBuilder.webAppContextSetup(this.context, springSecurity()).build();
    }

    @Test
    public void mvcTest() throws Exception {
        mvc.perform(get("/login"))
            .andExpect(status().isOk())
            .andExpect(content().string(containsString("Please log in")));
    }

    @Test
    public void clientTest() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
        HtmlPage loginPage = webClient.getPage("http://localhost:8080/app/");
        List<DomElement> pageList = loginPage.getElementsByTagName("h1");
        DomElement page = pageList.get(0);
        String text = page.getTextContent();
        assertThat(text).isEqualTo("Please log in");
    }

    @Test
    public void driverTest() {
        driver.get("http://localhost:8080/app/");
        assertEquals("Please log in",driver.findElement(By.xpath("/html/body/form/h1")).getText());
    }
}

使用当前的 Spring Boot 和 Spring Boot Test 版本,您只需执行以下操作:

@SpringBootTest
@AutoConfigureMockMvc
public class MyApplicationTests {
    
    @Autowired
    private WebApplicationContext context;
    
    @Autowired
    private MockMvc mockMvc;
    
    @Autowired
    private WebDriver driver;
    
    @Test
    void contextLoads() {
    }
    
    @Test
    public void shouldReturnDefaultMessage() throws Exception {
        this.driver.get("http://localhost:8080/app/");
        assertEquals("Please log in",driver.findElement(By.xpath("/html/body/form/h1")).getText());
        
        or
        
        this.mockMvc.perform(get("/app")).andDo(print()).andExpect(status().isOk())
                .andExpect(content().string("true"));
    }
}

暂无
暂无

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

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