TestNG error null point exception error - java

Hi I am receiving following error can someone help me out with debugging below code,
package testngpackg;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class ARXNewTest {
ChromeDriver driver;
#BeforeMethod
public void set() {
//ProfilesIni profile = new ProfilesIni();
//FirefoxProfile testprofile = profile.getProfile("default");
// testprofile.setAcceptUntrustedCertificates(true);
//testprofile.setAssumeUntrustedCertificateIssuer(true);
System.setProperty("webdriver.chrome.driver", "C:\\Selenium Web Driver 3.0.1\\geckodriver-v0.12.0-win32\\geckodriver.exe");
WebDriver driver = new ChromeDriver();
String baseURL = "<URL>";
driver.get(baseURL);
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
}
#Test
public void OpenBrowser() {
driver.findElement(By.linkText("Log In")).click();
driver.switchTo().frame(0);
driver.findElement(By.id("tx_username")).sendKeys("my email id");
}
}
Error
FAILED: OpenBrowser
java.lang.NullPointerException
at testngpackg.ARXNewTest.OpenBrowser(ARXNewTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129)
I am getting null point error while execution of above code

You find a NullPointerException cause driver is not getting initialized before that point. You tried to initialize a WebDriver instance inside beforeMethod() method, but it was local.
Use following code :
driver = new ChromeDriver();
Instead
WebDriver driver = new ChromeDriver();
Hope it will help you.

Here is the solution to your Question-
A few words about the Solution:
Avoid creating unnecessary instances. You have ChromeDriver driver; but not used it anywhere.
Keep your methods & code-blocks separated so that you can identify them properly.
Remove the unwanted code when you are asking for help //ProfilesIni profile = new ProfilesIni();
In System.setProperty you have mentioned the Key as webdriver.chrome.driver but you have provided the value as C:\\Selenium Web Driver 3.0.1\\geckodriver-v0.12.0-win32\\geckodriver.exe
Keep you code properly formatted so that it is easier to understand code-blocks.
Manage the opening & closing of { & } so you don't run into unexpected results.
Provide names to the methods as per the functiosn achieved through the code within. Your OpenBrowser() method have nothing to do with Browser opening.
Here is the working set of your own code with some minimal tweaks:
public class Q43910679_null_pointer
{
#BeforeMethod
public void set()
{
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseURL = "https://gmail.com";
driver.get(baseURL);
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
}
#Test
public void OpenBrowser()
{
System.out.println("Open Browser Method");
}
}
Let me know if this Answers your Question.

Related

Multiple login sessions not getting closed with driver.close in java-selenium

I have added code snippet for further clarity.
I am using java-selenium-testNG and trying to login to a website zoho.com with 3 accounts and verifying if success. I have looked through somewhat similar issues but haven't found a solution that works in my case.
I instantiate the ChromeDriver in #BeforeMethod. Then I login to the website with first account and close the browser in my #AfterMethod.
I have to re-instantiate the browser with Driver = new ChromeDriver to login back with a new account as the instance is closed or atleast that's the feeling it gives error that the session id doesn't exists.
My issue is only the last instance of the driver is getting closed as I have driver.quit in the #AfterTest method. The other two instances remain in memory.
I have also tried using the same instance without closing the browser but in such cases the login option is not available on that particular website and my subsequent tests fail.
here is the code below for further clarification
package uk.dev.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.util.concurrent.TimeUnit;
public class zohoLogin {
WebDriver driver=null;
String winHandleBefore;
#DataProvider(name ="testData")
public Object[][] loginPasswordData()
{
return new Object[][]{
{"xxx#gmail.com",new String ("somepassword")} ,
{"yyy#gmail.com",new String ("somepassword")},
{"zzz#gmail.com",new String ("somepassword")},
};
}
#Test ( dataProvider = "testData")
public void enterUserDetails(String userEmail, String userPassword) throws InterruptedException {
driver.findElement(By.xpath("//input[#id='login_id']")).sendKeys(userEmail);
System.out.println(("Running for Test data " + userEmail));
Thread.sleep(2000);
driver.findElement(By.xpath("//button[#id='nextbtn']")).click();
Thread.sleep(2000);
System.out.println("clicked on Next");
driver.findElement(By.xpath("//input[#id='password']")).sendKeys(userPassword);
System.out.println("Entered the password");
driver.findElement(By.xpath("//button[#id='nextbtn']//span[contains(text(),'Sign in')]")).click();
System.out.println("Clicked on Signin");
Thread.sleep(2000);
}
#BeforeMethod
public void loginZoho() throws InterruptedException
{
driver = new ChromeDriver();
driver.get("https://www.zoho.com");
System.out.println("Open the browser");
driver.findElement(By.xpath("//a[#class='zh-login']")).click();
//driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
Thread.sleep(5000);
}
#AfterMethod
public void closeBrosers() throws InterruptedException {
Thread.sleep(2000);
driver.close();
System.out.println("Close the browser");
}
#BeforeTest
public void setupBrowser()
{ WebDriverManager.chromedriver().setup();
}
#AfterTest
public void tearDown()
{
driver.quit();
}
}
Attached the run results where 2 chrome driver instance can be seen.Also note the AfterMethod is not getting executed.
enter image description here
I am using driver.quit() method in selenium but facing the same issue. I think issue is related to the latest version of chromedriver.
public static void CloseDriver()
{
if (Driver != null)
{
Driver.Quit();
Driver = null;
}
}
I even tried Driver.Kill() but it resulted in closing all the browsers.

