Wednesday, April 4, 2012

Webdriver - Store data on disk

Webdriver - Store data on disk.

There are situations where the data of the current test need to be used in the next test. In this scenario we can't store the values in variables, they are lost once the test is completed (Java program closed). It is advisable to store the data on disk using Java files.

Following are the two methods that are used to read and write data to the files.
I have used one single string where all the values are separated by ";". I can use split method to split the string into separate values.

public static void xFileWriteData(String FileName, String DataSemicolnSeperated) throws Exception   {
FileOutputStream out; 
PrintStream p; 
try
{
out = new FileOutputStream(FileName);
p = new PrintStream(out);
p.println (DataSemicolnSeperated); //Insert for loop if there is more than one line
p.close();
out.close();
}
catch (Exception e)
{
Print("Error writing to file");
}
}

public static String xFileReadData(String FileName) throws Exception   {
String DataSemicolnSeperated = null;
String Line;
try
{
BufferedReader br = new BufferedReader(new FileReader(FileName));
while ((Line = br.readLine()) != null) { 
DataSemicolnSeperated = Line;

br.close();

catch (Exception e)
{
Print("Error reading the file");
}
return DataSemicolnSeperated;
}


---- 

No comments:

Post a Comment