1

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)

Rugo
  • 349
  • 1
  • 6
  • 14
  • 1
    Can you please add more details of Exception? Better to provide full exception to see it. – Ukrainis Oct 30 '18 at 18:49
  • The best way to handle imports is to hover the red squggled text and fix your imports that way. That way you get the right import (assuming you make the right choices if there are more than one). – JeffC Oct 30 '18 at 20:38
  • Your question has been asked and answered. If you fixed your original question, open a new one with the details of the new error... don't keep editing this one with further issues. It makes it impossible for future readers to understand what is going on and is confusing on what answers are valid for the current question. – JeffC Oct 30 '18 at 20:39

2 Answers2

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"));
    }

}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried this but now it cant recognize the driver in "AfterTest" and "Test". – Rugo Oct 30 '18 at 19:23
  • Checkoutthe updated solution and let me know the status. – undetected Selenium Oct 30 '18 at 19:34
  • 1
    This is now working just found my mistake as well :) There's still a bunch of red text though, but at the end it says: Default test Tests run: 1, Failures: 0, Skips: 0 Default suite Total tests run: 1, Failures: 0, Skips: 0 so is the red text normal? – Rugo Oct 30 '18 at 19:39
  • @Rugo I am not sure about the ...red text... you are seeing. Possibly those are WARNINGS and we can live with those as you got **Default test Tests run: 1, Failures: 0, Skips: 0** – undetected Selenium Oct 30 '18 at 19:55
0

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();
AbsoluteSpace
  • 710
  • 2
  • 11
  • 21