1

I have a base class as follows where I load the property file and initialize browser

package com.mystore.base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

//import org.apache.log4j.xml.DOMConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeTest;

import io.github.bonigarcia.wdm.WebDriverManager;
import com.mystore.actiondriver.ActionDriver;


public class BaseClass1 {
    
    public static Properties prop;
    public static  WebDriver driver;
    //ActionDriver a = new ActionDriver();
    
    
    @BeforeTest
    public void loadConfig() {
        try {
            prop = new Properties();
            System.out.println("super constructor invoked");
            FileInputStream ip = new FileInputStream(
                    System.getProperty("user.dir") + "/Configuration/Config.properties");
            prop.load(ip);
            System.out.println("driver: "+ driver);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void launchApp() {
        
        String browserName = prop.getProperty("browser");
        
        if (browserName.equalsIgnoreCase("Chrome")) {
            WebDriverManager.chromedriver().setup();
            driver = new ChromeDriver();
            System.out.println("driver: "+ driver);
        } else if (browserName.equalsIgnoreCase("FireFox")) {
            WebDriverManager.firefoxdriver().setup();
            driver = new FirefoxDriver();
        } else if (browserName.equalsIgnoreCase("IE")) {
            WebDriverManager.iedriver().setup();
            driver = new InternetExplorerDriver();
        }
        
        ActionDriver.implicitWait(driver, 10);
        ActionDriver.pageLoadTimeOut(driver, 30);
        driver.get(prop.getProperty("url"));

    }
}

Index page class which inherits BaseClass and code below

package com.mystore.pageobjects;

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

import com.mystore.actiondriver.ActionDriver;
import com.mystore.base.BaseClass;
import com.mystore.base.BaseClass1;

public class IndexPage1 extends BaseClass1{
    
    @FindBy(xpath = "//a[@class='login']")
    WebElement signInBtn;
    
    @FindBy(xpath = "//img[@class='logo img-responsive']")
    WebElement myStoreLogo;
    
    @FindBy(id="search_query_top")
    WebElement searchProductBox;
    
    @FindBy(name="submit_search")
    WebElement searchButton;
    
    public IndexPage1() {
        PageFactory.initElements(driver, this);
    }
    
    public LoginPage clickOnSignIn() {
        ActionDriver.click(driver,signInBtn);
        return new LoginPage();
    }
    
    public boolean validateLogo() throws Throwable{
        System.out.println("Inside Validate Logo");
        return myStoreLogo.isDisplayed();
        
        //return true;
        //return ActionDriver.isDisplayed(driver, myStoreLogo);
    }
    
    public String getMyStoreTitle() {
        String myStoreTitle = driver.getTitle();
        return myStoreTitle;
    }
    
    public SearchResultPage searchProduct(String productName) throws InterruptedException {
        Thread.sleep(10000);
        searchProductBox.sendKeys(productName);
        Thread.sleep(10000);
        System.out.println("I passed!");
        ActionDriver.type(searchProductBox, productName);
        ActionDriver.implicitWait(driver, 60);
        ActionDriver.click(driver, searchButton);
        return new SearchResultPage();
    }
}

IndexPageTest class with test - where I try to call verifyLogo() test which checks if webElement myStoreLogo is displayed and should return true but returns false and NPE .

package com.mystore.testcases;

import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.mystore.base.BaseClass;
import com.mystore.base.BaseClass1;
import com.mystore.pageobjects.IndexPage;
import com.mystore.pageobjects.IndexPage1;

public class IndexPageTest1 extends BaseClass1{

    IndexPage1 indexPage1 = new IndexPage1();
    
    @BeforeMethod
    public void setup() {
        launchApp(); 
    }
    
    //@AfterMethod
    public void tearDown() {
        driver.quit();
    }
    
    @Test()
    public void verifyLogo() throws Throwable{

        //indexPage1= new IndexPage();
        boolean result=indexPage1.validateLogo();
        System.out.println("The value of result is:"+ result);
        Assert.assertTrue(result);
        
    }
    
    @Test(enabled = false)
    public void verifySearchProduct() throws InterruptedException {
        indexPage1.searchProduct("t-shirt");
    }
    
    @Test(enabled=false)
    public void verifyTitle() {
        
        String actTitle=indexPage1.getMyStoreTitle();
        Assert.assertEquals(actTitle, "My Store");
        
    }
}

Gives results below

driver: ChromeDriver: chrome on MAC (fa4c8f6fb20e625d6d7e2b148850b17b)
Inside Validate Logo
FAILED: verifyLogo
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.$Proxy12.isDisplayed(Unknown Source)
    at com.mystore.pageobjects.IndexPage1.validateLogo(IndexPage1.java:47)
    at com.mystore.testcases.IndexPageTest1.verifyLogo(IndexPageTest1.java:40)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:567)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
    at org.testng.TestRunner.privateRun(TestRunner.java:766)
    at org.testng.TestRunner.run(TestRunner.java:587)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
    at org.testng.TestNG.runSuites(TestNG.java:1039)
    at org.testng.TestNG.run(TestNG.java:1007)
    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)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
===============================================

Please help, I'm not sure why the webelement is not identified. I tried with other web elements and they also get the same NPE.

HoRn
  • 1,458
  • 5
  • 20
  • 25
Anu
  • 11
  • 2
  • driver is probably null. (LaunchApp() method should be called earlier? In @beforetest?) Track down the initialization and pass the reference into constructors when necessary. Also might want to re-consider declaring the webdriver as static. – pcalkins Oct 26 '22 at 20:34
  • I have the webdriver static in BaseClass and also the LaunchApp() is called in @BeforeMethod in IndexPageTest1 class – Anu Oct 27 '22 at 22:11
  • When i use @FindBy method for identifiying objects in POM and use PageFactory.initElements(driver, this) to initialize the elements, i am getting this error. I am new to frameworks and am trying to build one, and clueless, if there is anything else, that needs to be done for PageFactory(any files needed). PageFactory package is imported – Anu Oct 27 '22 at 22:16
  • I never use POM or TestNG, so I'm not sure I can help much. Maybe see this thread: https://stackoverflow.com/questions/50811759/what-is-the-difference-between-beforetest-and-beforemethod-in-testng Right now your IndexPage1 constructor is calling PageFactory.initElements(driver, this);... but driver has not been initialized or passed in as a reference. Your "launchApp()" method initializes the driver but that's in a @BeforeMethod annotation, which I think gets called before each method? That doesn't seem right. – pcalkins Oct 27 '22 at 22:34

1 Answers1

1

The problem lies in your test code.

  • The WebDriver object that is part of your base class BaseClass1 is getting initialised only when the method launchApp() is invoked.
  • But the constructor of your page class IndexPage1 already tries to access the WebDriver object (which at the time of instantiation of IndexPage1 is null)
  • So you end up invoking PageFactory.initElements(driver, this); with a null value for driver

Solution.

  • Please remove the logic of managing your webdriver instances via your base class and segregate it to a separate utility method which when called can give you a WebDriver object. Alternatively you can also make use of a library such as Autospawn that I created which uses an annotation way of managing webdriver instances. - The documentation should explain how to use this library. Note that if you use this library, you would need to create all your page classes within your @Test method (because that's the only place wherein a valid WebDriver instance would be available in the current Thread's context)
  • Refactor your IndexPage1 class's constructor so that it accepts a WebDriver instance as a parameter from outside.
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66