0

I tried to Create a Simple Program in Selenium Using PageObjectModel. While Running the Program it throws Null Pointer Exception. Don't Know what i am doing wrong.Is my initialization of Variable is Wrong. I know i am making mistake in initializing the By locator but don't know what i am doing wrong.

    public class main extends Base{

    private static final int TIMEOUT = 5;
    private static final int POLLING = 100;

    protected WebDriverWait wait;
    protected static WebElement ele;
    protected By locator;

    public void Base() {
        wait = new WebDriverWait(driver, TIMEOUT, POLLING);
    }

    public WebElement waitForElementToAppear(By locator) {
        wait.until(ExpectedConditions.presenceOfElementLocated(locator));//Line which Throws Null
        return ele;
    }

    protected void waitForElementToDisappear(By locator) {
        wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
    }

    protected void waitForTextToDisappear(By locator, String text) {
        wait.until(ExpectedConditions.not(ExpectedConditions.textToBe(locator, text)));
    }
@Test()
   public void getURL() {
            driver.get("https://www.google.com");
            waitForElementToAppear(By.name("q")).sendKeys("Pom");// Line Which Throws Null.

}

And My Base Class Code where i have saved the properties of the driver.

 public class Base {
    protected WebDriver driver;

    public WebDriver getDriver() {
        return driver;
    }

    public void setDriver(WebDriver driver) {
        this.driver = driver;
    }
    @BeforeSuite
    public void beforeSuite() {
        System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe"); // You can set this property elsewhere
        driver=new ChromeDriver();
        driver.manage().window().maximize();

    }
}
3
  • Can you please share error stack trace? Commented Mar 10, 2019 at 8:17
  • FAILED: getURL java.lang.NullPointerException at com.demo.main.waitForElementToAppear(main.java:27) at com.demo.main.getURL(main.java:41) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85) at org.testng.internal.Invoker.invokeMethod(Invoker.java:639) Commented Mar 10, 2019 at 9:11
  • Have you imported ExpectedConditions? Commented Mar 10, 2019 at 9:20

2 Answers 2

2

The problem lies in the way in which you are initialising the WebDriverWait object.

Your WebDriver object will get instantiated only when the @BeforeSuite method runs in your Base class.

The logic of initialising the WebDriverWait is part of the method public void Base() in your main class.

But your @Test annotated getURL() method does not invoke Base() method. So your wait object is always null.

To fix this, invoke Base() within your @Test method or have your Base() method annotated with @BeforeClass annotation, so that it gets automatically called by TestNG.

Sign up to request clarification or add additional context in comments.

1 Comment

This should nail the OP
0

There are multiple problems in your code

  • Probably you do not need global variable declarations. Of course you can do it like that but make sure you initialize them.
  • Do not put test methods in your page object
  • ele will always be null
  • Call the constructor

Probably you need something like following:

 public class MyPageObject extends Base{

    private static final int TIMEOUT = 5;
    private static final int POLLING = 100;

    protected WebDriverWait wait;

    public void MyPageObject() {
        super();                                             //Call constructor of Base if needed
        wait = new WebDriverWait(driver, TIMEOUT, POLLING);  //init wait
    }

    public WebElement waitForElementToAppear(By locator) {
        wait.until(ExpectedConditions.presenceOfElementLocated(locator));
        return driver.findElement(locator);                  //return WebElement
    }

    protected void waitForElementToDisappear(By locator) {
        wait.until(ExpectedConditions.invisibilityOfElementLocated(locator));
    }

    protected void waitForTextToDisappear(By locator, String text) {
        wait.until(ExpectedConditions.not(ExpectedConditions.textToBe(locator, text)));
    }
}

public class MyTestClass {
   @Test()
   public void getURL() {
       MyPageObject myPageObject = new MyPageObject();       //Initialize your page object
       driver.get("https://www.google.com");
       myPageObject.waitForElementToAppear(By.name("q")).sendKeys("Pom");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.