Home > Enterprise >  chromedriver NullPointerException error on linux
chromedriver NullPointerException error on linux

Time:12-22

I'm currently learning selenium. I have followed a step by step walkthrough on how to start selenium-chromedriver. However I am experiencing this error here and I am have no idea how to solve it.

NullPointerException

The following are the source code the walkthrough has given me.

package driverUtilities;

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

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DriverUtilities {
    private static DriverUtilities instanceOfDriverUtilities;
    private WebDriver driver;

    public static DriverUtilities getInstanceOfDriverUtilities() {
        if (instanceOfDriverUtilities == null) {
            instanceOfDriverUtilities = new DriverUtilities();
        }
        return instanceOfDriverUtilities;
    }

    public WebDriver getDriver() {
        if (driver == null) {
            CreateDriver();
        }
        return driver;
    }

    private String GetDriverName() {
        Properties config = new Properties();
        String driverName = "";
        try {
            config.load(new FileInputStream("config.properties"));
        } catch (FileNotFoundException e) {
            System.out.println("Config file is not present");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Error when loading config file");
            e.printStackTrace();
        }
        for (String key : config.stringPropertyNames()) {
            if (key.equals("browser")) {
                driverName = config.getProperty(key);
            }
        }
        return driverName;
    }

    private void CreateDriver() {
        String driverName = GetDriverName();

        switch (driverName) {
        case "Google Chrome":
            System.setProperty("webdriver.chrome.driver", "chromedriver");
            this.driver = new ChromeDriver();
            break;
        case "Firefox":
            System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
            this.driver = new FirefoxDriver();
            break;
        default:
            break;
        }
    }
}

This is the Driver Utilities class

package test;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import driverUtilities.DriverUtilities;

public class Mod08_Slide_16_Navigation_Commands {
    @Test
    public void navigationCommands() {
        DriverUtilities myDriverUtilities = new DriverUtilities();
        WebDriver driver = myDriverUtilities.getDriver();

        System.out.println("Start the Test Case");

        // Load the website - http://www.bbc.co.uk
        driver.get("http://www.bbc.co.uk");
        System.out.println("\nLoad the website - http://www.bbc.co.uk");

        // Refresh the page
        driver.navigate().refresh();
        System.out.println("\nRefresh the page");

        // Load the website - http://www.google.co.uk
        driver.get("http://www.google.co.uk");
        System.out.println("\nLoad the website - http://www.google.co.uk");

        // Go back to the website - http://www.bbc.co.uk
        driver.navigate().back();
        System.out.println("\nGo back to the website - http://www.bbc.co.uk");

        // Go forward to the website - http://www.google.co.uk
        driver.navigate().forward();
        System.out.println("\nGo forward to the website - http://www.google.co.uk");

        // Close the browser window
        driver.close();
        System.out.println("\nClose the browser window");

        System.out.println("\nEnd of Test Case \"Navigation Commands\"");
    }
}

This is the java test class I am trying to make work of

# Change the browser to use for testing purposes, accepted values are Google Chrome and Firefox
browser=Google Chrome

I also have the pom.xml file and a config.properties file and will share it if its required.

Note: My assumptions is that the setproperty function is not locating the correct path to chromedriver. However, I'm a arch linux user thus I'm not sure if the value inside the setproperty function is set to "chromedriver" or "/usr/bin/chromedriver"

Note 1: I have navigated to the .metadata of the Eclipse IDE and deleted it after reading some stackoverflow posts. However, this did not work*

Note 2: I am completely new to Selenium however, I have some experiences with Java and understand that nullpointerexception occurs when I declare a variable but did not create an object and assign that variable to it.

Edit 1: I have included the config.properties as specified

CodePudding user response:

One suggestion I have is to hard-code the creation of the driver (without config.properties) and once you have the code working, you can work on optimizations such as dynamically creating the web driver based on a key value like you are doing here. To do this, simply replace this line

WebDriver driver = myDriverUtilities.getDriver();

with

System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\WebDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

Obviously, make sure the path to the chromedriver.exe is resolved correctly for your system. By the way, unless your web drivers are in the root folder of your code project, that path is not correct. I will double check that is the case.

I ran your code with this recommendation and it worked. That tells me either your web driver path is not correct, or there is something funky with your config.properties.

Lastly, this loop isn't correct either

    for (String key : config.stringPropertyNames()) {
        if (key.equals("browser")) {
            driverName = config.getProperty(key);
        }
    }

You don't want to loop through all your browser keys. If you have more than one "key", it will iterate through them and return the last one found. Instead, you need to call getProperty() once your properties file is loaded:

String propValue = props.getProperty("browser");

Then, once you have the correct propValue, you should pass it to your switch to properly instantiate the web driver.

switch(propValue) {
    case "chrome":
        this.driver = new ChromeDriver();
        break;
    case "firefox":
        this.driver = new GeckoDriver();
        break;
    default:
        // throw some exception here
        throw new IllegalArgumentException(propValue   " is not a supported browser.");
}

CodePudding user response:

Not an answer but I have instead worked around the issue by importing the java project into windows 10. As commented above, the java class properly works under a windows environment and the issue seems to be revolving around the setpropert() function. The symlink for chromedriver is incorrect while trying to execute it in a linux environment. In the meantime, I am using windows as the main OS to study selenium. I will have a look at why I am getting NullPointerException on linux.

  • Related