Test data is one of the most complicated things to deal with in automated testing.
We generally do automated testing for the following reasons
->Automating repeated manual work (creating some 1000 users)
->Regression testing
You would have seen previous posts in my blog which use excel as a data source for test data.This is fine as long as we do not version control it with Subversion or Git.Excel is a binary file and does not work well with code versioning systems.
Automating manual work does not really need version control and excel works well in that situation but regression testing and maintaining a suite of regression tests will definitely need version control and also the test data which goes hand in hand with the tests.So for this situation using an excel is not really a good solution and will cause unnecessary problems/conflicts in committing/updating them.
We must use some form of text file based approach to this, but fortunately XML is widely used and probably the best way to solve this problem.While many may argue that XML is not really a text file and we need to use XSL transformation to generate plain text version of XML,it really depends upon how we want to use the XML file.Here in our situation we use/treat it as a text file and there should be no problems in using it with version control.
Ok,so we have decided to use XML,but plain XML won't be of much help and we need to have some structure over it.
Following is a sample format/structure we will use and the entire data retrieval is based upon this.
We use an ID to differentiate data and the rest is our choice.Now we need to have a method which is generic in nature and will retrieve data irrespective of the XML we give.XPath is what we use to navigate and retrieve the data from XML.
The following implementation will return a HashMap of the request nodes we send, in the LoginData example we will generally request for the Username and Password which follows the ID we give.Calling the method is explained as below
Sample usage of the retrieval method
HashMap<String, String> result = new HashMap<>();
String XmlPath = "LoginData.xml";
String rootNode = "User";
String id = "DatabaseUser";
result = RetrieveDataFromXML(XmlPath, rootNode, id, "Email","Password");
System.out.println(result.get("Email"));
System.out.println(result.get("Password"));
The retrieval implementation
https://github.com/Madusudanan/Selenium/blob/master/XMLDataManager.java
Sample usage of the retrieval method
HashMap<String, String> result = new HashMap<>();
String XmlPath = "LoginData.xml";
String rootNode = "User";
String id = "DatabaseUser";
result = RetrieveDataFromXML(XmlPath, rootNode, id, "Email","Password");
System.out.println(result.get("Email"));
System.out.println(result.get("Password"));
The retrieval implementation
https://github.com/Madusudanan/Selenium/blob/master/XMLDataManager.java
How we construct the parameters of calling the RetrieveDataFromXML is entirely upto us.It is better to have it verbose by mentioning what the parameters mean.Note that the first three parameters are fixed i.e the XML file path,rootNode,id(identifier to uniquely identify the particular set of data), the rest of the parameters depends upon what data we want to retrieve from that particular set. Email and Password are the counterpart of the data(varargs) we pass in to the retrieval method.We can additional parameters if we want to retrieve additional data and it has to be the same casing as in the xml data source.
This kind of implementation is flexible and we can select what data we want.If you find this tedious and if you want the entire set in a single go.This can be modified in the retrieval method by passing the XPath query as "//User[@id='Adminuser']" which will return the whole data upto the enclosing nodes.