我试图在旅行网站中运行相互依赖的TestNG测试用例测试用例是1)登录2)预订3rd)取消等。

在第二次测试上调用Webdriver时,我面临“ NullPointer异常”。
我已经将Driver声明为Public static。
我正在从属性文件中读取定位器。

是TestNG错误吗?

这是我的代码,请向下滚动以查看Pointer异常。

公共类LoginTest {

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

最佳答案

问题是您要在WebDriver driver中声明loginTest(),然后尝试在loginTest()中引用driverbooking()实例。

如果按以下方式修改loginTest(),则它应该起作用:

driver = new FirefoxDriver();

07-28 00:01