Search code examples
javaseleniumautomationtestng-eclipse

java.lang.NullPointerException ,at Annotaion_testng.Login_case.login(Login_case.java:34


package Annotaion_testng;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Login_case {

  public String baseUrl = "https://www.eventerprise.com";
  String driverpath = "C://Users//fizan//Documents//testing//webdriver_soft//fire_fox_driver//geckodriver.exe";
  public WebDriver driver;

  @BeforeMethod
  public void f() throws InterruptedException {

    System.out.println("opening firefox");
    System.setProperty("webdriver.gecko.driver", driverpath);
    WebDriver driver = new FirefoxDriver();
    driver.get(baseUrl);
    Thread.sleep(10000);
  }

  @Test
  public void login(){
    System.out.println("click on login link from header");

    //code is failing from here
    driver.findElement(By.xpath(".//*[@id='app']/div/div/div/header/div[1]/div[1]/div[2]/ul/li[4]/a")).click();

    driver.findElement(By.id("email")).sendKeys("[email protected]");

    driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    //enter  password

    driver.findElement(By.id("password")).sendKeys("atyantik@1234");

    driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    //click on login button

    driver.findElement(By.xpath(".//*[@id='eve-modal']/div/div[1]/div/div/div[2]/div/div/div/div[1]/div[2]/form/div[4]/button")).click();

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    //click on log out from header

    System.out.println("logging out");

    driver.findElement(By.xpath(".//*[@id='app']/div/div/header/div[1]/div[1]/div[2]/div[1]/ul/li[7]/a")).click();  
  }
}

Solution

  • You are shadowing driver in f WebDriver driver = new FirefoxDriver(); As you have already correctly declared this as field, removed the new redeclaration

    i.e.

    driver = new FirefoxDriver();