簡體   English   中英

NelePointerException在硒中使用頁面對象模型

[英]NullPointerException using Page Object Model in Selenium

我目前正在嘗試通過在其中實現Page Object Model來改進Selenium測試用例。 但是,我正在測試的網站處理許多模式。 當我嘗試訪問某些模態時,出現NullPointerException。 我無法確定驅動程序是否在等待元素或什么。

這是我的課程:

頁面對象

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();
}

}

測試班

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();
}
}

運行正常,直到我調用enterToAddress()。 當達到這一點時,它將引發NullPointerException。 即使我定義了隱式等待和顯式等待,驅動程序似乎也不在等待元素加載。

WebDriverWait wait未實例化,因此調用它會引發NullPointerException

ManualShipmentModal的構造函數中使用其構造函數:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM