简体   繁体   中英

NullPointerException using Page Object Model in Selenium

I'm currently trying to revamp my Selenium test cases by implement the Page Object Model in them. However, the website I'm testing deals with a lot of modals. When I try to access certain modals, I get a NullPointerException. I can't tell if the driver isn't waiting for the element or what.

Here are my classes:

Page Object

public class ManualShipmentModal
{
WebDriver driver;
WebDriverWait wait;

@FindBy(id = "manual-order-modal")
WebElement modalBody;

@FindBy(name = "mailToName")
WebElement toName;

/* Constructor */
public ManualShipmentModal(WebDriver driver)
{
    this.driver = driver;
    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

    AjaxElementLocatorFactory factory = new AjaxElementLocatorFactory(driver, 100);
    PageFactory.initElements(factory, this);
}

public boolean modalExists()
{
    return modalBody.isDisplayed();
}

public void enterToAddress(String toName, String addressName)
{
    wait.until(ExpectedConditions.visibilityOf(modalBody));
    WebElement toAddress = driver.findElement(By.linkText(addressName));

    this.toName.sendKeys(toName);
    toAddress.click();
}

}

Test class

public class TEST_11
{

WebDriver driver;

LoginPage loginPageObj;
HeaderMenu headerMenuObj;
ManualShipmentModal manualShipmentModalObj;

@Before
public void setup()
{
    driver = new ChromeDriver();
    driver.get("testpage.com");
}

@Test
public void testCreateNewShipment()
{
    loginPageObj = new LoginPage(driver);
    headerMenuObj = new HeaderMenu(driver);
    manualShipmentModalObj = new ManualShipmentModal(driver);

    loginPageObj.login("username", "password");
    headerMenuObj.createNewShipment();
    manualShipmentModalObj.enterToAddress("Test", "The Usual Test");
}

@After
public void teardown()
{
    driver.close();
}
}

It runs fine up until I call enterToAddress(). When it gets to that point, it throws a NullPointerException. It seems like the driver isn't waiting for the element to load even though I have an implicit wait defined as well as an explicit wait.

WebDriverWait wait isn't instantiated, so calling it throws a NullPointerException .

Use its constructor in ManualShipmentModal 's constructor:

public ManualShipmentModal(WebDriver driver)
{
    this.driver = driver;
    long timeOutInSeconds = 10; // just an arbitrary value example
    this.wait = new WebDriverWait(driver, timeOutInSeconds);

    // the rest of your constructor as in the original code
    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

    AjaxElementLocatorFactory factory = new AjaxElementLocatorFactory(driver, 100);
    PageFactory.initElements(factory, this);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM