我目前正在尝试通过在其中实现Page Object Model来改进Selenium测试用例。但是,我正在测试的网站处理许多模式。当我尝试访问某些模态时,出现NullPointerException。我不知道驾驶员是否在等待元素或什么。
这是我的课程:
页面对象
<div class="row-wrap">
<div class="row-top">uppertext1</div>
<div class="row-middle">My</div>
<div class="row-bottom">bottomtext1</div>
</div>
<div class="row-wrap">
<div class="row-top">uppertext2</div>
<div class="row-middle">Name</div>
<div class="row-bottom">bottomtext2</div>
</div>
<div class="row-wrap">
<div class="row-top">uppertext3</div>
<div class="row-middle">Is</div>
<div class="row-bottom">bottomtext3</div>
</div>
<div class="row-wrap">
<div class="row-top">uppertext4</div>
<div class="row-middle">Some</div>
<div class="row-bottom">bottomtext4</div>
</div>
<div class="row-wrap">
<div class="row-top">uppertext5</div>
<div class="row-middle">Long Name</div>
<div class="row-bottom">bottomtext5</div>
</div>
}
测试课程
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();
}
运行正常,直到我调用enterToAddress()。当达到这一点时,它将引发NullPointerException。即使我定义了隐式等待和显式等待,驱动程序似乎也不在等待元素加载。
答案 0 :(得分:0)
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);
}