Java Lang NullPointerException in Selenium,While running the selenium test scripts you'll face some challenges with the test scripts,in that challenges this is the main important exception,not able to understand why it is displaying and most of the time you'll think that it is raised because of element is not identifying by the Selenium,that is not the proper answer.
Java Lang NullPointerException in Selenium |
Write TestCase is PASS/FAIL In seleniumTestNG Classpath errorSession ID is null using webdriver after calling quitHow to send extent reports in email with screenshots
Java Lang NullPointerException in Selenium
When coming to Java Lang NullPointerException in Selenium,when passing single webdriver instance to multiple classes,you'll face this type of problems.Please read below instructions to resolve the same issue in Selenium.
SelDriver Class:
package com.sel.Selenium.Practise;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;
public class SelDriver {
public WebDriver driver;
String baseurl = "http://www.google.co.in";
@BeforeSuite
public void openBrowser(){
driver = new FirefoxDriver();
//driver = new HtmlUnitDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
}
GoogleSite Class:
package com.sel.Selenium.Practise;
import org.testng.Assert;
import org.testng.annotations.Test;
public class GoogleSite extends SelDriver {
@Test
public void seleniumElements() throws InterruptedException{
driver.get(baseurl);
System.out.println("Open Google site");
String Title=driver.getTitle();
Assert.assertEquals(Title, "Google");
try {
Assert.assertEquals(driver.getTitle(), "Google's");
} catch (Error e) {
System.out.println(e.getCause());
}
}
}
I', extending the class from SelDriver main class to use WebDriver instance,But when running the above class,you'll face same issue i.e Java.Lang.NullPointerException for some methods.In order to resolve this issue,you need to add static for WebDriver declaration as below.
package com.sel.Selenium.Practise;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;
public class SelDriver {
public static WebDriver driver;
String baseurl = "http://www.google.co.in";
@BeforeSuite
public void openBrowser(){
driver = new FirefoxDriver();
//driver = new HtmlUnitDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
}
Before Static Method Test Results:
After Changes - Test Results
[TestNG] Running:
E:\QTPTutorials\Selenium_Workspace\Selenium_Tutorials\testng.xml
Open Google site
null
driver=FirefoxDriver: firefox on WINDOWS (f2f86423-9806-4b4a-ae92-7504e3db59be)
Enter Search Keyword
Clear Enter Keyword
clear works
===============================================
GoogleTestSuite
Total tests run: 2, Failures: 0, Skips: 0
===============================================
TestNG Test Results |
Please provide your valuable comments on this solution in case it is resolved your java.lang.nullpointer exception in Selenium.
No comments:
Post a Comment