App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide Understanding System setProperty in Selenium

Understanding System setProperty in Selenium

By Jash Unadkat & Sonal Dwivedi, Community Contributors -

The first step in automating tests for a website or web app is deciding which browser to test. As a result, defining the system properties and initializing the browser object becomes necessary, especially before writing Selenium test scripts. The setProperty() method helps QAs do precisely that.

This article explains the setProperty method in Selenium with a sample program.

What is System class in Java?

  • System is a Java class that belongs to the java.lang package.
  • System is a final class and does not provide any public constructors; hence, it cannot be instantiated.
  • This means it cannot be inherited to override its methods.
  • As a System class restricts to inherit and override, it provides various pre-built class fields and methods facilitating standard input, standard output, access to externally defined properties and environment variables, a means of loading files and libraries, and a utility method for quickly copying a portion of an array.

What is setProperty in Selenium?

As the name suggests, the setProperty method enables QAs to set the properties for the desired browser for test automation.

The system setProperty method has two attributes – “propertyName” and “value.” The propertyName represents the name of the browser-specific driver, and the value points to the path of that browser driver.

The java.lang.System.setProperty() method sets the system property indicated by the specified key.

Syntax:

public static String setProperty(String key, String value)

Parameters:

Key:   This is the name of the system property.

Value: This is the value of the system property.

setProperty() method helps to set the system property key to have the value as value.

Returns:

This method would return the previous value of the system property in the form of String, or null if it did not have one.

Throws:

  • SecurityException − If a security manager exists and its checkPermission method doesn’t allow setting the specified property.
  • NullPointerException − If the key or value is null.
  • IllegalArgumentException − If the key is empty.

In Selenium, setProperty() can be used to set the driver path of the respective browser.

What is the use of the setProperty method? (with Examples)

To automate tests for a specific browser, QAs need to download the browser-specific drivers first as browsers do not have built-in servers for test automation. These drivers act as a bridge between test scripts and browsers for test automation.

One can check the complete list of available browser drivers on the official site of Selenium. Once the driver is downloaded for a specific browser, QAs need the setProperty() method to define the path for that driver before writing any test cases.
This helps the Selenium WebDriver identify the browser on which tests will be executed.

Let’s analyze this with examples:

System.setProperty("webdriver.chrome.driver", "D:\\Browser Binaries\\chromedriver_win32\\chromedriver.exe");

Here, setProperty() has two parameters:

Key:  “webdriver.chrome.driver” 

Value: “D:\\Browser Binaries\\chromedriver_win32\\chromedriver.exe” (Path of the executable)

Here, the key implies the Selenium WebDriver uses Google Chrome driver to execute on the Chrome browser.

Value implies the location from where the Chrome executable should be called.

If you wish to run on Firefox browser, download the binary and set the values accordingly. The sample is as below:

Key:  “webdriver.gecko.driver” 

Value: <Firefox binary file location>

Note: Binary file location value should be updated per your system path.

We need to initialize the browser as the system properties are set now.

For Chrome browser:

WebDriver driver = new ChromeDriver();

For Firefox browser:

WebDriver driver = new FirefoxDriver();

Example 1: Launching BrowserStack website using Selenium using setProperty method

public class SetPropertyExample {

    public static void main(String args[]) {

        System.setProperty("webdriver.chrome.driver", "D:\\Browser Binaries\\chromedriver_win32\\chromedriver.exe");

        // Initializing the browser driver

        WebDriver driver = new ChromeDriver();

        // Navigating to BrowserStack website

        driver.get("https://www.browserstack.com");

        System.out.println("BrowserStack is launched successfully on Chrome!");

        driver.close();

    }

}

Example 2: Passing Key as Null

public class NullPointerExceptionExample {

    public static void main(String args[]) {

        System.setProperty(null, "D:\\Browser Binaries\\chromedriver_win32\\chromedriver.exe");     

        

        //Initializing the browser driver

        WebDriver driver = new ChromeDriver();

        //Navigating to BrowserStack website

        driver.get("https://www.browserstack.com");

        System.out.println("BrowserStack is launched successfully on Chrome!");

        driver.close();     

    }

}

system setproperty

Example 3: Passing Key as Empty

public class SetPropertyExample {




    public static void main(String args[]) {

        System.setProperty("", "D:\\Browser Binaries\\chromedriver_win32\\chromedriver.exe");




        // Initializing the browser driver

        WebDriver driver = new ChromeDriver();




        // Navigating to BrowserStack website

        driver.get("https://www.browserstack.com");

        System.out.println("BrowserStack is launched successfully on Chrome!");

        driver.close();

    }

}

system setproperty

The System.setProperty() method forms the basis for test case automation on any browser. Naturally, QAs must understand how to use this fundamental method for all automation purposes in Selenium.

QAs can use BrowserStack’s cloud Selenium grid to execute their Selenium test cases for multiple browsers on various devices. BrowserStack also offers integrations with popular frameworks and CI/CD tools like Jenkins, CircleCI, and TeamCity to make the test process faster and more convenient using plugins.

Try Selenium Testing

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

findElement and findElements in Selenium

How to find broken links in Selenium

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.