質問

I am trying to run TestNG testcases which are InterDependent say in a Travel site Test cases are 1st) Login 2nd) Booking 3rd) Cancellation etc.

I am facing 'NullPointer exception' on When Webdriver is Called on 2nd Test.. Any Idea as I have also declared Driver as public static. I am reading the Locators from a Properties File.

Is It a TestNG bug ?

Here's my Code, Please Scroll Down to see the Pointer exception..

public class 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);
役に立ちましたか?

解決

The issue is that you're declaring WebDriver driver in loginTest(), then trying to reference the loginTest() instance of driver in booking().

If you modify loginTest() as follows, it should work:

driver = new FirefoxDriver();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top