[英]NullPointerException when running my test with page factory
I get an exception when I run my test. 运行测试时出现异常。 I am using selenium with page factory.
我在页面工厂中使用硒。 When I run following code ,it will open up the website and fail with exception below.
当我运行以下代码时,它将打开网站,但出现以下异常失败。 it doesn't perform the
HomePage.ClickbtnCookieWarning()
in my test case. 它在我的测试案例中不执行
HomePage.ClickbtnCookieWarning()
。
Can someone please help me to understand why my code isn't working? 有人可以帮我理解为什么我的代码不起作用吗?
FAILED CONFIGURATION: @BeforeTest SetUp java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69) at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38) at com.sun.proxy.$Proxy5.click(Unknown Source) at pageObjects.HomePage.ClickLoginLink(HomePage.java:57) at myaccountsuite.TC1DefaultDeliveryAddDisplay.SetUp(TC1DefaultDeliveryAddDisplay.java:29)
失败的配置:@BeforeTest在org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)处的java.lang.NullPointerException .java:38)位于com.sun.proxy。$ Proxy5.click(未知源)位于myaccountsuite的pageObjects.HomePage.ClickLoginLink(HomePage.java:57).TC1DefaultDeliveryAddDisplay.SetUp(TC1DefaultDeliveryAddDisplay.java:29)
Home Page page object 主页页面对象
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();
}
}
Login Page Object 登录页面对象
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();
}
}
My Test 我的测试
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("aa@yahoo.com", "125", "Test");
}
You're getting NullPointerException
because you're using WebDriver
instance before initialising. 之所以得到
NullPointerException
是因为在初始化之前使用的是WebDriver
实例。
You need to Initialize WebDriver
before using this instance as :- 您需要先初始化
WebDriver
然后将此实例用作:-
driver = new FirefoxDriver();
HomePage HomePage = PageFactory.initElements(driver, HomePage.class);
Login loginpage =PageFactory.initElements(driver, login.class);
If you want to use WebDriver
as singleton
which returns single instance for all your test methods you can follow this answer which is exactly you want . 如果要使用
WebDriver
作为singleton
,它返回所有测试方法的单个实例,则可以按照您想要的答案进行操作 。
The problem is in each class you are creating new instance of driver. 问题在于您正在创建驱动程序的新实例的每个类中。 You just need to create one driver instance in you base class where you do your browser setup.
您只需要在您的基类中创建一个驱动程序实例,即可在其中进行浏览器设置。 Please refer Page Object Model.
请参考页面对象模型。 Once the Driver instance is created you need to use the same in all your classes.
创建Driver实例后,您需要在所有类中使用相同的实例。 Or else it will throw NullPointerException because driver will not have any reference.
否则它将抛出NullPointerException,因为驱动程序将没有任何引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.