Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用selenium pagefactory时接收空指针异常_Java_Selenium_Selenium Webdriver_Selenium Chromedriver_Pageobjects - Fatal编程技术网

Java 使用selenium pagefactory时接收空指针异常

Java 使用selenium pagefactory时接收空指针异常,java,selenium,selenium-webdriver,selenium-chromedriver,pageobjects,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,Pageobjects,我正在使用selenium页面工厂。在使用任何WebElement时,我都会收到空指针异常 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.AddNewCustom

我正在使用selenium页面工厂。在使用任何WebElement时,我都会收到空指针异常

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上找到了一些解决方案

然而,这对我来说并不管用。 我尝试在测试用例和页面对象脚本中初始化页面对象。然而,这两种方法对我都不起作用

以下是page factory的代码:

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);
可能重复的