null pointer exception_using java selenium webdriver with TestNG - selenium

when execute the code below , null pointer exception is occures, as driver of class Pom_MainHerokuapp is always null
testcases:-
package testcases;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import poms.Pom_MainHerokuapp;
import testbase.TestBase;
public class MainHerokuapp extends TestBase {
Pom_MainHerokuapp mainHerokuappObject;
public MainHerokuapp() {
mainHerokuappObject = new Pom_MainHerokuapp(driver);
}
#Test(priority = 0)
public void TestMainpagetitle() {
mainHerokuappObject.VerifyTitles();
}
#Test(priority = 1)
public void TestABTestingText() {
mainHerokuappObject.VerifyTextOfABTesting();
}
}
TestBase class:-
package testbase;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
public class TestBase {
public WebDriver driver;
#BeforeTest
public void setup() {
driver = new FirefoxDriver();
driver.get("https://the-internet.herokuapp.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}
Also:
package poms;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
public class Pom_MainHerokuapp {
public WebDriver driver;
String text;
String StringMaintext;
String Stringsubtitle;
#FindBy(xpath = "//html//body//div[2]//div//h1")
WebElement Maintitle;
#FindBy(xpath = "//html//body//div[2]//div//h2")
WebElement Subtitle;
#FindBy(linkText = "A/B Testing")
WebElement ABTesting;
#FindBy(xpath = "//html//body//div[2]//div//div//h3")
WebElement ABTestingText;
create constructor of this class
public Pom_MainHerokuapp(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this); // Initialization all webelements
}
public void VerifyTitles() {
StringMaintext = Maintitle.getText();
Stringsubtitle = Subtitle.getText();
System.out.println(StringMaintext);
System.out.println(Stringsubtitle);
Assert.assertEquals(StringMaintext, "Welcome to the Internet");
Assert.assertEquals(Stringsubtitle, "Available Examples");
}
public void VerifyTextOfABTesting() {
ABTesting.click();
text = ABTestingText.getText();
System.out.println(text);
Assert.assertEquals(text, "A/B Test Variation 1");
}
}
the error is :-
FAILED: TestMainpagetitle 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.$Proxy7.getText(Unknown Source) at
poms.Pom_MainHerokuapp.VerifyTitles(Pom_MainHerokuapp.java:36) at
testcases.MainHerokuapp.TestMainpagetitle(MainHerokuapp.java:28) 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:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639) at
org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816) at
org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124) at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774) at
org.testng.TestRunner.run(TestRunner.java:624) at
org.testng.SuiteRunner.runTest(SuiteRunner.java:359) at
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) at
org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) at
org.testng.SuiteRunner.run(SuiteRunner.java:261) at
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at
org.testng.TestNG.runSuitesSequentially(TestNG.java:1215) at
org.testng.TestNG.runSuitesLocally(TestNG.java:1140) at
org.testng.TestNG.run(TestNG.java:1048) at
org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112) at
org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205) at
org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)
FAILED: TestABTestingText 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.$Proxy7.click(Unknown Source) at
poms.Pom_MainHerokuapp.VerifyTextOfABTesting(Pom_MainHerokuapp.java:46)
at testcases.MainHerokuapp.TestABTestingText(MainHerokuapp.java:33)
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:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639) at
org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816) at
org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124) at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774) at
org.testng.TestRunner.run(TestRunner.java:624) at
org.testng.SuiteRunner.runTest(SuiteRunner.java:359) at
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) at
org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) at
org.testng.SuiteRunner.run(SuiteRunner.java:261) at
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at
org.testng.TestNG.runSuitesSequentially(TestNG.java:1215) at
org.testng.TestNG.runSuitesLocally(TestNG.java:1140) at
org.testng.TestNG.run(TestNG.java:1048) at
org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112) at
org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205) at
org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)

