Tuesday 25 February 2014

Selenium Data Driven | Text File

The Text file lets you store the keywords for data driven tests. Here, the Text file acts as a data-source.

1| Open Notepad
2| Enter keywords as shown in the figure.
3| Make sure that both the keywords are separated with a space.
4| Save them as .txt [data.txt]




package packagename;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class classname {
WebDriver driver;
private String baseUrl;

@BeforeClass
public void BeforeClass() {

driver = new FirefoxDriver();
driver.manage().window().maximize();
baseUrl = "http://www.google.co.in";
}

@Test
public void Test01() {
Execute();
}

public void Execute() {

BufferedReader in = null;
InputStreamReader inputStream = null;
try {

inputStream = new InputStreamReader(new FileInputStream("C:"
+ File.separator + "data.txt"));
in = new BufferedReader(inputStream);
String line = null;
String actualvalue = "";
String expectedvalue = "";
while ((line = in.readLine()) != null) {
String[] data = line.split(" ");
if (data.length >= 1) {
actualvalue = data[0];
expectedvalue = data[1];
System.out.println("Actual : " + actualvalue);
System.out.println("Expected : " + expectedvalue);
// for (int i = 0; i < 10; i++) { // same action multiple
// times
// Your CODE here
driver.get(baseUrl + "/");
driver.findElement(By.id("gbqfq")).sendKeys(actualvalue);
driver.findElement(By.id("gbqfq")).sendKeys(Keys.RETURN);
boolean b = driver.getPageSource().contains(expectedvalue);
Assert.assertTrue(b);
// }
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

@AfterClass
public void AfterClass() {
driver.quit();
}
}

No comments:

Post a Comment