my code looks in the next way:
Parent class:
package TestPackageForStructure;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class PageElements {
WebDriver driver;
public PageElements(WebDriver driver) {
this.driver = driver;
}
WebElement searchBox = driver.findElement(By.xpath("//*[@id=\"APjFqb\"]"));
}
Child class:
package TestPackageForStructure;
import io.qameta.allure.Step;
import org.openqa.selenium.WebDriver;
public class PageSteps extends PageElements {
public PageSteps(WebDriver driver) {
super(driver);
}
@Step
public void inputData(String text) {
searchBox.sendKeys("Hello");
}
Test class:
import TestPackageForStructure.PageSteps;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class TestStructure {
@Test
public void insertSomeData() throws InterruptedException {
WebDriver driver = new ChromeDriver();
PageSteps pageSteps = new PageSteps(driver);
driver.get("https://www.google.com/");
Thread.sleep(5000);
pageSteps.inputData("Hello World");
}
}
Error: java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null
So to say it shortly, I'm throwing my instance of a WebDriver to a parent class by using the super keyword in the child constructor but the driver is still null
I was expecting the driver to be passed to a parent class and an instance of WebElement to be created without any daviations (driver not being null)
I believe this should fix your code:
The problem you are having is that
driveris not set yet the way you are doing it.