Getting error:
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver" is null
I am trying to create sample framework but facing issue as url is not opening correctly and also browser is opening twice .
Chrome browser is opening twice and also we are seeing null value when debugging driver.
Below is the step definition file:
public class steps {
private WebDriver driver;
Baseclass base = new Baseclass(driver);
Loginpage login = new Loginpage(driver);
public static Logger logfile = LogManager.getLogger(steps.class.getName());
@Given("open chrome browser")
public void openBrowser() {
base.intiatingDriver();
}
@And("Login to application url")
public void loginAppicaltionUrl() {
login.openUrlLogin("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
}
@And("^enter the valid \"(.*)\"$")
public void enterValidUsername(String username) {
login.enteringUsername(username);
}
@And("^enter valid \"(.*)\"$")
public void enterValidPassword(String password) {
login.enteringPassword(password);
}
Baseclass:
public class Baseclass {
private WebDriver driver=null;
public static Logger logfile = LogManager.getLogger(Baseclass.class.getName());
public static String parentWindow;
public Baseclass(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void intiatingDriver() {
driver =new ChromeDriver();
driver.manage().window().maximize();
}
public void openUrl() throws Throwable{
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
threadsleep(5000);
logfile.info("Url opened successfully");
}
public void sendKeysUsingCss(WebElement element,String username) {
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(40));
element.sendKeys(username);
}
Login class:
public class Loginpage {
private WebDriver driver=null;
public Loginpage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
Baseclass base = new Baseclass(driver);
@FindBy(how=How.CSS, using="input[name^='username']")
private WebElement name;
@FindBy(how=How.CSS,using="input[name^='password']")
private WebElement passwrd;
public void openUrlLogin(String url) {
try {
driver.get(url);
driver.manage().window().maximize();
base.threadsleep(5000);
base.logfile.info("Url opened successfully");
} catch (Throwable e) {
e.printStackTrace();
System.out.println(e);
Assert.fail("Url not opened");
}
}
public void enteringUsername(String username) {
name.sendKeys(username);
}