This isn't hard to explain. The default constructor of your test class MainHerokuapp will be called as soon as it is run, i.e. when driver is still null - before your #BeforeTest method, where driver gets set.
MainHerokuapp and TestBase seem to be mixed-up rather than having a clear separation, so you'd be better off merging them back into one.
Another way is to restore control to the child class by dropping the constructor, moving the #BeforeTest there, and calling up to the parent. This definitely works:
public class MainHerokuapp extends TestBase {
Pom_MainHerokuapp mainHerokuappObject;
#BeforeTest
public void setup() {
super.setup();
mainHerokuappObject = new Pom_MainHerokuapp(driver);
}
#Test(priority = 0)
public void TestMainpagetitle() {
mainHerokuappObject.VerifyTitles();
}
#Test(priority = 1)
public void TestABTestingText() {
mainHerokuappObject.VerifyTextOfABTesting();
}
}
public class TestBase {
public WebDriver driver;
// #BeforeTest
public void setup() {
driver = new FirefoxDriver();
driver.get("https://the-internet.herokuapp.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}

I also get the same error while I didnt mention the static but when I wrote
public static WebDriver driver = null;
in TestBase class it was working fine.

Related

Getting java.lang.NullPointerException in Selenium WebDriver

I am getting java.lang.NullPointerException when I run the testcase in Eclipse. Can somebody help me in pointing out the error I have made.
Error # Line 17: WebElement in LoginPage.Java.
# Line 12: LoginPage in TC_LoginTest_001.java.
**LoginPage.Java**
package com.internetBanking.pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LoginPage {
WebDriver driver;
public LoginPage (WebDriver driver) {
this.driver = driver;
}
WebElement usrname = driver.findElement(By.name("uid"));
WebElement pwd = driver.findElement(By.name("password"));
WebElement login = driver.findElement(By.name("btnLogin"));
public void setUsrname(String uname) {
usrname.sendKeys(uname);
}
public void setPwd(String pswd) {
pwd.sendKeys(pswd);
}
public void login() {
login.click();
}
}
**TC_LoginTest_001.java**
package com.internetBanking.testCases;
import org.testng.annotations.Test;
import com.internetBanking.pageObjects.LoginPage;
public class TC_LoginTest_001 extends BaseClass {
#Test
public void LoginTest() {
driver.get(baseURL);
logger.info("URL is opened");
LoginPage loginPage = new LoginPage(driver);
loginPage.setUsrname(username);
logger.info("Username is entered");
loginPage.setPwd(password);
logger.info("Password is entered");
loginPage.login();
logger.info("Login button is clicked");
}
}
When I run the test case, I am getting the below errors:
java.lang.NullPointerException
at com.internetBanking.pageObjects.LoginPage.(LoginPage.java:17)
at com.internetBanking.testCases.TC_LoginTest_001.LoginTest(TC_LoginTest_001.java:12)
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:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
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:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Consider this code:
public class MainTest {
public static void main(String[] args) {
new Test(new Object());
}
}
class Test {
InitMe initMe = new InitMe();
public Test(Object o){
System.out.println("Test instance has been created");
}
}
class InitMe{
public InitMe(){
System.out.println("Init me instance has been created");
}
}
If you run it you will see the following output:
Init me instance has been created
Test instance has been created
which means that fields are initialized before the constructor is executed. So in your case you have the following structure:
public class MainTest {
public static void main(String[] args) {
new Test(new Object());
}
}
class Test {
Object o;
String oStr = o.toString();
public Test(Object o){
this.o = o;
}
}
where your fields are initialized through the reference that has not been yet initialized itself (because the constructor has not yet been called).
Your particular solution would be to change this:
WebDriver driver;
public LoginPage (WebDriver driver) {
this.driver = driver;
}
WebElement usrname = driver.findElement(By.name("uid"));
WebElement pwd = driver.findElement(By.name("password"));
WebElement login = driver.findElement(By.name("btnLogin"));
to this:
WebDriver driver;
WebElement usrname;
WebElement pwd;
WebElement login;
public LoginPage (WebDriver driver) {
this.driver = driver;
usrname = driver.findElement(By.name("uid"));
pwd = driver.findElement(By.name("password"));
login = driver.findElement(By.name("btnLogin"));}
}

Throws "Java.lang.AssertionError: Unsupported type parameter" When we pass DriverManagerType browser in TESTNG

Thanks for providing the webdrivermanager-3.7.1 Jar.
I have written small Selenium Webdriver+TestNG program. I am using #Parameters({"browser","URL"}) in my code and pass DriverManagerType.
When I executed TestNG or this class,It throws "java.lang.AssertionError: Unsupported type parameter :classio.github.bonigarcia.wdm.DriverManagerType".
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.DriverManagerType;
import io.github.bonigarcia.wdm.WebDriverManager;
public class testNewSeleniumManager {
public static WebDriver driver;
#BeforeTest
#Parameters({"browser","URL"})
public void lunchBrowser(#Optional("optional value") DriverManagerType browser,String URL) throws Throwable
{
WebDriverManager.getInstance(browser).setup();
WebDriver driver = new ChromeDriver();
}
#Test
public void URL1()
{
driver.get("https://dzone.com/articles/webdriver-manager-hassle-free-browser-binary-manag");
}
}
TrackStace:
java.lang.AssertionError: Unsupported type parameter : class io.github.bonigarcia.wdm.DriverManagerType
at org.testng.internal.Parameters.convertType(Parameters.java:246)
at org.testng.internal.Parameters.createParameters(Parameters.java:165)
at org.testng.internal.Parameters.createParameters(Parameters.java:355)
at org.testng.internal.Parameters.createConfigurationParameters(Parameters.java:86)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:199)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:641)
at org.testng.TestRunner.run(TestRunner.java:609)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)
at org.testng.TestNG.run(TestNG.java:1031)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

