>我正在使用硒页面工厂。在使用任何 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 来识别对象时,它都会抛出空指针异常。奇怪的是页面工厂适用于第一个 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);
}
}
请帮帮我。一个多星期以来,我一直在解决这个问题,但无法找到解决方案。
看这里
WebDriver driver;
@Test
public void TC_02() throws Exception{
HomePage hompg = new HomePage(driver);
我希望在主页中,有初始化驱动程序的代码,这就是为什么它可以工作。 然后你正在传递未初始化的驱动程序
WebDriver driver;
因此,您可能需要尝试从主页收集驱动程序,然后也传递到其他页面。
正如 murail 所说,当页面工厂初始化时,驱动程序不会初始化。它将驱动程序传递为空。
驱动程序初始化后更改页面出厂初始化,如下所示。
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);