Basically the same answer as rzwitserloot gave but in a different order.
java.lang.NullPointerException
The stack trace complains about a NullPointerException
. This means that a reference that should have pointed to an object was not pointing to anything (i.e. the reference was null).
So why was it null? For that you have to look at the next part of the stack trace. Each line describes a method. Each time a new method is called inside another a line is added. The format is <package>.<class>.<method>(<file:line>)
.
at java.util.Objects.requireNonNull(Objects.java:203)
at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:106)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:85)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
at Tests.Test(Tests.java:15)
So the first few lines aren't your code. The first line is from java, the other ones are from selenium. Your code starts at Tests.Test(Tests.java:15)
.
So what happens at this line?
WebDriverWait wait = new WebDriverWait(driver, 6000);
At this line you are creating a new instance of WebDriverWait
. You pass two arguments to the constructor. The second one is a number and clearly not null. The first one driver
is a reference to a WebDriver
. Because the other argument can't be null, this one probebly is.
So why would driver
be null
?
You've declared the driver
a field in the Hooks
class. And by default if you don't assign a value when declaring the field it's value is null.
public class Hooks
{
public WebDriver driver;
So problem located.
But you say, you are giving driver
a value in the Setup
method!
public class Hooks
{
public WebDriver driver;
@BeforeMethod
public void Setup() throws InterruptedException
{
...
WebDriver driver = new ChromeDriver();
...
}
Because you've added WebDriver
before driver = new ChromeDriver();
it means you've created a local variable with the same name as the class field driver
. Because local variables are more important then class fields the driver
name in the Setup
method now only refers to the local variable.
To assign the driver to the field you can either use this.driver = driver
after creating the new ChromeDriver
. Or you can remove WebDriver
and only write driver = new ChromeDriver();
to assign the new ChromeDriver
to the class field.