Getting Null Pointer Exception while running Selenium - TestNG Scripts using pagefactory
Getting Null Pointer Exception while running Selenium - TestNG Scripts using pagefactory
I have created 3 different classes like SelectBrowserTest, GmailLoginPOTest and SampleTest (this is my test case). i tried to call the methods from SelectBrowserTest and GmailLoginPOTest to SampleTest. But, Am continuously getting Null pointer exception. Can any one help to solve this?
Code
SampleTest:
package TestCases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.*;
import TCPSampleProject.SelectBrowserTest;
import pageObjects.GmailLoginPOTest;
public class SampleTest
static WebDriver driver;
private static GmailLoginPOTest g1;
@BeforeMethod
public void OpenBrowser()
SelectBrowserTest open = new SelectBrowserTest();
open.ChromeBrowser();
@AfterMethod
public void CloseBrowser()
SelectBrowserTest close = new SelectBrowserTest();
close.Close();
@Test(priority = 0)
public static void GmailLogin()
g1 = PageFactory.initElements(driver, GmailLoginPOTest.class);
g1.GmailLink.click();
System.out.println("Gmail Link clicked");
g1.SignInLink.click();
g1.EmailIDTextBox.sendKeys("rvigneshprabu");
g1.EmailNextButton.click();
g1.PasswordTextBox.sendKeys("Password");
g1.PswdNextButton.getText();
System.out.println("Gmail Login Completed");
GmailLoginPOTest:
package pageObjects;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class GmailLoginPOTest
@FindBy(linkText = "Gmail")
public WebElement GmailLink;
@FindBy(xpath = ".//a[contains(text(),'Sign In')]")
public WebElement SignInLink;
@FindBy(id = "identifierId")
public WebElement EmailIDTextBox;
@FindBy(id = "identifierNext")
public WebElement EmailNextButton;
@FindBy(name = "password")
public WebElement PasswordTextBox;
@FindBy(id = "passwordNext")
public WebElement PswdNextButton;
SelectBrowserTest:
package TCPSampleProject;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SelectBrowserTest
WebDriver driver;
public void ChromeBrowser()
System.setProperty("webdriver.chrome.driver",
"//Users//vigneshprabur//Documents//Automation//Drivers//chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
public void Close()
driver.quit();
System.out.println("Browser Closed");
public void ScreenShot()
try
File ScrShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(ScrShot, new File("/Users/vigneshprabur/Documents/Automation/Screenshots/image_"+System.currentTimeMillis()+".png"));
System.out.println("Screenshot Taken");
catch (IOException e)
System.out.println("Exception While Taking Screenshot" + e.getMessage());
These 3 are my class files. While running the script am getting the following error message.
Output:
FAILED CONFIGURATION: @AfterMethod CloseBrowser
java.lang.NullPointerException
at TCPSampleProject.SelectBrowserTest.Close(SelectBrowserTest.java:29)
at TestCases.SampleTest.CloseBrowser(SampleTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:455)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
FAILED: GmailLogin
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy9.click(Unknown Source)
at TestCases.SampleTest.GmailLogin(SampleTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:580)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Hi @AnkurSingh am running scripts from the Mac machine. so no need to add .exe and now browser is getting initiated but getting error in next step only.
– Vigneshprabu
Sep 8 '18 at 9:07
@Vigneshprabu, still facing the issue?
– Pradeep hebbar
Sep 9 '18 at 15:26
@Pradeephebbar, yes please help me to resolve this?
– Vigneshprabu
Sep 10 '18 at 6:15
1 Answer
1
Please try the below code.In your SampleTest class the driver is null.Thats why you got null pointer exception.
SampleTest.java
package TestCases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.*;
import TCPSampleProject.SelectBrowserTest;
import pageObjects.GmailLoginPOTest;
public class SampleTest
static WebDriver driver;
private static GmailLoginPOTest g1;
SelectBrowserTest open;
@BeforeMethod
public void OpenBrowser()
open = new SelectBrowserTest();
open.ChromeBrowser();
driver = open.getDriver();
@AfterMethod
public void CloseBrowser()
open.Close();
@Test(priority = 0)
public static void GmailLogin()
g1 = PageFactory.initElements(driver, GmailLoginPOTest.class);
g1.GmailLink.click();
System.out.println("Gmail Link clicked");
g1.SignInLink.click();
g1.EmailIDTextBox.sendKeys("rvigneshprabu");
g1.EmailNextButton.click();
g1.PasswordTextBox.sendKeys("Password");
g1.PswdNextButton.getText();
System.out.println("Gmail Login Completed");
SelectBrowserTest .java
package TCPSampleProject;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SelectBrowserTest
WebDriver driver;
public WebDriver getDriver()
return driver;
public void ChromeBrowser()
System.setProperty("webdriver.chrome.driver",
"//Users//vigneshprabur//Documents//Automation//Drivers//chromedriver");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
public void Close()
driver.quit();
System.out.println("Browser Closed");
public void ScreenShot()
try
File ScrShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(ScrShot, new File("/Users/vigneshprabur/Documents/Automation/Screenshots/image_"+System.currentTimeMillis()+".png"));
System.out.println("Screenshot Taken");
catch (IOException e)
System.out.println("Exception While Taking Screenshot" + e.getMessage());
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You are missing chromedriver.exe In System.setProperty("webdriver.chrome.driver", "//Users//vigneshprabur//Documents//Automation//Drivers//chromedriver//chromedriver.exe");
– Ankur Singh
Sep 8 '18 at 9:00