O código não execulta mesmo com os imports corretos:
Erro exibido: java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.browser" is null
Segue abaixo meu código no formato texto:
public class LoginTest {
private static final String URL_LOGIN = "http://localhost:8080/login";
private WebDriver browser;
@BeforeAll
public static void beforeAll() {
System.setProperty("webdriver.chrome.driver", "/drivers/chromedriver.exe");
}
@BeforeEach
public void beforeEach(){
this.browser = new ChromeDriver();
browser.navigate().to(URL_LOGIN);
}
@AfterEach
public void afterEach(){
this.browser.quit();
}
@Test
public void deveriaEfetuarLoginComDadosValidos() {
browser.findElement(By.id("username")).sendKeys("fulano");
browser.findElement(By.id("password")).sendKeys("pass");
browser.findElement(By.id("login-form")).submit();
Assert.assertFalse(browser.getCurrentUrl().equals(URL_LOGIN));
Assert.assertEquals("fulano", browser.findElement(By.id("usuario-logado")).getText());
}
@Test
public void naoDeveriaLogarComDadosInvalidos() {
browser.findElement(By.id("username")).sendKeys("invalido");
browser.findElement(By.id("password")).sendKeys("123123");
browser.findElement(By.id("login-form")).submit();
Assert.assertTrue(browser.getCurrentUrl().equals("http://localhost:8080/login?error"));
Assert.assertTrue(browser.getPageSource().contains("Usuário e senha inválidos."));
Assert.assertThrows(NoSuchElementException.class, () -> browser.findElement(By.id("usuario-logado")));
}
}