-1

I did a basic project to training about Page Objects using selenium WebDriver with java and Junit. So, I make a page object class and Junit class too. I Make a call of method and pass the parameters to method but, the eclipse show a message that say: java.lang.NullPointerException

public class LogarBkoMaisPage {

static WebDriver driver;

 By campoNome = By.id("matricula_I");
 By campoSenha = By.id("senha_I");
 By btnLogin      = By.id("bt_entrar");

public LogarBkoMaisPage(WebDriver driver) {
    this.driver = driver;
}
public void logar(String usuario, String senha) {
    driver.findElement(campoNome).sendKeys(usuario);
    driver.findElement(campoSenha).sendKeys(senha);
    driver.findElement(btnLogin).click();
}
}

public class LogarBkoMaisTest {

static WebDriver driver;

@Before
public void setUp() throws Exception {  
    SelecionarNavegador nav = new SelecionarNavegador();
    nav.iniciarNavegador("ie","http://10.5.9.45/BkoMais_Selenium/");
}
@Test
public void logarAplicacao() {      
    try {
        LogarBkoMaisPage login = new LogarBkoMaisPage(driver);
        login.logar("844502","Bcc201707");
    }catch(Exception e) {
        System.out.println("Mensagem de erro: " +e);
    }
}
@After
public void tearDown() throws Exception {
}
}

public class SelecionarNavegador {

static WebDriver driver;

public static WebDriver iniciarNavegador(String nomeNavegador, String url) {

    if(nomeNavegador.equalsIgnoreCase("firefox")) {
        System.setProperty("webdriver.gecko.driver", "E:\\workspace_BCC_QA_BKOMAIS\\"
                + "FireFoxGeckodriver64\\geckodriver.exe");
        driver = new FirefoxDriver();
    }
    else if(nomeNavegador.equalsIgnoreCase("chrome")){
        System.setProperty("webdriver.chrome.driver", "E:\\workspace_BCC_QA_BKOMAIS"
                + "\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();    
    }else if(nomeNavegador.equalsIgnoreCase("IE")) {
        System.setProperty("webdriver.ie.driver", "E:\\workspace_BCC_QA_BKOMAIS"
                + "\\IE Plugin\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
    }
    driver.manage().window().maximize();
    driver.get(url);
    return driver;
}
}

Exception:

Exception

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Leandro Pereira
  • 19
  • 1
  • 2
  • 7
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – MarkusEgle Jan 04 '18 at 15:39
  • Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. – JeffC Jan 04 '18 at 18:25

1 Answers1

0

You are getting a null pointer exception because iniciarNavegador method inside SelecionarNavegador class is the one which initializes the driver and it returns the driver which has to be assigned to a vairable. You need to do this in your setUp()method

@Before
public void setUp() throws Exception {  
    SelecionarNavegador nav = new SelecionarNavegador();
    driver=nav.iniciarNavegador("ie","http://10.5.9.45/BkoMais_Selenium/");
}
shank087
  • 492
  • 8
  • 17