Dynamically Find Control Type Used in a Web Application using WaTin


While performing an automation so many times we have faced issues finding the control type on the fly and performing some basic operation or getting the properties of controls.
 
Let me give an example to explain this with a clear picture.
 
Suppose that a developer has placed a table on a webpage. This table contains many controls like textbox, button, picture box, label, dropdown, list etc. etc. all of these controls appear in table cells dynamically, it means we are not sure when and at what position of the cells it will appear in the table.
 
Now we have to read this control on the fly and perform some basic operations.

So being an automation engineer, we have programmed our automation code in such a way to know about this control and where it has appeared then perform an operation or read properties of that control.
 
It's a bit tricky to handle this kind of situation.
 
To solve this kind of issue WaTin has provided one of the best features to get the type of controls and then based on the control we can call our custom method to either get the properties or perform a common action.
 
Here I will show to how to read control types on the FLY using WaTin.
 
Below is the Image of the web page (to automate this scenario I have developed one basic web application)

wantin1.jpg
And here is the piece of the code to do the same.
 
 
try
 

 
    IE ie = IE.AttachTo<IE>(Find.ByUrl("http://localhost:28348/Home.aspx"));
 
    var tablecells = ie.Table("table").TableCells;
 
    var type = new string[tablecells.Count];
 
    int i = 0;
 
    foreach (TableCell tableCell in tablecells)
     {
 
        try
 
        {
 
            string watinType = tableCell.Elements[0].GetType().FullName;
 
            if (watinType != null) type[i] = watinType.Split('.')[2];
         }
 
        catch (Exception)
         {
 
            throw;
         } 
         i++;
     }
 }
 
catch (Exception)
 {
 
    throw;
 }
 

I had provided a screen shot of the code as shown below.
wantin2.jpg
 
And below is the Result when you run the above piece of code. You can see all the controls type in the Type array of string. Shown in below drop down.
 
wantin3.jpg

Feel free to provide your valuable comments and suggestion.
 
Thanks