0

So I'm I have the following in my Global properties file:

browser=Chrome
QAurl = https://rahulshettyacademy.com/seleniumPractise/#/

I then have the properties class in order to be able to read the global property file:

public class TestBase {
    
    public WebDriver driver;
    
    public WebDriver WebDriverManager() throws IOException  
    {
    FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"//src//test//Resources//global.properties");
    Properties prop = new Properties();
    prop.load(fis);
    String url = prop.getProperty("QAUrl"); //The url we will pass here
    if(driver == null)
    {
        if(prop.getProperty("browser").equalsIgnoreCase("Chrome"))
        {
    driver.get(url);
    driver = new ChromeDriver();
    driver.manage().window().maximize();
        }
        if(prop.getProperty("browser").equalsIgnoreCase("firefox"))
        {
            //firefox code
        }
    }
    
    return driver;
    

    }
}

I am getting the following error in my console:

FAILED: runScenario("Search Experience for product search in both home and Offers page", "Search and Place the order for Products")
        Runs Cucumber Scenarios
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver" is null
    at Utils.TestBase.WebDriverManager(TestBase.java:24)
at Utils.TestContextSetup.<init>(TestContextSetup.java:20)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at org.picocontainer.injectors.AbstractInjector.newInstance(AbstractInjector.java:145)
    at org.picocontainer.injectors.ConstructorInjector$1.run(ConstructorInjector.java:342)
    at org.picocontainer.injectors.AbstractInjector$ThreadLocalCyclicDependencyGuard.observe(AbstractInjector.java:270)

Any ideas where I am going wrong?

Immy110
  • 13
  • 5
  • In `if(driver == null) { ... }` the field `driver` is used (`driver.get(url);`) before setting it (`driver = new ChromeDriver();`), so `driver.get(url);` will always cause a `NullPointerException`. Does switching those lines fix your issue? – howlger Aug 10 '23 at 13:15
  • So I tried swapping those lines, i.e. putting 'driver = new ChromeDriver();' followed by 'driver.get(url);' - It throws the following error: FAILED: runScenario("Search Experience for product search in both home and Offers page", "Search and Place the order for Products") Runs Cucumber Scenarios java.lang.NullPointerException: null value in entry: url=null – Immy110 Aug 10 '23 at 14:16

2 Answers2

0

Create a constructor of TestBase class and put your code to load properties inside the constructor, something like below:

public class TestBase {
    
    public WebDriver driver;
    public static Properties prop;

    public TestBase(){
        try {
            prop = new Properties();
            FileInputStream ip = new FileInputStream(System.getProperty("user.dir")+ "//src//test//Resources//global.properties");
            prop.load(ip);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public WebDriver WebDriverManager() throws IOException  {
    
         String url = prop.getProperty("QAUrl"); //The url we will pass here
         // rest of the code
    }
}

Shawn
  • 4,064
  • 2
  • 11
  • 23
0

This error message...

FAILED: runScenario("Search Experience for product search in both home and Offers page", "Search and Place the order for Products")
    Runs Cucumber Scenarios
java.lang.NullPointerException: Cannot invoke "org.openqa.selenium.WebDriver.get(String)" because "this.driver" is null

...implies that the driver instance was null when the following line of code was executed:

driver.get(url);

Details & Solution

You have declared driver as an instance variable:

public WebDriver driver;

Next when you read the browser property, first you need to initialize the ChromeDriver initiated Browsing Context and then invoke the url as follows:

public class TestBase {

    public WebDriver driver;

    public WebDriver WebDriverManager() throws IOException  {

        FileInputStream fis = new FileInputStream(System.getProperty("user.dir")+"//src//test//Resources//global.properties");
        Properties prop = new Properties();
        prop.load(fis);
        String url = prop.getProperty("QAUrl"); //The url we will pass here
        if(driver == null) {
            if(prop.getProperty("browser").equalsIgnoreCase("Chrome")) {
                driver = new ChromeDriver();
                driver.get(url);
                driver.manage().window().maximize();
            }
            if(prop.getProperty("browser").equalsIgnoreCase("firefox")) {
                driver = new FirefoxDriver();
                driver.get(url);
            }
        }
        return driver;
    }

}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352