2.2 C
New York
Sunday, January 12, 2025

selenium webdriver – I get this.driver” is null , Would you be capable to assist and what must be mounted?


java.lang.NullPointerException: Can’t invoke “org.openqa.selenium.WebDriver.give up()” as a result of “this.driver” is null
at hooks.MyHook.teardown(MyHook.java:24)

public class MyHook extends Base{

    @Earlier than
    public void beforeScenario(){
    System.out.println("Eventualities has began");
    }
    @After
    public void teardown()  {
    driver.give up();

    }   
    

}

public class Base {

public WebDriver driver;
public Properties prop;

public WebDriver initializeDriver() throws IOException{
prop= new Properties();
FileInputStream fis=new FileInputStream("C:CustomersCITPL-Developer-2eclipse-workspaceFdmssrcprimaryjavacitplsourcesinformation.properties");
prop.load(fis);

String browserName = System.getProperty("browser")!=null ? System.getProperty("browser") :prop.getProperty("browser");

if(browserName.equals("chrome"))
{
    ChromeOptions choices = new ChromeOptions();
    choices.addArguments("--remote-allow-origins=*");
    System.setProperty("webdriver.chrome.driver","C:CustomersCITPL-Developer-2driverchromedriver.exe");

    if(browserName.incorporates("headless")){
    choices.addArguments("headless");
    }       
    driver = new ChromeDriver(choices);
    driver.handle().window().maximize();
}
driver.handle().timeouts().implicitlyWait(Period.ofSeconds(10));
return driver;
}

public class LoginSteps extends Base{

@Given ("Naviagate to the login web page")
public void Naviagate_to_the_login_page() throws IOException {
    initializeDriver();
    driver.get(prop.getProperty("url"));
}
@When ("^I enter legitimate 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);
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles