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)