2

I'm new in Selenium learning. I'm getting null pointer exception when I try to use web element - Milestone_Tile_Text.click; in my code but it works fine when I use

LoginTestScript.fd.findElement(By.linkText("Milestone")).click();

Please see below code I have used PageFactory.initElements as well but not sure how to solve this error.

public class MilestoneTileModel 
{

    GenerateTestData objtestdata = new GenerateTestData() ;

        public  MilestoneTileModel() //constructor
            {
                PageFactory.initElements(LoginTestScript.fd, this);
            }

        @FindBy(xpath="//a[text()='Milestone']")
        WebElement Milestone_Tile_Text;

public void Milestone_Tile_Click()
            {
                Milestone_Tile_Text.click();
                LoginTestScript.fd.findElement(By.linkText("Milestone")).click();
LoginTestScript.fd.findElement(By.xpath("//*@id='CPH_btnAddNewMilestoneTop']")).click();
            }
}
NarendraR
  • 7,577
  • 10
  • 44
  • 82
N.D
  • 21
  • 3
  • Does that link is clickable by using xpath? LoginTestScript.fd.findElement(By.xpath("//a[text()='Milestone']")).click(); – peter pawar Feb 04 '17 at 09:45
  • Please check the last click from Milestone_Tile_Click() method, you have a bad xpath, you need to open [ parenthesis after the * – lauda Feb 04 '17 at 10:02
  • @peterpawar...yes that link is clickable. – N.D Feb 04 '17 at 10:19
  • @lauda....mistake in copy paste, there is [ in code. – N.D Feb 04 '17 at 10:22
  • 1
    You should add a method to wait for the element, findElement does not have a wait and if the element is not found then will return null and it will result in a null pointer exception. – lauda Feb 04 '17 at 10:44

2 Answers2

0

Timing issues might occur more often when you use an init method.

The timing issue is when you init an element the driver immediately try to find the elements, on failure you will get no warning but the elements will refer null.

The above can occur for example because the page was not fully rendered or the driver see an older version of the page.

A fix can be to define the elements as a property and on the get of the property use the driver to get the element from the page

Please note that selenium does not promise the driver sees the latest version of the page so even this might break and on some situations a retry will work.

Avishay
  • 305
  • 1
  • 11
0

First problem what I see: You didn't set LoginTestScript

Following documentation at first you need to set PageObject variable: GoogleSearchPage page = PageFactory.initElements(driver, GoogleSearchPage.class);

The best way to rich that point is separate Page Object Model and scenario scipt

You fist file POM should contain:

LoginTestPOM

 public class LoginTestPOM {
     @FindBy(xpath="//a[text()='Milestone']")
     WebElement MilestoneTileText;

     public void clickMilestoneTitleText(){
            MilestoneTitleText.click();
     }
}

TestScript

import LoginTestPOM
public class TestLogin {
    public static void main(String[] args) {
        // Create a new instance of a driver
        WebDriver driver = new HtmlUnitDriver();

        // Navigate to the right place
        driver.get("http://www.loginPage.com/");

        // Create a new instance of the login page class
        // and initialise any WebElement fields in it.
        LoginTestPOM page = PageFactory.initElements(driver, LoginTestPOM.class);

        // And now do the page action.
        page.clickMilestoneTitleText();
    }
}  

This is basis of Page Object Pattern.

NOTE: I'm writing that code only in browser so it could contain some mistakes.

LINK: https://github.com/SeleniumHQ/selenium/wiki/PageFactory

The "ugly" solution without page object pattern is:

UglyTestScript

public class UglyTestLogin {
    public static void main(String[] args) {
        // Create a new instance of a driver
        WebDriver driver = new HtmlUnitDriver();

        // Navigate to the right place
        driver.get("http://www.loginPage.com/");

        // DON'T create a new instance of the login page class
        // and DON'T initialise any WebElement fields in it.
        // And do the page action.
        driver.findElement(By.xpath("//a[text()='Milestone']").click()
        }
}  
Liniel
  • 719
  • 1
  • 6
  • 15