Webdriver无法读取元素空指针异常..

时间:2014-05-08 11:19:49

标签: java selenium webdriver

我正在尝试自动化登录测试序列 - 预订 - 取消。 直到预订它的好,但它到达取消的时刻,它抛出一个java lang Null-Pointer Exp。 我重新检查,我的定位器(xpath)是正确的(xpath-Checker),我正在尝试getText()票号并继续取消。

问题是WebDriver何时到达预订确认页面,一段时间后加载,它失败并返回空指针Exp ..

这可能是一个正在处理的加载问题,或者我搞乱了我的Java概念...

请帮助!!任何人...

public class StackOverflow { 

       public static  WebDriver driver;
       public static  Properties p;
       public static  FileInputStream f ;          

 @Test
        public void loginTest()  {
            System.out.println("Enter Login");
        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();
        }       

        driver = new FirefoxDriver();
        driver.get("https://in3.seatseller.travel/");
        driver.manage().window().maximize();


        try{
            driver.findElement(By.name(p.getProperty("login.username.textfield"))).sendKeys("UserName");
            driver.findElement(By.name(p.getProperty("login.password.textfield"))).sendKeys("Password");
            WebElement ele =driver.findElement(By.id(p.getProperty("login.signin.button")));
            ele.click();            
                }
             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();
            }

                WebDriverWait wait2 = new WebDriverWait(driver, 30);
                wait2.until(ExpectedConditions.visibilityOfElementLocated(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);         

                // Type Mysore on Destination Field             
                WebDriverWait wait1 = new WebDriverWait(driver, 30);
                wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath(p.getProperty("oneapp.destination.textfield"))));
                driver.findElement(By.xpath(p.getProperty("oneapp.destination.textfield"))).sendKeys("Tirupathi");
                driver.findElement(By.xpath(p.getProperty("oneapp.destination.textfield"))).sendKeys(Keys.TAB);     

  }
@Test (dependsOnMethods={"booking"})

              public void cancellation() throws InterruptedException{
                  System.out.println("Enter Cancellation");               
                    WebDriverWait wait4 = new WebDriverWait(driver, 60);
                    Thread.sleep(9000);
    /*Facing Null Pointer Exp  Here */                  
                    wait4.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(p.getProperty("oneapp.congratulations.text"))));
                    wait4.until(ExpectedConditions.presenceOfElementLocated(By.xpath(p.getProperty("oneapp.congratulations.text"))));
                    Thread.sleep(9000);
   /*Facing Null Pointer Exp  Here  */                  
                    WebElement ticket =driver.findElement(By.xpath(p.getProperty("oneapp.ticket.text")));   
   /*Want to getText() Ticket Number to cancel*/
                    String ticket1 = ticket.getText();
                    System.out.println(ticket1);
                    driver.findElement(By.xpath(p.getProperty("oneapp.leftNavCancel.link "))).click();                 
                    WebDriverWait wait1 = new WebDriverWait(driver, 30);
                    wait1.until(ExpectedConditions.presenceOfElementLocated(By.xpath(p.getProperty("oneapp.phoneNumber.textfield"))));
                    driver.findElement(By.xpath(p.getProperty("oneapp.phoneNumber.textfield"))).sendKeys(TIN1);                 
                    driver.findElement(By.xpath(p.getProperty("oneapp.search.button"))).click();
                    driver.findElement(By.xpath(p.getProperty("oneapp.cancel.button"))).click();  
}
}

2 个答案:

答案 0 :(得分:0)

在前两种方法中,您可以创建f = new FileInputStream,但在第三种方法中,您可以在没有它的情况下读取属性。您是否尝试再次阅读属性?

答案 1 :(得分:0)

在您的Properties p=new Properties();方法中添加cancellation()或在测试方法之外提及它(在开始时,您已声明public static Properties p;而是在public static Properties p = new Properties();定义它)