使用 selenium pagefactory 时收到空指针异常

Recieving null pointer exception while using selenium pagefactory

我正在使用 selenium 页面工厂。在使用任何 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 文件中使用相同的页面工厂时,它总是失败并出现空指针异常。 我在 Whosebug

上看到了一些解决方案

但是,它对我不起作用。 我尝试在我的测试用例和我的页面对象脚本中初始化页面对象。但是,这两种方法都不适合我。

这是页面工厂的代码:

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所说,初始化页面工厂时驱动程序没有被初始化。它将驱动程序作为 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);