Home > Software engineering >  Null pointer exception while running selenium test. I am using Page object model
Null pointer exception while running selenium test. I am using Page object model

Time:05-11

Below is my Testbase class:

public class TestBase {
    
    public static WebDriver driver;  //Initialise web-driver
    public static Properties prop;
    public TestBase() {
        
        try { 
            prop= new Properties();
            FileInputStream ip= new FileInputStream("/Users/admin/Desktop/BASE_AutomationTests/src/main/java/com/base/Config/config.properties");
            prop.load(ip);
            
        }catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //** Write the code for reading the property of browser 
    public static void initialization() {
        String browserName=prop.getProperty("browser");
        
        if (browserName.equals("chrome"))
        {
            System.setProperty("webdriver.chrome.driver", "/C:/Users/admin/Desktop/BASE-Automation/chromedriver_win32/chromedriver.exe/");
            driver = new ChromeDriver();
        }
        
            driver.manage().window().maximize();
            driver.get(prop.getProperty("url"));
        
        
    }
}

My VerifyLaydownlogintest as below:

public class VerifyLaydownLoginTest extends TestBase {
    //
    LoginPage loginpage;
    HomePage homepage;
    
    //**Creating a constructor with Super so that after construction is called the Super keay word initialise properties of TestBase class first 
     //** Super keyword = to call super class constructor i.e TestBase class 
    public VerifyLaydownLoginTest() 
    {
        super();                                             
    }
    
    
    @BeforeMethod
    public void setUp()
    {
            initialization();
            loginpage= new LoginPage();
            
    }
    
    @Test(priority= 1)
    public void loginTest() {
        loginpage.loginToBase(prop.getProperty("username"),prop.getProperty("password"));
    }
    
    @Test(priority=2)
    public void loginLaydownTest() 
    {
        homepage=loginpage.changeLogin();   //**changeLogin method is returning you the object of Homepage class, so we can store it in a object
    }
    
    @AfterMethod
    public void tearDown()
    {
        driver.close();
    }
    
            
    }

And this is my Login page class:

/**
 * 
 */
package com.base.Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import com.base.Baseclass.TestBase;

/**
 * @author ketaki
 * This class will store all the locators and methods of Login page
 *
 */
public class LoginPage  extends TestBase{
    
    @FindBy(id="inputUsername") // Same as writing driver.findelement 
    WebElement username;
    
    @FindBy(id="inputPassword")
    WebElement password;
    
    @FindBy(name="_submit")
    WebElement submit_button;

//*** Initialise all these elements with the help pf page factory
    public LoginPage () {
        PageFactory.initElements(driver, this);
    }
    
//** Write all the login functions methods
    public void loginToBase(String userid, String pass)
    {
         
         username.sendKeys(userid);
         password.sendKeys(pass);
         submit_button.click();
    }
    public HomePage changeLogin() 
        {
        
         driver.navigate().to("https://uat-heineken.base.website/?_switch_user=alaric.james");
         driver.get("https://uat-heineken.base.website/#!/budget/?tab=edit");
         
         return  new HomePage(); 
                 
         
         }
}

This is error of NPE:

RemoteTestNG] detected TestNG version 6.14.3
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
FAILED CONFIGURATION: @BeforeMethod setUp
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.manage()" because "com.base.Baseclass.TestBase.driver" is null
    at com.base.Baseclass.TestBase.initialization(TestBase.java:42)
    at VerifyLaydownLoginTest.setUp(VerifyLaydownLoginTest.java:40)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    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:458)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:523)
    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)

I tried to resolve this but unable to find the root cause. Where am I going wrong?

My config.properties

url = https://uat-heineken.base.website/#!/

username = ketaki.naik
password = 123456

browser = chrome 

CodePudding user response:

You are using TestNG as your test automation framework.

In TestNG, execution will take place according to the annotation that you define in your test class.

@BeforeMethod has higher priority than @Test or @AfterMethod

and if you take a look at your @BeforeMethod

@BeforeMethod
public void setUp()
{
        initialization();
        loginpage= new LoginPage();
}

the first method that you are calling is initialization(); and the first line in that method is String browserName=prop.getProperty("browser");

You are calling prop here from public static Properties prop; and it is a static variable and you've not initialized it there, hence you are getting the NPE. Since prop is just a variable that you are trying to access.

You should rather create an object of TestBase class in your @BeforeMethod

Something like this:

@BeforeMethod
public void setUp()
{
        TestBase base = new TestBase();
        base.initialization();
        loginpage= new LoginPage();
        
}

Now once you create an instance of TestBase like above, the below constructor will get invoked

public TestBase() {
    
    try { 
        prop= new Properties();
        FileInputStream ip= new FileInputStream("/Users/admin/Desktop/BASE_AutomationTests/src/main/java/com/base/Config/config.properties");
        prop.load(ip);
        
    }catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }
}

And since prop is a static variable, the assigned value to this variable will persist.

  • Related