0

I created a POM structure where driver is initialized in the main class and called in page classes using constructor. The problem is that the url is also passed from one of a page class called s1 landing page but its not invoking, only chrome is getting opened. URL is not getting passed.

Main class

public class Sample1 {
    public static void main(String[] args) {
        String item = "ADIDAS ORIGINAL";
        WebDriverManager.chromedriver().setup();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

        S1LandingPage S1landingPage = new S1LandingPage(driver);
        S1landingPage.Goto();
        S1landingPage.login("sanasameer.k@gmail.com","Sana@1234");
    }
}

S1LandingPage

public class S1LandingPage extends Sample1 {
    WebDriver driver;

    public S1LandingPage(WebDriver driver) {
        driver = this.driver;
        PageFactory.initElements(driver, this); //to get driver for local variables made by find by 
    }

    @FindBy(id="userEmail")
    WebElement Uname;

    @FindBy(id="userPassword")
    WebElement pw;

    @FindBy(id="login")
    WebElement submit;

    public void login(String email, String passwrd) {
        Uname.sendKeys(email);
        pw.sendKeys(passwrd);
        submit.click();
    }

    public void Goto()  {
        driver.get("https://rahulshettyacademy.com/client/"); 
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
    }
}

Error

Exception in thread "main" java.lang.NullPointerException
    at pageObjects.S1LandingPage.Goto(S1LandingPage.java:45)
    at rahulshety.EndtoEnd.Sample1.main(Sample1.java:33)
JeffC
  • 22,180
  • 5
  • 32
  • 55

1 Answers1

0

Your assignment in the constructor is backwards.

public S1LandingPage(WebDriver driver) {
    driver = this.driver;
}

should be

public S1LandingPage(WebDriver driver) {
    this.driver = driver;
}

this references the current object, S1LandingPage. So, this.driver is the property driver on the S1LandingPage class and driver is the parameter driver.

See the docs on this usage.

JeffC
  • 22,180
  • 5
  • 32
  • 55