Friday, 12 October 2012

How to get input data in your test program using Hashmap


In my last post,i described about,what are the technology we can use for a good and simple framework development.
Now you want to know about how to import input data in your test program,
So below are the code for same.
Note: you can arrange package structure according your requirement.

package com.companyName;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import com.companyName.*;


public class Test {
String mystring []= new String [10000];
Map mMap = new HashMap();
//if you are using testng then use @BeforeSuite(alwaysRun=true) method and change below constructor to a simple java method.
Test()
{


String sPath=null;
String filename="inputfilename";//Tested with .csv file
int linenumber=0;
String[] temp;
StringTokenizer st = null;
try {
sPath = new java.io.File(".").getCanonicalPath();
System.out.println("Path: "+ sPath);

FileInputStream fileSt = new FileInputStream(sPath+"\\"+filename);
DataInputStream in = new DataInputStream(fileSt);

BufferedReader bufRdr = new BufferedReader(new InputStreamReader(in));
String line = "";
System.out.println("Path: "+ sPath+"\\"+filename);
while((line = bufRdr.readLine()) != null)
{

mystring[linenumber]=line;
TestDataObj TestDataObject = new TestDataObj() ;
System.out.println("Path: "+ sPath+"\\"+filename+TestDataObject);
temp=line.split(",");

System.out.println("Values-->"+temp[0]+"--"+temp[1]+"--"+temp[2]);
TestDataObject.setTestcaseID(temp[0]);
TestDataObject.setUserName(temp[1]);
TestDataObject.setPassword(temp[2]);

mMap.put(temp[0], TestDataObject);
//because temp[0] is a testcase id in my input file so i am using key value in hashmap.
linenumber++;

}
in.close();
}
catch(Exception e)
{

}

}

}

for setter and getter

package com.companyName;

public class TestDataObj {
private String sUserName=null;
private String sPassword=null;
private String sTestcaseID=null;
public String getUserName() {
return sUserName;
}
public void setUserName(String userName) {
this.sUserName = userName;
}
public String getPassword() {
return sUserName;
}
public void setPassword(String password) {
this.sPassword = password;
}

public String getTestcaseID() {
return sTestcaseID;
}
public void setTestcaseID(String testcaseid) {
this.sTestcaseID = testcaseid;
}

}
Test program

package com.companyName;

public class TestData {

public static void main(String args[])
{
Test test= new Test();
TestDataObj testobj=(TestDataObj)test.mMap.get("tc1");
System.out.println(testobj.getTestcaseID());
System.out.println(testobj.getUserName());
System.out.println(testobj.getPassword());
}

}

let me know if have any doubts.

1 comment:

  1. i don't think this is a good solution for the data driver testing .sometimes we often use the excel file to store our data ,that's simple and everyone can know it very well .so that's the disadvantage for this solution .

    ReplyDelete