I got an interesting request from a customer today. It seems the app that the user intends to test has a user.config file associated with it. And here's what the user is looking to do in his own words:
How do I create a test which modifies the user.config file before opening the application?
Hopefully it will be interesting for you to see how I put this together. I did a bit of research and it went like this:
1) What's a user.config file?
I figured I probably have one of these on my hard-drive. I did a Windows search and I located a single user.config file - it was a XML file. I copied it into c:\test\user.config. This was to be my guinea pig - so far so good.
2) We need an XML API
So user.config is a XML file. It's still possible to edit it as a regular txt file (using the System.IO API) but that seems a bit amateurish. I wanted to use a XML API. System.XML which is a .Net assembly was my tool of choice
3) Putting together the code
I spent a bit of time researching this. Mostly I looked at MSDN. Here are a few of the articles I checked out:
Anyway, here's the code that will take the user.config file from c:\test\user.config and add a comment node to it:
XmlDocument config = new System.Xml.XmlDocument(); \\Create new document
config.Load("C:\\test\\user.config"); \\Load the existing content
XmlNode newElem = config.CreateNode(XmlNodeType.Comment,config.Name,config.NamespaceURI); \\Create a new node to add to the document
newElem.InnerText = "this is my node"; \\Add the text content of the new node
config.AppendChild(newElem); \\Put newly created node into the document
config.Save(XmlWriter.Create("C:\\Intel\\user.config")); \\Overwrite the existing xml file with the new document (which is the old document plus a node)
It's C# code but you can convert it to VB if you need to by using our coded convertor. I put this code in a coded step.
4) Adding the necessary references
In order to make this code works you'll need to add an Assembly reference to System.XML. On my machine I located the file in the following location:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.XML.dll
5) Need this to execute before/after your test?
No problem! You can make that work as seen here.