当我试图执行基于POM的测试用例时,我得到了“java.lang.NullPointerException”。类BrowserFactory允许我选择浏览器,基于POM的类Flipkart_Login存储该特定页面的所有元素,并有一个Valid_Login()的方法,最后是Test_Flipkart_Login类-调用Valid_Login()方法执行executon,但当我尝试执行该类时,得到java.lang.NullPointerException。
好心奉劝!
公共类ConfigDataProvider{静态属性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();
}
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();
}
}
您已经在COnfigDataProider的构造函数中初始化了Properties pro,但是您正在使用对测试类中的getURL方法的静态调用。因此pro将为空且未初始化。移除静态调用,使用构造函数或make pro进行静态化,并在静态块中初始化。