Header Ads

HOW TO PERFORM DATA DRIVEN USING PROPERTIES FILE IN SELENIUM WEBDRIVER ?

We can perform data driven in selenium webdriver using properties file.
Here is the very basic program to explain How to perform data driven using properties file in selenium webdriver.




Create a testdata.properties file in the project source folder with following details:

Search=Hello Selenium,abhishek yadav qa

Use below code to perform data driven:

Java Source code:

package com.helloselenium.selenium.test;

import java.util.*;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DataDrivenUsingPropertiesFile{

 public static void main(String[] args)

 {
  ResourceBundle testdata = ResourceBundle.getBundle("testdata");
  String search = testdata.getString("Search");
  StringTokenizer keyword = new StringTokenizer (search,",");

  WebDriver driver = new FirefoxDriver();

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

  while(keyword.hasMoreTokens()){
   String value = keyword.nextToken();
   driver.findElement(By.name("q")).clear();
   driver.findElement(By.name("q")).sendKeys(value);
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }

  driver.quit();
 }
}

Detailed explanation for the above program is as follows:




Following code is the required packages for JAVA utilities to make integration with properties file.
import java.util.*;
Following code is the required packages for Selenium Webdriver and its methods.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
Following code is the required packages for Firefox browser.
import org.openqa.selenium.firefox.FirefoxDriver;
Above both required packages are available in downloaded selenium server jar file.
Following code is to bundled the saved properties file.
ResourceBundle testdata = ResourceBundle.getBundle("testdata");
Following code is to get search string from the properties file.
String search = testdata.getString("Search");
Following code is to define keyword token of the properties file.
StringTokenizer keyword = new StringTokenizer (search,",");




Following code is to initialize the Firefox driver.
WebDriver driver = new FirefoxDriver();
Following code is to open the Goolge in browser.
driver.get("http://www.google.com");
You can also use the following code is to open the hello selenium blog in browser.
driver.navigate().to("http://www.google.com");
Following code is to initialize the loop while defined token is available in the file.
while(keyword.hasMoreTokens()){
Below code is capturing the next token value of keyword.
String value = keyword.nextToken();
Following code is clear the Google search text box previous value.
driver.findElement(By.name("q")).clear();
Following code is to input the keyword into Google search text box.
driver.findElement(By.name("q")).sendKeys(value);
Following code is to pause the thread for defined time. This code need exception handling to make code run smoothly.
Thread.sleep(1000);
Following code is to quit the Firefox driver instance.
driver.quit();
You can also use the following code is to close the Firefox driver instance.
driver.close();



No comments


What would you like to add in my list? I look forward to reading your comments below.



Powered by Blogger.