“Cannot instantiate class” error while executing t

2019-03-01 18:38发布

问题:

I'm using Selenium and TestNG for the first time and I've been trying to search an element by its ID but I keep getting an "Cannot instantiate class" error. This is my code:

import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;

public class NewTesting {

    WebDriver driver = new FirefoxDriver();

    @BeforeTest
    public void setUp() {
        driver.get("http://book.theautomatedtester.co.uk/chapter1");
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testExample() {
        WebElement element = driver.findElement(By.id("verifybutton"));
    }

}

Maybe I missed installing something? I installed the TestNG plug-in for eclipse and added the WebDriver JAR files, do I need to do more? I tried following multiple tutorials but I keep getting errors, I hope someone can help. Thanks in advance!

EDIT: I now have this:

public class NewTest {
     private WebDriver driver;

    @BeforeTest
    public void setUp() {
        System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Selenium\\FirefoxDriver\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://book.theautomatedtester.co.uk/chapter1");
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testExample() {
        WebElement element = driver.findElement(By.id("verifybutton"));
    }

}

It does open the website now but I'm getting a nullpointer exception now:

FAILED CONFIGURATION: @AfterTest tearDown java.lang.NullPointerException at NewTest.tearDown(NewTest.java:21)

回答1:

Replace this set of imports:

import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;

With:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;

Additionally, you have to download the required format of GeckoDriver executable from mozilla/geckodriver, extract the binary and then initialize the FirefoxDriver.

Your effective code block will be:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;

public class NewTesting {

    WebDriver driver;

    @BeforeTest
    public void setUp() {
        System.setProperty("webdriver.gecko.driver","C:\\path\\to\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.get("http://book.theautomatedtester.co.uk/chapter1");
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }

    @Test
    public void testExample() {
        WebElement element = driver.findElement(By.id("verifybutton"));
    }

}


回答2:

If you're on windows, this previous question may be some help to you.

It mentions that you can download geckodriver, and then initialize your FirefoxDriver like this:

System.setProperty("webdriver.gecko.driver","G:\\Selenium\\Firefox driver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();