Search code examples
spring-bootwebdriverhtmlunitmockmvc

Unable to locate element using HtmlUnit and WebDriver on Spring Boot


Same title question already exists, however the environment seems to be different.

I'm trying to execute Spring Boot Web MVC test using MockMvc and WebDriver, so I don't need to run Selenium Server.

I created sample project on GitHub as public repository.

I try to impelent the code referencing Spring Framework Document and Spring Boot Document.

The test code works when using MockMvc and HtmlUnit (MessageControllerMockMvcAndHtmlUnitTest.java). However, when I execute the test using MockMvc and WebDriver (MessageControllerMockMvcAndWebDriverTest.java), the error occurs as follows:

java.lang.IllegalStateException: Unable to locate element by name for com.gargoylesoftware.htmlunit.TextPage@~

Does anyone know what is wrong and how to fix it?


Solution

  • I finally solved the problem by myself.

    I just made 2 mistakes.

    1. I misstyped the URL (messages not messagges)

    2. I tried to operate hidden element. So I need to operate by JavaScript.

    • before (wrong)
    this.id.sendKeys(id);
    
    • after (correct)
    JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
    javascriptExecutor.executeScript("document.getElementById('id').value = '" + id + "';");