2015-05-08 4 views
1

Пытался создать файл свойств и получить информацию от него, но я получаю java.lang.NullPointerException, также попытался попробовать и поймать, поскольку я очень новичок в кодировании, может кто-нибудь, пожалуйста, дайте мне знать, почему вы получаете исключение нулевого указателя.Exception Selenium Webdriver TestNg

package ObjectRepository; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.util.Properties; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.testng.annotations.Test; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.AfterTest; 

public class PropertiesGuru99Bank { 
    WebDriver driver; 
     @Test 
    public void f() { 
     File file=new File("E:\\selenium\\Rahul\\Project\\src\\ObjectRepository\\object.properties"); 
     FileInputStream f=null; 
     try { 
      f=new FileInputStream(file); 
    } catch (Exception e) { 
     // TODO: handle exception 
     System.out.println(e.getStackTrace()); 
    } 
     Properties prop=new Properties(); 
     try { 
      prop.load(f); 
     } catch (Exception e) { 
      // TODO: handle exception 
      System.out.println(e.getStackTrace()); 
     } 
    driver.get(prop.getProperty("url")); 
     driver.findElement(By.id(prop.getProperty("id"))).sendKeys("rahul"); 
    } 
    @BeforeTest 
    public void beforeTest() { 
    System.setProperty("webdriver.chrome.driver", "E:\\selenium\\lib\\chromedriver_win32\\chromedriver.exe"); 
    new ChromeDriver(); 
    } 
    @AfterTest 
    public void afterTest() { 
    } 
} 

ответ

3

Небольшая опечатка, вероятно. Вы не создаете экземпляр драйвера. new ChromeDriver(); не запускает драйвер. do driver = new ChromeDriver(); или показано ниже после установки драйвера exe в системное свойство.

import org.testng.annotations.Test; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.AfterTest; 

public class PropertiesGuru99Bank { 
    WebDriver driver; 

    @Test 
    public void f() { 
     File file=new File("E:\\selenium\\Rahul\\Project\\src\\ObjectRepository\\object.properties"); 
     FileInputStream f=null; 
     try { 
      f=new FileInputStream(file); 
     } catch (Exception e) { 
      // TODO: handle exception 
      System.out.println(e.getStackTrace()); 
     } 

     Properties prop=new Properties(); 

     try { 
      prop.load(f); 
     } catch (Exception e) { 
      // TODO: handle exception 
      System.out.println(e.getStackTrace()); 
     } 

     driver.get(prop.getProperty("url")); 
     driver.findElement(By.id(prop.getProperty("id"))).sendKeys("rahul"); 
    } 

    @BeforeTest 
    public void beforeTest() { 
    System.setProperty("webdriver.chrome.driver", "E:\\selenium\\lib\\chromedriver_win32\\chromedriver.exe"); 
    //this is what you are missing 
    driver = new ChromeDriver(); 
    } 

    @AfterTest 
    public void afterTest() { 
    } 
} 
+0

Huhh .. wow thankyou somuch :) получил очищенный !! –

+0

Да, конечно ... это сработало !!! :) –

+0

вы также можете помочь мне с этим https://stackoverflow.com/questions/30137514/getting-tooltip-of-an-element-amazon-site .. :) @Saifur –

Смежные вопросы