運行我的測試時出現異常。我在頁面工廠使用硒。當我運行下面的代碼時,它會打開網站並在下面例外失敗。它在我的測試用例中不執行HomePage.ClickbtnCookieWarning()
。在頁面工廠運行我的測試時出現NullPointerException
有人可以幫我理解爲什麼我的代碼不工作嗎?
失敗配置:在 org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)@BeforeTest的SetUp顯示java.lang.NullPointerException 在 org.openqa.selenium.support .pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) 在com.sun.proxy。$ Proxy5.click(來源不明)在 pageObjects.HomePage.ClickLoginLink(HomePage.java:57)在 myaccountsuite.TC1DefaultDeliveryAddDisplay .SetUp(TC1DefaultDeliveryAddDisplay.java:29)
首頁頁面對象
public class HomePage {
WebDriver driver;
public HomePage (WebDriver driver)
{
this.driver=driver;
}
@FindBy(id="ctl00_header_hdrCookieWarning_btnHideCookieWarning")
WebElement btnCookieWarning;
@FindBy(xpath=".//*@id='ctl00_masterContainerTop_Block_637_LoginView1_ulAnonymous']/li[2]/a")
WebElement LoginLink;
public void ClickbtnCookieWarning()
{
btnCookieWarning.click();
}
public void ClickLoginLink()
{
LoginLink.click();
}
}
登錄頁面對象
public class login {
WebDriver driver;
public login(WebDriver driver)
{
this.driver = driver;
}
@FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_UserName")
WebElement UserName;
@FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_Password")
WebElement Password;
@FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_LoginButton")
WebElement btn_LogIn;
@FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_txtAccount")
WebElement Account;
@FindBy(id="ctl00_ContentPlaceHolderMain_container_container_Block_166_lgn1_btnHomeBranch_3")
WebElement btn_Continue;
public void userLogin(String uname, String pass, String acc)
{
UserName.sendKeys(uname);
Password.sendKeys(pass);
btn_LogIn.click();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Account.sendKeys(acc);
btn_LogIn.click();
btn_Continue.click();
}
}
我的測試
public class TC1DefaultDeliveryAddDisplay {
public WebDriver driver;
@BeforeTest(alwaysRun = true)
public void SetUp() {
HomePage HomePage = PageFactory.initElements(driver, HomePage.class);
login loginpage = PageFactory.initElements(driver, login.class);
driver = new FirefoxDriver();
driver.get("http://URL/");
HomePage.ClickbtnCookieWarning();
HomePage.ClickLoginLink();
loginpage.userLogin("[email protected]", "125", "Test");
}
LoginLink爲空。關鍵是弄清楚爲什麼沒有初始化。也許@FindBy失敗了。 –
你需要在'PageFactory.initElements'之前初始化webdriver,因爲'driver = new FirefoxDriver(); HomePage HomePage = PageFactory.initElements(driver,HomePage.class); login loginpage = PageFactory.initElements(driver,login.class);' –
謝謝。你能否給我更多的細節?任何想法爲什麼@FindBy失敗?你有什麼建議改變我的代碼? – Prasa