java.lang.NullPointerException webdriver

时间:2012-10-05 13:46:51

标签: firefox webdriver selenium-webdriver

我是使用webdriver进行自动测试的新手。有人请检查我收到的原因

java.lang.NullPointerException
    at SuccessfullHoverTestCib.testSuccessfullHoverTestCib
    (SuccessfullHoverTestCib.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source).....

我正在运行以下代码:

public class SuccessfullHoverTestCib {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        File pathToFirefoxBinary = new File("C:\\Documents and Settings\\chakarova\\Local Settings\\Application Data\\Mozilla Firefox\\firefox.exe");  
        FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);
        WebDriver driver = new FirefoxDriver(firefoxbin,null); 
        baseUrl = "http://cibnew.sofia.ifao.net:7001/cib_web/web";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testSuccessfullHoverTestCib() throws Exception {
        driver.get(baseUrl);
        driver.findElement(By.id("inputButton")).click();
        driver.findElement(By.linkText("Hotels")).click();
        driver.findElement(By.id("pageLink_57")).click();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

当我指向错误跟踪时,它链接到测试method->driver.get(baseUrl);我认为问题与firefox.exe路径的定义有关,但我不知道如何解决it.Any帮助将不胜感激。

1 个答案:

答案 0 :(得分:12)

在setUp()方法中,您声明一个名为driver的本地WebDriver变量,并将其设置为新的驱动程序实例。您的类级驱动程序变量永远不会设置为实例,因此始终为null。

public class SuccessfullHoverTestCib {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        File pathToFirefoxBinary = new File("C:\\Documents and Settings\\chakarova\\Local Settings\\Application Data\\Mozilla Firefox\\firefox.exe");  
        FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary);

        // **** This is the line that changes.
        // **** Note the lack of the WebDriver type.
        driver = new FirefoxDriver(firefoxbin,null); 
        baseUrl = "http://cibnew.sofia.ifao.net:7001/cib_web/web";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    // The rest of your class should remain the same
}