java.lang.NullPointerException: Cannot invoke “org.openqa.selenium.WebDriver.quit()” because “this.driver” is null
at hooks.MyHook.teardown(MyHook.java:24)
public class MyHook extends Base{
@Before
public void beforeScenario(){
System.out.println("Scenarios has started");
}
@After
public void teardown() {
driver.quit();
}
}
public class Base {
public WebDriver driver;
public Properties prop;
public WebDriver initializeDriver() throws IOException{
prop= new Properties();
FileInputStream fis=new FileInputStream("C:\Users\CITPL-Developer-2\eclipse-workspace\Fdms\src\main\java\citpl\resources\data.properties");
prop.load(fis);
String browserName = System.getProperty("browser")!=null ? System.getProperty("browser") :prop.getProperty("browser");
if(browserName.equals("chrome"))
{
ChromeOptions options = new ChromeOptions();
options.addArguments("--remote-allow-origins=*");
System.setProperty("webdriver.chrome.driver","C:\Users\CITPL-Developer-2\driver\chromedriver.exe");
if(browserName.contains("headless")){
options.addArguments("headless");
}
driver = new ChromeDriver(options);
driver.manage().window().maximize();
}
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
return driver;
}
public class LoginSteps extends Base{
@Given ("Naviagate to the login page")
public void Naviagate_to_the_login_page() throws IOException {
initializeDriver();
driver.get(prop.getProperty("url"));
}
@When ("^I enter valid username (.+) and password (.+)$")
public void I_enter_valid_username_valid_username_and_Pasword(String username, String password) throws InterruptedException {
LoginPage lp= new LoginPage(driver);
lp.entervalidCredentials(username, password);
}
Source: Read More