10i. Advanced WebDriver – Using property files

Hiya champions! Welcome back to another fun filled article which tells you why property files exist in the first place!! This particular concept is not just limited to Selenium WebDriver projects. It can be used in basically any Java project that involves hardcoded stuff.

Imagine you have more than a couple hundred automated test cases and in each test case, you hard-coded the URL of the application under test. So far, so good. But, what if another release pops up changing the application URL to a different version and the same test cases are to be executed again (regression testing)? You might think, “That’s easy! I just have to run the build file one more time. Just one click and I will be back playing Super Mario!”. Did you miss the URL part? How much more pain it is, to manually go to every single test case and edit the hard-coded URL just to make it work? Oh no!!!

Don’t fret! We have a solution. Property files come to our rescue! To make your test cases all the more dynamic, make sure you do not put any hard-coded values into them. Abstract out such values to a property file so that whenever they change, you can edit them in just one place and the test cases work perfectly all over again.

Without further ado, let us understand how to implement this in our project, in just three small steps,

Step 1:

Right-click on the project -> New -> Package. Make sure the “Source Folder” displays the name of your project and let the package name be “resources”.

Now right-click on the “resources” package -> New -> File. Let the File Name be “config.properties”.

Folder Structure

Step 2:

Now it is time to abstract out all those hardcoded values from the test cases.

In “config.properties” file, have all the required properties as key-value pairs. This will help us to refer each property in the test case with its key, which will be demonstrated in a minute. Any changes to the values can be made in this one particular file and they will magically get reflected in all the test cases where the reference is made.


property file

Step 3:

In order to use these properties in the test case,

Properties props = new Properties();

– Declare a “props” variable of type “Properties”. This creates an empty property list with no default values.

This will require an import from java.util package,  import java.util.Properties;

FileInputStream fis = new FileInputStream("resources//config.properties");  –

A connection is created to read all the properties from “config.properties” file under “resources” package.

This also requires an import from the java.io package,  import java.io.FileInputStream;

props.load(fis);  – Reads all the properties from the input byte stream with the opened connection, “fis”.

props.getProperty("baseURL");  –  To get the value of a particular property, use “getProperty” method. The corresponding key is to be mentioned within the double-quotes as an argument to the getProperty() method. It searches for the property with the specified key in the property list. If the key is not found, null is returned.

Overall picture

Let us see a test case implementing the concept covered so far,


  1. Open Firefox browser.
  2. Read the firefox driver path and base URL from the property file.
  3. Navigate to the demo site
  4. Locate ‘Tricycle’ checkbox by name and print the corresponding message to the console
  5. Check if the ‘Tricycle’ checkbox is enabled and print corresponding message to console
  6. Based on the current selection status of ‘Van’ and ‘SUV’ checkboxes, check or uncheck and print the status before and after performing click() action
  7. Locate ‘Sedan’ checkbox using XPath
  8. Toggle between select and unselect states using two iterations
  9. Locate ‘Magazines’ radio button using cssSelector
  10. Check if it is selected by default
  11. If yes, then print the corresponding message to console and if no, select the radio button

Verify Eclipse IDE console output screen and JUnit pane for success result

JUnit code for this scenario is,


#Properties as key-value pairs
[email protected]


package com.blog.junitTests;

import java.io.FileInputStream;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class RadioBtns_Checkboxes {
  // Declaring variables
  private WebDriver driver;
  private String baseUrl;
  Properties props; 

  public void setUp() throws Exception {
    // Creates an empty property list
    props = new Properties();
    // A connection is created to config.properties file
    FileInputStream fis = new FileInputStream("resources//config.properties");
    // Reads the properties from the input byte stream
    // Get the firefox driver path from property file
    String firefoxPath = props.getProperty("firefoxPath");
    // Assign the URL to be invoked to a String variable
    baseUrl = props.getProperty("baseUrl");

    // Mention the property where required
    System.setProperty("webdriver.gecko.driver", firefoxPath);
    // Create a new instance for the class FirefoxDriver
    // that implements WebDriver interface
    driver = new FirefoxDriver();
    // Implicit wait for 5 seconds
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

  public void testPageTitle() throws Exception {
    // Open baseUrl in Firefox browser window

    // Locate 'Tricycle' checkbox using name
    WebElement tricycleCheckbox = driver.findElement(By.name("vehicle2"));
    // Check if tricyle is displayed
    System.out.println("Is tricycle displayed? "+ tricycleCheckbox.isDisplayed());
    // Check if tricyle is enabled to select
    if (tricycleCheckbox.isEnabled()) {
      // Click if enabled
    } else {
      // Print message to console if disabled
      System.out.println("Unable to select 'Tricycle' checkbox as it is disabled.");
    //Get all checkbox elements in a list
    List<WebElement> list = driver.findElements(By

    // Loops through all checkboxe elements
    for (int i = 0; i < list.size(); i++) {
      // Checking if the checkbox is a 'Van' or 'SUV'
      if ((list.get(i).getAttribute("value").trim()
          || (list.get(i).getAttribute("value").trim()
              .equalsIgnoreCase("suv"))) {
        // Print selection status to console
        System.out.println("BEFORE: Is "
            + list.get(i).getAttribute("value") + " selected? "
            + list.get(i).isSelected());
        // Check if the checkbox is selected
        if (!(list.get(i).isSelected())) {
          // Click the checkbox
          System.out.println("AFTER: Is "
              + list.get(i).getAttribute("value") + " selected? "
              + list.get(i).isSelected());
        } else {
          // Uncheck the checkbox
          System.out.println("AFTER: Is "
              + list.get(i).getAttribute("value") + " selected? "
              + list.get(i).isSelected());
    // Locate 'Sedan' checkbox using xPath
    WebElement sedanCheckbox = driver.findElement(By
    System.out.println("Trying to select and de-select Sedan checkbox...");
    for (int i = 0; i < 2; i++) {
      // Click the checkbox
      // Print current status to console
      System.out.println("Selection status of 'Sedan' checkbox: "
          + sedanCheckbox.isSelected());
    // Locate 'Magazines' radio button using cssSelector
    WebElement magazinesRadioBtn = driver.findElement(By
    // Check if radio button is selected by default
    if (magazinesRadioBtn.isSelected()) {
      // Print message to console
      System.out.println("Magazines radio button is selected by default");
    } else {
      // Click the radio button
  } //End of @Test

  public void tearDown() throws Exception {
    // Close the Firefox browser


Clear comments are provided for each line of code, making it self-explanatory. Upon executing the test case, eclipse IDE console window output is as follows,

property file eclipse output

Property files make an automated tester’s life, a dream come true. Thus proved! Time has come to experiment with today’s concept. Be sure to put on your safety helmets so that you don’t bump into any exceptions! And one more thing before I go, all code files are available in the GitHub repo. Check them out!

See you soon! Have a great day!

Series Navigation<< 10h. Advanced WebDriver – Sending email with attachments10j. Advanced WebDriver – Reading data from excel using POI >>

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.