我正在尝试运行TestNG测试用例,这些测试用例是InterDependent的,比如在旅游网站中。测试用例是1)登录2)预订3)取消等。
当在第二次测试中调用Web驱动程序时,我面临"NullPointer异常"。。正如我所说的任何想法都将Driver声明为公共静态。我正在从属性文件中读取Locators。
这是一个TestNG错误吗?
这是我的代码,请向下滚动查看指针异常。。
公共类登录测试{
public static WebDriver driver;
public static Properties p;
public static FileInputStream f ;
@Test
public void loginTest() {
System.out.println("Enter LoginTest");
Properties p=new Properties();
FileInputStream f = null;
try {
f = new FileInputStream("D:\BOSSFramework\Framework\locators.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
p.load(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebDriver driver = new FirefoxDriver();
driver.get("https://in3.seatseller.travel/");
driver.manage().window().maximize();
// Login Process Starts here ..
try{
driver.findElement(By.name(p.getProperty("login.username.textfield"))).sendKeys("user");
driver.findElement(By.name(p.getProperty("login.password.textfield"))).sendKeys("password");
WebElement ele =driver.findElement(By.id(p.getProperty("login.signin.button")));
ele.click();
/*String classValue = ele.getAttribute("class");*/
}
catch (Exception e) {
}
}
@Test (dependsOnMethods={"loginTest"})
public void booking() throws InterruptedException{
System.out.println("Enter Booking");
// Type Bangalore on Source Field..
Properties p=new Properties();
FileInputStream f = null;
try {
f = new FileInputStream("D:\BOSSFramework\Framework\locators.properties");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
p.load(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*Null Pointer is on Below Line*/
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(p.getProperty("oneapp.source.textfield"))));
driver.findElement(By.xpath(p.getProperty("oneapp.source.textfield"))).sendKeys("Bangalore");
driver.findElement(By.xpath(p.getProperty("oneapp.source.textfield"))).sendKeys(Keys.TAB);
Thread.sleep(900L);
问题是在loginTest()
中声明WebDriver driver
,然后尝试在booking()
中引用driver
的loginTest()
实例。
如果按如下方式修改loginTest()
,它应该可以工作:
driver = new FirefoxDriver();