0

Just wanted to ask a simple question. I'm learning a bit about testing and once again I have a little problem.

In my code I want to call a method that will use JavascriptExecutor.

I write:

JavascriptExecutor js = (JavascriptExecutor)driver 

And after that I use it in method:

public void clickSearch()
{ js.executeScript("arguments[0].click()", sOmEtHiNg);}

But then, when I start my code I get : Null Pointer Exception.

But... when I simply use JavascriptExecutor in my method For example:

public void clickSearch()
{ ((JavascriptExecutor)driver).executeScript("arguments[0].click()",eight); }

Then everything is ok and I get no errors.

Someone could please tell me where I make a mistake that causes Null Pointer Exception ?

Thanks a lot :-)

2 Answers 2

1
public void clickSearch()
{ js.executeScript("arguments[0].click()", sOmEtHiNg);}

It looks like js could be null here, which would throw the exception. We need to ensure js is inside the scope for your clickSearch() method. Could you try this instead:

public void clickSearch()
{
    JavascriptExecutor js = (JavascriptExecutor)driver 
    js.executeScript("arguments[0].click()", sOmEtHiNg);
}

Or possibly:

JavascriptExecutor js = (JavascriptExecutor)driver;
clickSearch(js);

public void clickSearch(JavascriptExecutor js)
{
    js.executeScript("arguments[0].click()", sOmEtHiNg);
}
Sign up to request clarification or add additional context in comments.

Comments

0
public void clickSearch()
{
    JavascriptExecutor js = (JavascriptExecutor)driver 
    js.executeScript("arguments[0].click()", sOmEtHiNg);
}

This one works. Second one can not be used becouse of the different classes. I write it on one and run on another :-) Thanks a lot again :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.