简体   繁体   English

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

[英]Recieving null pointer exception while using selenium pagefactory

I am using selenium page factory. 我正在使用硒页工厂。 And while using any of the WebElements, I am receiving null pointer exception. 在使用任何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("");
}
}

` Whenever I am using pagefactory for identifying objects, it throws nullpointer exception. `每当我使用pagefactory来识别对象时,它都会抛出nullpointer异常。 The weird thing is the page factory works for first java file test case, when I use same page factory in another java file, it always fails with nullpointer exception. 奇怪的是页面工厂适用于第一个java文件测试用例,当我在另一个java文件中使用同一页面工厂时,它始终以nullpointer异常失败。 I saw some solution on stackoverflow Selenium java.lang.NullPointerException with PageFactory 我在使用PageFactory的 stackoverflow Selenium java.lang.NullPointerException上看到了一些解决方案

However, it did not work for me. 但是,它对我不起作用。 I tried initializing page object in my test case and also in my page object script. 我尝试在我的测试用例和我的页面对象脚本中初始化页面对象。 However, neither of it worked for me. 但是,它都不适用于我。

Here is the code for 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);
    }


}

Please help me out. 请帮帮我。 I am debigging this issue since more than a week and not able to find the solution. 我超过一周就解决了这个问题而无法找到解决方案。

see here 看这里

  WebDriver driver;
@Test
public void TC_02() throws Exception{
    HomePage hompg = new HomePage(driver);

i hope in HomePage, there is code to initialize driver, that why it is working. 我希望在HomePage中,有代码来初始化驱动程序,这就是为什么它正在工作。 then you are passing driver which is not initialized 那么你传递的是未初始化的驱动程序

WebDriver driver;

So, you may need to try to collect driver from Homepage and then pass to other pages also. 因此,您可能需要尝试从主页收集驱动程序,然后再传递到其他页面。

As murail said, the driver is not initialized when page factory is intialized. 正如murail所说,当页面工厂初始化时,驱动程序未初始化。 It is passing driver as null. 它将驱动程序传递为null。

Change page factory initialization after driver initialization as given below. 驱动程序初始化后更改页面工厂初始化,如下所示

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行Selenium时获取空指针异常-使用Pagefactory的TestNG脚本 - Getting Null Pointer Exception while running Selenium - TestNG Scripts using pagefactory selenium webdriver中的空指针异常 - TestNG - PageFactory - Null pointer exception in selenium webdriver - TestNG - PageFactory PageFactory给出了空指针异常 - PageFactory gives null pointer exception 为什么我收到空指针异常? - Why am I recieving a null pointer exception? 我使用com.sun.jdi.InvocationException发生了调用方法的“空指针异常”。 在硒中使用PageFactory时 - I am getting “null pointer Exception” with com.sun.jdi.InvocationException occurred invoking method. when used PageFactory in selenium 使用Java的Selenium RC中的空指针异常 - Null pointer exception in selenium RC Using java 使用Java的硒代码中的空指针异常 - null pointer exception in selenium code using java 使用Selenium 3.8.1与android驱动程序一起运行时对象抛出空指针异常 - Object throwing null pointer exception while running with android Driver using Selenium 3.8.1 运行 selenium 测试时出现 Null 指针异常。 我正在使用页面 object model - Null pointer exception while running selenium test. I am using Page object model 使用总和时发生空指针异常 - Null Pointer Exception while using sum agregation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM