I came across an issue in one of the UI Automation project where the Automation developer has to use some elements which were references to UI elements from an XML. Let’s see how the XML was looking like:
Now this XML has tag <element> which has to be used in helper methods to find elements. It was something like the following:
The problem was that these type of lines were scattered across the project. I found this issue during the code review step and decided to find an approach how to get rid of such problem and then an idea came in my mind.
T4 Templates to rescue
I had this notion of T4 templates before. I saw and did some fixes in EF generated TT templates but I have never created a template from scratch. To create a template from scratch its always handy to use tools like T4 editor and few more custom utilities which makes life easy.
I knew about Tangible T4 template extension of Visual studio 2013. This extension has a free version which gives you features more than what you wanted, but of course there are many benefits to have pro-licensed as well, but this was enough for me so what I got with Tangible T4 template.
The Editor for syntax highlighting:
Figure 1
This was the only feature I was looking for. But there was more, a few VS File templates were also installed and couple of them were having sample code as well to get started which was great.
I used the Advanced T4 template to add new Template file which has generated a sample template like shown in Figure 1.
Now, I had to change it to give a custom implementation so that I can get desired classes. Because the Editor gave a lot of help and everything was visible so it was easy to differentiate the local code and template code. So all I had to do was read my XML file, iterate through it and generate desired result as classes and constant fields.
The following is the complete code that I used:
- <#@ template debug="true" hostSpecific="true" #>
- <#@ output extension=".cs" #>
- <#@ Assembly Name="System.Core.dll" #>
- <#@ Assembly Name="System.Xml.dll" #>
- <#@ Assembly Name="System.Xml.Linq.dll" #>
- <#@ Assembly Name="System.Windows.Forms.dll" #>
- <#@ import namespace="System" #>
- <#@ import namespace="System.IO" #>
- <#@ import namespace="System.Diagnostics" #>
- <#@ import namespace="System.Linq" #>
- <#@ import namespace="System.Xml.Linq" #>
- <#@ import namespace="System.Collections" #>
- <#@ import namespace="System.Collections.Generic" #>
- <#
-
- varstr_fileName = "GlobalActionsElements.xml";
- varprojDir = System.IO.Path.GetDirectoryName(this.Host.TemplateFile);
-
-
- string str_XMLFileNamePath = Path.Combine(projDir, str_fileName);
-
-
-
-
-
-
- #>
-
-
- namespace WebTest{
-
-
- public static class <#= Path.GetFileNameWithoutExtension(str_XMLFileNamePath).Replace(".","") #>
- {
- <# GenerateConstants(str_XMLFileNamePath); #>
- }
- }
-
- <#+
-
- void GenerateConstants(string path)
- {
- System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Load(path);
-
-
- var classes = from c in doc.Descendants("uielements")
- where c.NodeType != System.Xml.XmlNodeType.Comment
- select c.Descendants();
-
-
- foreach (var item in classes.ToList())
- {
- IEnumerable<string> constants = item.Descendants("element").Select(x =>x.Attribute("Name").Value);
- if(constants.Count() == 0)
- {
- constants = item.Select(x =>x.Attribute("Name").Value);
- }
- foreach (var node in constants)
- {
- #>
- public const string <#= node.ToUpper() #> = "<#= node #>";
- <#+
- }
- }
- }
- #>
I have attached the screenshot and highlighted the code which would generate. It’s really as simple as it seems. You can debug the code easily by placing a Debug breakpoint any where in the code and right click on the Template and select “
Debug Template”.
With Tangible T4 template it allows to compile and run the T4 when you press Ctrl + S key (Save document). That’s it.
Here are the results that I generated using it:
What we have achieved using it? Well see for yourself:
For now everybody was happy. But this is not all, Next I’m going to use T4 to generate Enums from values in Domain tables from database. Hope this would help someone looking for something like this.