I developed a web application using SpringMVC with Spring Boot. Now I'm testing my web application with Selenium. For this, I'm using TestNG.
The problem I'm currently having is with the assignment of the port. When using @LocalServerPort, it always assignes the port with 0. Therefore, the web driver can't get to the url.
This is the test for my login page:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TrabajoParcialApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DatabaseSetup("classpath:test-data.xml")
@TestExecutionListeners({
DbUnitTestExecutionListener.class
})
public class LoginTest {
@LocalServerPort
private int port;
private String url;
private LoginPage loginPage;
private WebDriver driver;
@BeforeTest
public void setUp() {
System.out.println(port);
driver = WebDriverFactory.getWebDriver("CHROME");
url = "http://localhost:" + port;
System.out.println(url);
loginPage = new LoginPage(driver);
}
@Test
public void loginWithCorrectCredentialsShowsDashboard() {
assertTrue(true);
}
@Test
public void loginWithIncorrectCredentialsShowsErrorMessage() throws Exception {
driver.get(url);
String wrongUsername = "wrong";
String wrongPassword = "wrong";
loginPage.loginAs(wrongUsername, wrongPassword);
}
@AfterTest
public void tearDown() {
driver.quit();
}
}
Then, the console output is the following:
0
Starting ChromeDriver 2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a) on port 32634
Only local connections are allowed.
http://localhost:0
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"username"}
How can I solve this?
Edit: I'm using Spring Boot 1.5.6
Here is the link to the repository: https://github.com/GianfrancoMS/SpringISW