Nullpointerexception when running a Selenium Webdriver test

Asked

Viewed 40 times

0

I have a certain difficulty in instantiating the driver in another class. What happens is the following:

I have two definition classes of steps

First:

public class TranscricaoSadtStep {

    static WebDriver driver = Web.openBrowser();
    Autorizacoes_InicialPage inicial;
    AutorizacoesSite_Page autorizacoes;
    TranscricaoSadt_Page sadt;
    Atendimento_Page atendimento;

    @Given("^que acessei o menu transcricao$")
    public void queAcesseiOMenuTranscricao() throws Throwable {
        inicial = new Autorizacoes_InicialPage(driver);
        inicial.navigateTo();
        this.inicial.selecionarUsuario("MARCELO DA SILVEIRA MACHADO");
        this.inicial.clickQaAutorizacoes();
        String oldTab = driver.getWindowHandle();
        autorizacoes = new AutorizacoesSite_Page(driver);
        this.autorizacoes.trocarAba(oldTab, 0);
        this.autorizacoes.menuTranscricao();
    }

    @Given("^cliquei no submenu SADT$")
    public void cliqueiNoSubmenuSADT() throws Throwable {
        this.autorizacoes.submenuSadt();
        sadt = new TranscricaoSadt_Page(driver);
    }

Second:

WebDriver driver;
AutorizacoesSite_Page autorizacoes;
TranscricaoInternacao_Page internacao;
Atendimento_Page atendimento;

@Given("^cliquei no submenu solicitacao de internacao$")
public void cliqueiNoSubmenuSolicitacaoDeInternacao() throws Throwable {
    autorizacoes.submenuInternacao();
    internacao = new TranscricaoInternacao_Page(driver);
}

That is, it executes the first step of class 1 and then tries to execute the first step of class 2, because that is how it is written in Feature.

Only that it occurs NullPointException when it tries to run in class 2. And I already know the reason is that the driver is null, but I don’t know how to boot it.

Someone has a suggestion?

  • Good evening, all right? From what I read, you need to carry out an implementation of this Webdriver. There is a material in the following link containing the implementation in Firefox and Chrome. Link: https://artoftesting.com/automationTesting/launching-browsers-in-selenium.html

  • Actually, Webdriver is already implemented. It opens the browser, executes the step of class 1, and then it must execute the step of class 2. And that’s when it becomes null. What I need is for it to perform the class 2 step in the same open window, as it is the continuation of the test. That is, it opens the browser, click on the menu (step of class 1) then you must click on the submenu of that menu (class 2).

1 answer

0

You will need a class that starts and returns the driver before starting the tests and then loading that driver until the end of the execution as Voce already does in its constructors. the classes that receive these drivers need to extend from the class that creates the driver. because Voce uses the same instance. Example

method that creates driver instance:

public class CriaDriver(){

 public WebDriver getDriver() {
        System.setProperty("webdriver.chrome.driver","chromewindows/chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", prefs);
        return new ChromeDriver(options);
     }
    }

Test Steps that receive the method:

public class Teste1 extends CriaDriver{
    private WebDriver driver = new ChromeDriver;

  public Teste1(WebDriver driver){
    driver = getDriver();     

  }
  public void realizaTestesAqui(){
    //aqui voce faz seus testes
  }
}

Test class 2

public class Teste2 extends CriaDriver{
    private WebDriver driver = new ChromeDriver;

  public Teste1(WebDriver driver){
    driver = getDriver();     

  }
  public void realizaTestesAqui(){
    //aqui voce faz seus testes
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.