Null Pointer Exception in Selenium WebDriver in second page after successful login Page

How to resolve below exception.
Here is code
package com;`
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Browserfactory {
static WebDriver driver;
public static WebDriver startBrowser(String browserName)
{
if(browserName.equalsIgnoreCase("firefox"))
{
System.setProperty("webdriver.firefox.marionette","geckodriver.exe");
driver = new FirefoxDriver();
}
else if(browserName.equalsIgnoreCase("chrome"))
{
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
driver = new ChromeDriver();
}
return driver;
}
}
Accessing from another class.
below code is written in different class and accessing this as a Base class.
i wanted to make Logout method as utility, when ever required to logout just call it and get done.
package com;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Guru99 {
//creating a guru99 bank page login verify.
public WebDriver driver=null;
private static final String URL="http://demo.guru99.com/v4/";
#BeforeTest
//pre-requisition
public void launchpage()
{
WebDriver driver = Browserfactory.startBrowser("firefox");
driver.get(URL);
driver.findElement(By.name("uid")).sendKeys("mngr117051");
driver.findElement(By.name("password")).sendKeys("EhYtErY");
driver.findElement(By.name("btnLogin")).click();
}
#Test
public void logout()
{
driver.findElement(By.xpath("html/body/div[2]/div/ul/li[15]/a")).click();
}
}
//Running code through TestNG.
Getting Error
FAILED: logout
java.lang.NullPointerException
at com.Guru99.logout(Guru99.java:26)
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: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:1137)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:753)
at org.testng.TestRunner.run(TestRunner.java:607)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:368)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
at org.testng.SuiteRunner.run(SuiteRunner.java:270)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
at org.testng.TestNG.runSuites(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1096)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
That is because of the mistake you have done in your code.
I didn't understand why you have declared driver in 3 places in your code. Whenever you declare same variable again and again its previous value get override every time and that is the reason you are getting NullPointerException.
I have modified your code and verified the working also.
public class Browserfactory {
public static WebDriver startBrowser(WebDriver driver,String browserName) {
if (browserName.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.firefox.marionette","geckodriver.exe");
driver = new FirefoxDriver();
} else if (browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
driver = new ChromeDriver();
}
return driver;
}
}
public class Guru99 {
// creating a guru99 bank page login verify.
public WebDriver driver = null;
private static final String URL = "https://demo.guru99.com/v4";
#BeforeTest
// pre-requisition
public void launchpage() {
driver = Browserfactory.startBrowser(driver,"firefox");
driver.manage().window().maximize();
driver.get(URL);
driver.findElement(By.name("uid")).sendKeys("mngr117051");
driver.findElement(By.name("password")).sendKeys("EhYtErY");
driver.findElement(By.name("btnLogin")).click();
}
#Test
public void logout() {
driver.findElement(By.xpath("html/body/div[2]/div/ul/li[15]/a")).click();
}
}
Hope this will help you.

Selenium pageFactory NullPointerException for driver

Selenium pageFactory NullPointerException. Any help will be appreciated. It works for the login of setUp(), but after that driver comes up null.
public class TestBase {
public WebDriver driver;
String url = PropertiesFile.readPropertiesFile("autUrl");
public WebDriver getDriver() {
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir") + "/driver/chromedriver.exe");
driver = new ChromeDriver();
return driver;
}
public void getUrl(String url) {
driver.get(url);
driver.manage().window().maximize();
}
public void init() {
getDriver();
getUrl(url);
}
}
public class LogInPage extends TestBase {
WebDriver driver;
#FindBy(id = "loginEmail")public WebElement userName_field;
#FindBy(name = "password")public WebElement password_field;
#FindBy(xpath = "//*[#id='main-content']/aside/div/form/input[2]")public WebElement SignMeIn_btn;
public LogInPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
// Login
public void logIn(String userName, String password) {
userName_field.sendKeys(userName);
password_field.sendKeys(password);
SignMeIn_btn.click();
}
}
public class LogIn extends TestBase {
LogInPage logInPage;
private String user = PropertiesFile.readPropertiesFile("user");
private String password = PropertiesFile.readPropertiesFile("password");
public LogIn(WebDriver driver) {
this.driver = driver;
}
public void setUp(){
init();
}
public void logIn(){
logInPage = new LogInPage(driver);
logInPage.logIn(user, password);
}
}
public class PortalsPage extends TestBase {
WebDriver driver;
#FindBy(id = "loginEmail") public WebElement userName_field;
#FindBy(xpath=".//*[#id='tabDetail']") public WebElement tenantPage_li;
#FindBy(id="tabDetail") public WebElement ownerPage_li;
#FindBy(xpath="//a[contains(#href,'tenant.action')]") public WebElement tenantPortal_link;
#FindBy(xpath="//a[contains(#href,'owner.action')]") public WebElement ownerPortal_link;
public PortalsPage(WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void goToPortals(){
userName_field.sendKeys("a");
tenantPage_li.click();
}
}
public class Portals extends TestBase {
PortalsPage portals;
WebDriver driver;
#BeforeClass
public void setUp(){
LogIn login = new LogIn(driver);
login.setUp();
login.logIn();
}
#Test
public void goToPortal(){
portals = new PortalsPage(driver);
portals.goToPortals();
}
}
Following exception I got:
FAILED: goToPortal 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.$Proxy6.sendKeys(Unknown Source) at
com.demo.pages.PortalsPage.goToPortals(PortalsPage.java:30) at
com.demo.control.Portals.goToPortal(Portals.java:28) 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:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) at
org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at
org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at
org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767) at
org.testng.TestRunner.run(TestRunner.java:617) at
org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at
org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at
org.testng.SuiteRunner.run(SuiteRunner.java:240) at
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at
org.testng.TestNG.runSuitesSequentially(TestNG.java:1198) at
org.testng.TestNG.runSuitesLocally(TestNG.java:1123) at
org.testng.TestNG.run(TestNG.java:1031) at
org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Your current approach seems to be a bit convoluted. But without delving into the details of what other approach you can use to fix your overall design, here's a cleaned up version of your code, that should work.
The idea here is that you only need page classes (the ones that end with *Page which represent a particular page, and exposes some business functions that can be executed on that particular page) and test classes, with the test class themselves extending from your TestBase
So here are the classes
LoginPage.java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class LogInPage {
#FindBy(id = "loginEmail")
private WebElement userNameTextField;
#FindBy(name = "password")
private WebElement passwordTextField;
#FindBy(xpath = "//*[#id='main-content']/aside/div/form/input[2]")
private WebElement signInButton;
public LogInPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
public void logIn(String userName, String password) {
userNameTextField.sendKeys(userName);
passwordTextField.sendKeys(password);
signInButton.click();
}
}
PortalsPage.java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class PortalsPage {
#FindBy(id = "loginEmail")
private WebElement usernameTextField;
#FindBy(xpath = ".//*[#id='tabDetail']")
private WebElement tenantPage;
#FindBy(id = "tabDetail")
private WebElement ownerPage;
#FindBy(xpath = "//a[contains(#href,'tenant.action')]")
private WebElement tenantPortalLink;
#FindBy(xpath = "//a[contains(#href,'owner.action')]")
private WebElement ownerPortalLink;
public PortalsPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
public void goToPortals() {
usernameTextField.sendKeys("a");
tenantPage.click();
}
}
TestBase.java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestBase {
private WebDriver driver;
protected WebDriver getDriver() {
if (driver != null) {
return driver;
}
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/driver/chromedriver.exe");
driver = new ChromeDriver();
return driver;
}
public void getUrl(String url) {
getDriver().get(url);
getDriver().manage().window().maximize();
}
}
PortalsTest.java
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class PortalsTest extends TestBase {
private PortalsPage portals;
private String user = PropertiesFile.readPropertiesFile("user");
private String password = PropertiesFile.readPropertiesFile("password");
private String url = PropertiesFile.readPropertiesFile("autUrl");
#BeforeClass
public void setUp() {
LogInPage login = new LogInPage(getDriver());
getUrl(url);
login.logIn(user, password);
}
#Test
public void goToPortal() {
portals = new PortalsPage(getDriver());
portals.goToPortals();
}
}

Null pointer exception when calling a Page Object Model class

I am trying my hands on Selenium test automation using TestNG as the test framework. For this, I have used a Page Object pattern to model each of the page of the website that I am writing the test for.
I have two java classes
SignInPage.java (Page Object Model Class)
TestLogin.java (where the actual test is written and where the SignInPage is instantiated)
SignInPage.java
public class SignInPage {
private WebDriver driver;
#FindBy(id = "username")
private WebElement usernameTextbox;
#FindBy(id = "password")
private WebElement passwordTextbox;
#FindBy(xpath = "//*[#id=\"left\"]/div[3]/div/form/input[3]")
private WebElement loginButton;
public SignInPage(WebDriver driver) {
this.driver = driver;
driver.get("www.somewebsite.com");
}
public HomePage performLogin(String username, String password){
usernameTextbox.sendKeys(username);
passwordTextbox.sendKeys(password);
loginButton.click();
return PageFactory.initElements(driver, HomePage.class);
}
}
TestLogin.java
public class TestLogin {
public WebDriver driver;
#BeforeClass
public void setup(){
System.setProperty("webdriver.chrome.driver", "path_to_chromedriver.exe");
driver = new ChromeDriver();
}
#Test
public void TestSignIn(){
SignInPage signInPage = new SignInPage(driver);
HomePage homePage = signInPage.performLogin("someusername","somepassword");
}
}
My problem is whenever the signInPage.performLogin() is invoked in the #Test of TestLogin.java, a null pointer exception is encountered.
java.lang.NullPointerException
at com.tipidpc.webpages.SignInPage.performLogin(SignInPage.java:30)
at com.tipidpc.tests.TestLogin.TestSignIn(TestLogin.java:26)
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:104)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:756)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
at org.testng.TestNG.runSuites(TestNG.java:1133)
at org.testng.TestNG.run(TestNG.java:1104)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
I solved the issue. Since I was using the TestNG framework to run my tests, the proper imports must be present for the test script to run properly.
All my driver initialization is being done in the #BeforeClass method but it uses the import org.junit.BeforeClass. The solution was to change this to org.testng.annotations.BeforeClass.