Properties file Data-Driven framework

The properties file is similar to the text file but with the .properties file extension. It is widely used in Java apps to store and configure parameters.

Data-Driven testing using the properties file as the data source is feasible on Selenium WebDriver to handle small data. Create a .properties file and store the keywords with a constant key (search). In Eclipse, open the project, right-click on the src folder and select New| Other... | General | File. Name the file with the.properties extension, for example, config.properties.

Let's see an example of how to read values from the properties file:

Properties file Data-Driven framework

Build a test class that depicts a simple Google search by reading a series of keywords from the .properties file:

import java.util.ResourceBundle;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;

public class classname {
  private WebDriver driver;
  private String baseUrl;

  @BeforeTest
  public void setUp() throws Exception {
    driver = new ChromeDriver();
    baseUrl = "https://www.google.co.in";
  }

  @Test
  public void Test01() throws Exception {

    ResourceBundle bundle = ResourceBundle.getBundle("config");
    String Channel = bundle.getString("search");
    StringTokenizer st = new StringTokenizer(Channel, ", ");
    while (st.hasMoreTokens()) {
      String value = st.nextToken();
      driver.get(baseUrl + "/");
      driver.findElement(By.name("q")).click();
      driver.findElement(By.name("q")).sendKeys(value);
      driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
      Thread.sleep(2000);
    }
  }
}

Let's see an alternative method to fetch values from the .properties file. More than a data source, the properties file acts as a repository by storing web elements for data reusability. The following is the syntax for the properties file:

prop.getProperty("path")

The following screenshot is a properties data source of the next example. Here, q is a path as mentioned in the preceding syntax and search is a constant key to define the search keyword.

Properties file Data-Driven framework

The following example contains a simple Google search functionality driven with the Java utility library on a properties file:

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

public class className {
  private WebDriver driver;
  private String baseUrl;

  @BeforeTest
  public void setUp() throws Exception {
    driver = new ChromeDriver();
    baseUrl = "https://www.google.co.in";
  }

  @Test
  public void Test01() throws Exception {
    driver.get(baseUrl + "/");

      FileInputStream fs = new FileInputStream("/Users/.../config.properties");
      Properties prop = new Properties();
      prop.load(fs);

      String value = prop.getProperty("search");
      System.out.println(prop.getProperty("search"));
      driver.findElement(By.name(prop.getProperty("path"))).click();
      driver.findElement(By.name(prop.getProperty("path"))).sendKeys(value);
      driver.findElement(By.name(prop.getProperty("path"))).sendKeys(Keys.ENTER);
      Thread.sleep(2000);
    }
}

Data-Driven testing using TestNG with @dataProvider annotation – properties file

In this method, we use the @dataProvider annotation of the TestNG framework to fetch multiple sets of data from a properties file and pass arguments to a test method.

Create a .properties data source file similar to the following screenshot, where the data is separated with a comma and a space (,). The two constant keys, search1 and search2, are used to define the data available in a properties file.

Data-Driven testing using TestNG with @dataProvider annotation – properties file

The following example is a simple Google search that reads a set of keywords separated by a comma and a space using PropertiesDriver that is reusable. Create a test class (GoogleTest.java), as follows:

import java.util.HashMap;

public class GoogleTest{
  private WebDriver driver;

  @DataProvider(name = "keywords")
  public Object[][] data() throws Exception {
  HashMap<String, String[]> dataSet = new PropertiesDriver().getData();

    String search1Strings[] = dataSet.get("search1");
    String search2Strings[] = dataSet.get("search2");
    int size = search1Strings.length;

    // modify 2 upon the no. of rows; Here, two rows, 'search1'& 'search2' are used
    Object[][] creds = new Object[size][2];
    for (int i = 0; i < size; i++) {
      creds[i][0] = search1Strings[i];
      creds[i][1] = search2Strings[i];
    }
    return creds;
  }

