5
respostas

Não esta abrindo mais o browser

Depois de fazer a adição de novos testes não esta mais abrindo o browser:

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.browser" is null at br.com.alura.leilao.login.LoginPage.preencheFormularioDeLogin(LoginPage.java:34) at br.com.alura.leilao.leiloes.LeiloesTest.deveriaCadastrarLeilao(LeiloesTest.java:20) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:727) at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:156) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:147) at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:86) at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(InterceptingExecutableInvoker.java:103) at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.lambda$invoke$0(InterceptingExecutableInvoker.java:93) at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37) at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:92) at org.junit.jupiter.engine.execution.InterceptingExecutableInvoker.invoke(InterceptingExecutableInvoker.java:86) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:217) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:213) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:138) at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at

5 respostas

Insira aqui a descrição dessa imagem para ajudar na acessibilidade

Oi!

Manda aqui o código das suas classes LeilaoTest e LoginPage

public class LeiloesTest {

private LoginPage paginaDeLeiloes;

@AfterEach
public void afterEach(){
    this.paginaDeLeiloes.fechar();
}

@Test
public void deveriaCadastrarLeilao() {
    LoginPage paginaDeLogin = new LoginPage();
    paginaDeLogin.preencheFormularioDeLogin("fulano", "pass");
    this.paginaDeLeiloes = paginaDeLogin.efetuaLogin();
    CadastroLeilaoPage paginaDeCadastro = paginaDeLeiloes.carregarFormulario();
}

}

public class LoginPage {

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

public LoginPage(WebDriver browser) {
    // TODO Auto-generated constructor Stub    
    System.setProperty("webdriver.chrome.driver", "webdriver/bin/chromedriver.exe");
    this.browser = new ChromeDriver();
    this.browser.navigate().to(URL_LOGIN);

}

public LoginPage() {
    // TODO Auto-generated constructor stub
}

public void fechar() {
    this.browser.quit();

}

public void preencheFormularioDeLogin(String username, String password) {
    browser.findElement(By.id("username")).sendKeys(username);
    browser.findElement(By.id("password")).sendKeys(password);

}

public LoginPage efetuaLogin() {
    browser.findElement(By.id("login-form")).submit();
    return new LoginPage(browser);


}

public boolean isPaginaDeLogin() {
    return browser.getCurrentUrl().equals("URL_LOGIN");
}

public String getNomeUsuarioLogado() {
    try {
        return browser.findElement(By.id("usuario-logado")).getText();

    }catch (NoSuchElementException e) {
        return null;
    }

}

public void navegaParaPaginaDeLances() {
    this.browser.navigate().to("http://localhost:8080/leiloes/2");

}

public boolean contemTexto(String texto) {
    return browser.getPageSource().contains(texto);
}

public boolean isPaginaDeLoginComDadosInvalidos() {
    return browser.getCurrentUrl().equals(URL_LOGIN + "?error");
}

public CadastroLeilaoPage carregarFormulario() {
    // TODO Auto-generated method stub
    return null;
}

}

O problema está na sua classe LoginPage. Apague o construtor vazio:

public LoginPage() {
    // TODO Auto-generated constructor stub
}

E no outro construtor remova o parâmetro browser:

public LoginPage() {
    System.setProperty("webdriver.chrome.driver", "webdriver/bin/chromedriver.exe");
    this.browser = new ChromeDriver();
    this.browser.navigate().to(URL_LOGIN);
}

Ficaria assim ?

public LoginPage(WebDriver browser) { System.setProperty("webdriver.chrome.driver", "webdriver/bin/chromedriver.exe");

    this.browser.navigate().to(URL_LOGIN);

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