![](/img/trans.png)
[英]NullpointerException while invoking SendKeys through Selenium using PageFactory with Page Object
[英]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.