Selenium POM java.lang.NullPointerException

时间:2016-08-14 19:51:31

标签: selenium-webdriver

我得到" java.lang.NullPointerException"当我试图基于POM执行我的测试用例时。 BrowserFactory类允许我选择一个浏览器,基于POM的类Flipkart_Login存储该特定页面的所有元素,并具有Valid_Login()的方法 最后Test_Flipkart_Login类 - 调用Valid_Login()方法执行,但是当我尝试执行这个类时,我得到了java.lang.NullPointerException。

请建议!

失败:Flipkart_Login_Test 显示java.lang.NullPointerException     at DataProviders.ConfigDataProvider.getURL(ConfigDataProvider.java:31)     在TestCases.Test_Flipkart_Login.Flipkart_Login_Test(Test_Flipkart_Login.java:19)

公共类ConfigDataProvider {     static Properties pro;

public ConfigDataProvider()
{

    File src = new File("C:\\Data\\Bimlesh\\Flipkart_HybridFramework\\Flipkart.Hybrid.FrameworkComplete\\Configuration\\Config.Properties");

    try 
    {
        FileInputStream fis = new FileInputStream(src);
        pro = new Properties();
        pro.load(fis);

    } catch (Exception e) 
    {
        System.out.println("The Config exception is :"+e.getMessage());
    }

}

public static String getURL()
{
    String URL = pro.getProperty("URL");
    return URL;
}

public static String ChromePath()
{
    String Chrome = pro.getProperty("Chromepath");
    return Chrome;
}

public static String IEPath()
{
    String IE = pro.getProperty("IEpath");
    return IE;
}

}

公共类BrowserFactory {     静态WebDriver驱动程序;

public static WebDriver getBrowser(String BrowserName)
{
    if(BrowserName.equalsIgnoreCase("Firefox"))
    {
        driver = new FirefoxDriver();
    }
    else if(BrowserName.equalsIgnoreCase("Chrome"))
    {
        System.setProperty("webdriver.chrome.driver", ConfigDataProvider.ChromePath());
        driver = new ChromeDriver();
    }
    else if(BrowserName.equalsIgnoreCase("IE"))
    {
        System.setProperty("webdriver.ie.driver", ConfigDataProvider.IEPath());
        driver = new InternetExplorerDriver();
    }

    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    return driver;
}

public void ShutDown(WebDriver driver)
{
    driver.quit();
}

}

公共类Flipkart_Login {     WebDriver驱动程序;

public Flipkart_Login(WebDriver driver)
{
    this.driver=driver;
}

@FindBy(xpath="//a[text()='Log In']") WebElement Login_Click;
@FindBy(xpath="//input[@class='_2zrpKA' and @type='text']") WebElement Enter_Email;
@FindBy(xpath="//input[@class='_2zrpKA _3v41xv' and @type='password']") WebElement Enter_Pass;
@FindBy(xpath="//button[@type='submit' and @class='_3zLR9i _1LctnI _36SmAs']") WebElement Login_Button;

public void Valid_Login()
{
    Login_Click.click();
    Enter_Email.sendKeys("xxx@gmail.com");
    Enter_Pass.sendKeys("xxx");
    Login_Button.click();

}

}

公共类Test_Flipkart_Login {     WebDriver驱动程序;

@Test
public void Flipkart_Login_Test()
{
    driver = BrowserFactory.getBrowser("Firefox");
    driver.get(ConfigDataProvider.getURL());
    Flipkart_Login page1 = PageFactory.initElements(driver, Flipkart_Login.class);
    page1.Valid_Login();

}

}

1 个答案:

答案 0 :(得分:0)

您已在COnfigDataProider的构造函数中初始化了Properties pro,但您正在使用对测试类的getURL方法的静态调用。因此pro将为null并且不会初始化。删除静态调用并使用构造函数或使pro成为静态并在静态块中初始化。