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.
Use below code to perform data driven:
Detailed explanation for the above program is as follows:
Following code is the required packages for JAVA utilities to make integration with properties file.
Following code is the required packages for Selenium Webdriver and its methods.
Following code is the required packages for Firefox browser.
Above both required packages are available in downloaded selenium server jar file.
Following code is to bundled the saved properties file.
Following code is to get search string from the properties file.
Following code is to define keyword token of the properties file.
Following code is to initialize the Firefox driver.
Following code is to open the Goolge in browser.
You can also use the following code is to open the hello selenium blog in browser.
Following code is to initialize the loop while defined token is available in the file.
Below code is capturing the next token value of keyword.
Following code is clear the Google search text box previous value.
Following code is to input the keyword into Google search text box.
Following code is to pause the thread for defined time. This code need exception handling to make code run smoothly.
Following code is to quit the Firefox driver instance.
You can also use the following code is to close the Firefox driver instance.
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.*;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
Following code is to bundled the saved properties file.
ResourceBundle testdata = ResourceBundle.getBundle("testdata");
String search = testdata.getString("Search");
StringTokenizer keyword = new StringTokenizer (search,",");
Following code is to initialize the Firefox driver.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.navigate().to("http://www.google.com");
while(keyword.hasMoreTokens()){
String value = keyword.nextToken();
driver.findElement(By.name("q")).clear();
driver.findElement(By.name("q")).sendKeys(value);
Thread.sleep(1000);
driver.quit();
driver.close();
0 Comments
What would you like to add in my list? I look forward to reading your comments below.