Home Guide Understanding setProperty in Selenium

Understanding setProperty in Selenium

By Jash Unadkat, Technical Content Writer at BrowserStack -

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

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

What is setProperty in Selenium?

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

The 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.

Example :

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

What is the use of setProperty method?

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 are to be executed.

Let’s analyze this with a simple example:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Sample
{
public static void main(String[] args) {

// Configuring the system properties of chrome driver
System.setProperty("webdriver.chrome.driver", "C:Selenium-java-chromedriver_win32chromedriver.exe");

//Initializing the browser driver
WebDriver driver = new ChromeDriver();

//Navigating through a particular website
driver.get("https://www.browserstack.com");

System.out.println("Selenium Webdriver Script in Chrome");
driver.close();

}
}

When the script above is executed, it will open the BrowserStack website on a Chrome browser using ChromeDriver. This is achieved because System.setProperty manages the initialization of the Chrome driver in the first step.

Try Selenium Testing For Free

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.


Read article on How to run Selenium tests on Chrome using ChromeDriver: Tutorial


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

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free