1
resposta

Erro na Execução do Teste - "this.browser" is null

Olá,

O test não executa por conta deste erro, consegue me ajudar?

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.browser" is null at leilão.login.TestLogin2.naoDeveriaLogarComDadosInvalidos(TestLogin2.java:49)

public class TestLogin2 {

private static final String URL_LOGIN = "http://localhost:8080/login";
private WebDriver browser;

@BeforeAll
public static void beforeAll() {
    System.setProperty("Webdriver.chrome.driver", "drivers/chromedrive");
}

@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("12345");
        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")));




    }
}
1 resposta

Oi Luiz,

Se a variável browser está null significa que o JUnit não passou pelo método anotado com @BeforeEach.

Talvez seja por conta do import da anotação @Test. Verifica nos imports se ela veio do pacote org.junit.jupiter.api.Test, pois talvez deve ter importado do JUnit antigo, que era org.junit.Test

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software