0

Code trials:

package Pages;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
public class pageBase {
    public static WebDriver driver;
    
    public pageBase(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }
    public static void AcceptAlert() {
        Alert alert = driver.switchTo().alert();
        alert.accept();
    }
}

Test page:

public void SignUp(String user_name,String password) throws InterruptedException {
    Set_UserName(user_name);
    Set_Password(password);
    click_SignupBtn();
    Thread.sleep(3000);
    AcceptAlert();
}

Error:

java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.switchTo()" because "Pages.pageBase.driver" is null
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Feb 23 '22 at 18:07

1 Answers1

0

In the pageBase pageobject as you are casting the WebDriver instance you need to remove the keywords:

  • public
  • static

Effectively, instead of:

public static WebDriver driver;

your line of code will be:

WebDriver driver;

References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352