selenium not get http

I tried below code in eclipse. When I run this code firefox will open but driver.get("https://www.easybooking.lk/login"); not working. Please help me in resolving this error
package login;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class easylogin {
//public static void main(String[] args) {
// TODO Auto-generated method stub
public static void main(String[] args) throws InterruptedException {
//Object webdriver;
System.setProperty("webdriver.gecko.driver", "D:\\jjpppp\\geckodriver-v0.17.0-win64/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
driver.get("https://www.easybooking.lk/login");
I am getting below error. How can I fix this? I added selenium firefox drivers
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:185)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:120)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:641)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteTimeouts.implicitlyWait(RemoteWebDriver.java:872)
at login.easylogin.main(easylogin.java:20)
As per error message, it seems there is some problem in implicitlyWait. It does not seems to be working. Just comment this code and check once
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
Have you tried to change the path to your driver
in the following way:
D:\\jjpppp\\geckodriver-v0.17.0-win64\\geckodriver.exe
You might also have a look at the following change in order to be sure that your DOM is completely loaded:
WebDriverWait logWait = new WebDriverWait(driver, 10);
logWait.until(ExpectedConditions.presenceOfElementLocated(by));
driver.findElement(...)
The issue is because your browser and geckodriver are not compatible.
I tried with latest firefox version(56.0) and geckodriver 18. It worked fine.
Then I tried firefox(56.0) and geckodriver 17. It gave me similar issue.
So better use latest firefox and geckodriver.
try putting implicit wait after navigating to web address.
public static void main(String[] args) {
// TODO Auto-generated method stub
public static void main(String[] args) throws InterruptedException {
//Object webdriver;
System.setProperty("webdriver.gecko.driver", "D:\\jjpppp\\geckodriver-v0.17.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.easybooking.lk/login");
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
this will help you to wait till web-page loads next find element.
driver.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
remove this and try

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system

Following previous related issues posted and given resolution, I tired everything but still getting same error for FireFox, Chrome & Internet Explorer.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Search {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
System.getProperty("webdriver.gecko.driver",
"C:\\Users\\nitin\\Downloads\\geckodriver-v0.18.0-
win64\\geckodriver.exe");
driver.get("http://www.wikipedia.org");
WebElement link;
link = driver.findElement(By.linkText("English"));
link.click();
Thread.sleep(5000);
WebElement searchbox;
searchbox = driver.findElement(By.id("searchInput"));
searchbox.sendKeys("Software");
searchbox.submit();
Thread.sleep(5000);
driver.quit();
Shouldn't that be System.setProperty() instead of .getProperty()?
System.setProperty("webdriver.gecko.driver, "C:\\Users\\...\\geckodriver.exe");
Use that gecko driver system property before driver intialization
So first line gecko property and next line driver=new so and so..
Use .setProperty and declare it after providing the path to webdriver
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\nitin\\Downloads\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

Unable to run TestNG test case in Eclipse with ChromeDriver

I have written below code to open a site in chrome browser and verify its title. but when using System.setProperty() to set the ChromeDriver Path, it gives me syntax error and when I commented the line I get:
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property..
My Code :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class FirsttestNGFile {
String BaseURL = "http://newtours.demoaut.com/";
System.setProperty("webdriver.chrome.driver", "E:\\Automation Jars\\chromedriver_win32\\chromedriver.exe"); -- If I comment this line, I get Illegal state Exception for chromedriver path; if not commented , I get syntax error
WebDriver driver = new ChromeDriver();
#Test
public void verifyHomePageTitle() {
driver.get(BaseURL);
String ExpectedTitle = "Welcome: Mercury Tours";
String ActualTitle = driver.getTitle();
Assert.assertEquals(ExpectedTitle, ActualTitle);
driver.quit();
}
}
You cannot define System.setProperty Globally.
Use below code and try:
WebDriver driver;
#Before
public void browser(){
System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\CP-SAT\\Chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
}
#Test
public void verifyHomePageTitle() {
String BaseURL = "http://newtours.demoaut.com/";
driver.get(BaseURL);
String ExpectedTitle = "Welcome: Mercury Tours";
String ActualTitle = driver.getTitle();
Assert.assertEquals(ExpectedTitle, ActualTitle);
}
#Test
public void a() {
driver.get("https://www.google.co.in/?gfe_rd=cr&ei=6PDbV-qTAZHT8gecr4qQBA");
}
#After
public void close(){
driver.quit();
}
}
If you are using Junit then use #Before or If you are using TestNG then #BeforeTest.
Reply me for further query.
Happy Learning. :-)
You should consider to use https://github.com/bonigarcia/webdrivermanager which will make the job for you:
ChromeDriverManager.getInstance().setup();
Update chrome driver path in environmental variables path and then try to use below code in your script
#BeforeClass
public void setup() {
WebDriver driver = new ChromeDriver();
}

How to type in textbox using Selenium WebDriver (Selenium 2) with Java?

I am using Selenium 2.
But after running following code, i could not able to type in textbox.
package Actor;
import org.openqa.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.junit.*;
import com.thoughtworks.selenium.*;
//import org.junit.Before;
public class Actor {
public Selenium selenium;
public WebDriver driver;
#Before
public void setup() throws Exception{
driver = new FirefoxDriver();
driver.get("http://www.fb.com");
}
#Test
public void Test() throws Exception{
//selenium.type("id=gs_htif0", "test");
System.out.println("hi");
// driver.findElement(By.cssSelector("#gb_1 > span.gbts")).click();
selenium.waitForPageToLoad("300000000");
WebElement email=driver.findElement(By.id("email"));
email.sendKeys("nshakuntalas#gmail.com");
driver.findElement(By.id("u_0_b")).click();
}
#After
public void Close() throws Exception{
System.out.println("how are you?");
}
}
This is simple if you only use Selenium WebDriver, and forget the usage of Selenium-RC. I'd go like this.
WebDriver driver = new FirefoxDriver();
WebElement email = driver.findElement(By.id("email"));
email.sendKeys("your#email.here");
The reason for NullPointerException however is that your variable driver has never been started, you start FirefoxDriver in a variable wb thas is never being used.
Thanks Friend, i got an answer. This is only possible because of your help. you all give me a ray of hope towards resolving this problem.
Here is the code:
package facebook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Facebook {
public static void main(String args[]){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.facebook.com");
WebElement email= driver.findElement(By.id("email"));
Actions builder = new Actions(driver);
Actions seriesOfActions = builder.moveToElement(email).click().sendKeys(email, "gati.naveen#gmail.com");
seriesOfActions.perform();
WebElement pass = driver.findElement(By.id("pass"));
WebElement login =driver.findElement(By.id("u_0_b"));
Actions seriesOfAction = builder.moveToElement(pass).click().sendKeys(pass, "naveench").click(login);
seriesOfAction.perform();
driver.
}
}
You should replace WebDriver wb = new FirefoxDriver(); with driver = new FirefoxDriver(); in your #Before Annotation.
As you are accessing driver object with null or you can make wb reference variable as global variable.
Try this :
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("email")).sendKeys("emal#gmail.com");
Another way to solve this using xpath
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath(//*[#id='email'])).sendKeys("your#email.here");
Hope that will help. :)
You can use JavaScript as well, in case the textfield is dithered.
WebDriver driver=new FirefoxDriver();
driver.get("http://localhost/login.do");
driver.manage().window().maximize();
RemoteWebDriver r=(RemoteWebDriver) driver;
String s1="document.getElementById('username').value='admin'";
r.executeScript(s1);

Categories