我正在使用selenium page factory。在使用任何WebElements时,我收到空指针异常。
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.PagesUsingPageFactory.AddNewCustomerUsingPF;
import com.PageswithoutPageFactory.HomePage;
import com.PageswithoutPageFactory.InvokeBrowserSettings;
import com.PageswithoutPageFactory.LoginPage;
public class CreateNewCustomerNegative {
WebDriver driver;
@Test
public void TC_02() throws Exception{
HomePage hompg = new HomePage(driver);
AddNewCustomerUsingPF newcust = new AddNewCustomerUsingPF(driver);
LoginPage loginpage = new LoginPage(driver);
System.setProperty("webdriver.chrome.driver","C:\\Users\\Chinmay\\Downloads\\chromedriver_win32\\chromedriver.exe");
InvokeBrowserSettings invoke = new InvokeBrowserSettings();
driver = invoke.invokeBrowser("chrome", Constant.URL);
loginpage.SignIntoAppWithValidUsrPwd(driver);
//verify home page displayed after valid credentials
hompg.validateHomePageLogo(driver);
hompg.validateManagerButton(driver);
hompg.validatenewCustomerButton(driver);
hompg.clickNewCustomer(driver);
//driver.findElement(By.xpath("//a[contains(text(),'New Customer')]")).click();
//check if add new customer tab is present
Assert.assertTrue(driver.findElement(By.xpath("//p[contains(text(),'Add New Customer')]")).isDisplayed(), "Add new customer option is not visible");
//check if customer name textbox is present
Assert.assertTrue(driver.findElement(By.name("name")).isDisplayed(), "Customer name text box is not presernt");
//name field blank validation
System.out.println("driver=" + driver);
newcust.typeCustomerName("");
}
}
` 每当我使用pagefactory来识别对象时,它都会抛出nullpointer异常。 奇怪的是页面工厂适用于第一个java文件测试用例,当我在另一个java文件中使用同一页面工厂时,它始终以nullpointer异常失败。 我在stackoverflow Selenium java.lang.NullPointerException with PageFactory
上看到了一些解决方案然而,它对我不起作用。 我尝试在我的测试用例和我的页面对象脚本中初始化页面对象。但是,它都不适用于我。
以下是页面工厂的代码:
package com.PagesUsingPageFactory;
import org.apache.commons.lang3.RandomStringUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
public class AddNewCustomerUsingPF {
public WebDriver driver;
public AddNewCustomerUsingPF(WebDriver driver) {
this.driver=driver;
PageFactory.initElements(driver, this);
}
@FindBy(how=How.XPATH, using="//p[contains(text(),'Add New Customer')]")
public WebElement addNewCustomerLabel;
@FindBy(how=How.XPATH, using="//input[@type='text'][@name='name']")
public WebElement customerNameTxtField;
@FindBy(how=How.XPATH, using="//a[contains(text(),'New Customer')]")
public WebElement newCustomerButton;
public void typeCustomerName(String name) throws Exception {
customerNameTxtField.sendKeys(name);
}
}
请帮帮我。我一个多星期以来一直在解决这个问题而且无法找到解决方案。
答案 0 :(得分:2)
见这里
WebDriver driver;
@Test
public void TC_02() throws Exception{
HomePage hompg = new HomePage(driver);
我希望在HomePage中,有代码来初始化驱动程序,这就是为什么它正在工作。那么你传递的是未初始化的驱动程序
WebDriver driver;
因此,您可能需要尝试从主页收集驱动程序,然后再传递给其他页面。
答案 1 :(得分:1)
正如murail所说,当页面工厂初始化时,驱动程序未初始化。它将驱动程序传递为null。
在驱动程序初始化后更改页面工厂初始化,如下所示。
public class CreateNewCustomerNegative {
WebDriver driver;
@Test
public void TC_02() throws Exception{
//Initialize the driver first
System.setProperty("webdriver.chrome.driver","C:\\Users\\Chinmay\\Downloads\\chromedriver_win32\\chromedriver.exe");
InvokeBrowserSettings invoke = new InvokeBrowserSettings();
driver = invoke.invokeBrowser("chrome", Constant.URL);
//Initialize page factory
HomePage hompg = new HomePage(driver);
AddNewCustomerUsingPF newcust = new AddNewCustomerUsingPF(driver);
LoginPage loginpage = new LoginPage(driver);
loginpage.SignIntoAppWithValidUsrPwd(driver);