  @BeforeTest
  public void setUp() throws Exception {
    driver = new ChromeDriver();
  }

  @Test(dataProvider = "keywords", description = "Google_Test")
  public void search(String search1, String search2) throws Exception {

    driver.get("http://www.google.co.in");

    // search google via keyword 1
    driver.findElement(By.name("q")).clear();
    driver.findElement(By.name("q")).sendKeys("" + search1);
    driver.findElement(By.name("q")).submit();
    Thread.sleep(2000);

    // search google via keyword 2
    driver.findElement(By.name("q")).clear();
    driver.findElement(By.name("q")).sendKeys("" + search2);
    driver.findElement(By.name("q")).submit();
    Thread.sleep(2000);
  }
}

The following is a reusable library class file (PropertiesDriver.java) to fetch data from a properties file:

import java.util.Enumeration;
import java.util.HashMap;
import java.util.ResourceBundle;

public class PropertiesDriver {
  public PropertiesDriver() {
  }
  public HashMap<String, String[]> getData() {
  HashMap<String, String[]> configMap = new HashMap<String, String[]>();
  try {
    ResourceBundle bundle = ResourceBundle.getBundle("config");
      Enumeration<String> keys = bundle.getKeys();
      while (keys.hasMoreElements()) {
        String aKey = keys.nextElement();
        String aValue = bundle.getString(aKey);
        configMap.put(aKey, aValue.split(","));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return configMap;
  }
}

CSV file Data-Driven framework

The term CSV refers to Comma-Separated Values. In CSV, plain-text values are automatically accessed as tabular data. The following screenshot depicts the CSV data source. These values are actually stored using a notepad with separator (comma) between any two keywords.

CSV file Data-Driven framework

The FileReader class is a Java library class used to read data from a CSV file. Refer to the following code snippet that explains how to read data from a CSV-formatted file.

String path = "/Users/.../data.csv";
File file = new File(path);
BufferedReader IN = new BufferedReader(new FileReader(file));
String line = null;
while ((line = IN.readLine()) != null) {
  String[] data = line.split(",");
  driver.findElement(By.locatorType("path")).sendKeys(data[0]);
  driver.findElement(By.locatorType("path")).sendKeys(data[1]);
}

The FileWriter class is a Java library class to write data into a CSV file. This library class helps you to create a CSV file and store output data as a fresh copy. The following code snippet explains how to write data into a CSV file:

FileWriter writer = new FileWriter("/Users/.../output.csv");
writer.append("your_text or runtime_value");

Let's see an example that involves both reading and writing CSV files. The following example depicts a simple Google search by reading a set of keywords and writing the attained page title into a CSV file.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class classname {
  private WebDriver driver;
  private String baseUrl;

  String path = "/Users/.../data.csv";

  @Before
  public void setUp() throws Exception {
    driver = new ChromeDriver();
    baseUrl = "https://www.google.com";
  }

  @Test
  public void Test01() throws Exception {
    driver.get(baseUrl + "/");

    FileWriter writer = new FileWriter("/Users/.../output.csv");
    writer.append("ColumnHeader1");
    writer.append(','),
    writer.append("ColumnHeader2");
    writer.append('
'),

    File file = new File(path);
    BufferedReader IN = new BufferedReader(new FileReader(file));
    String line = null;
    while ((line = IN.readLine()) != null) {
      String[] data = line.split(",");

      driver.findElement(By.name("q")).sendKeys(data[0]);
      driver.findElement(By.name("q")).submit();
      Thread.sleep(2000);
      String element1 = driver.getTitle();
      driver.findElement(By.name("q")).clear();

      driver.findElement(By.name("q")).sendKeys(data[1]);
      driver.findElement(By.name("q")).submit();
      Thread.sleep(2000);
      String element2 = driver.getTitle();

      writer.append(element1);
      writer.append(','),
      writer.append(element2);
      writer.append('
'),
      writer.flush();
    }
    try {
      IN.close();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.87.161