Site Loader
Auckland, New Zealand
In the last post we discussed creating custom methods for Html controls in C# for Coded UI testing, but we saw, there were some moving parts like
  • Parent Control
  • Control Type
  • Property Type
  • Property value
But, we can make our controls even more generic using Generic in C# HtmlEdit/HtmlTextArea Control Generic control method Here is the code
public void EnterText(PropertyType type, string propertyvalue, string text) where T : HtmlControl
{
    HtmlControl genericControl = (T)Activator.CreateInstance(typeof(T), new object[] { ParentWindow });
    if (type == PropertyType.Name)
        genericControl.SearchProperties[HtmlControl.PropertyNames.Name] = propertyvalue;
    else if (type == PropertyType.Id)
        genericControl.SearchProperties[HtmlControl.PropertyNames.Id] = propertyvalue;
    //Type in the UI
    Keyboard.SendKeys(genericControl, text);
}
Calling the above method To call the above methods use the code snippets below
EnterText(PropertyType.Name, "UserName", "Karthik"); 

Thanks for reading the post and watching the video !!! Please leave your comments and let me know if there is anything to improve in this post !!! Thanks, Karthik KK

Post Author: Karthik kk

14 Replies to “Creating generic method for control with Generic in Coded UI”

  1. Hi Karthik,

    Thanks for your Knowledge sharing its pretty much clear .

    I have a doubt here
    If the Same generic method using the button what about the string text parameter in the method.

  2. I am trying to access a dropdown and the HTML view is:

    When I am using HTML textarea in codedUI, it is throwing controlnotfound exception.

    Since here it is textarea tag, why controlnotfound is thrown?
    IS the HTML reference used is wrong?

  3. Hi Karthik,

    Appreciated for sharing all CodedUI too knowledge. I have doubt on this topic, for example, if I have to pass multiple properties with values(example: name, controltype, class..etc) from the generic control method what would be the best option?

    Thanks,
    Govardhan

  4. Hi Karthik,

    Thank you for these wonderful videos! 🙂

    By the way same as @Govardhan, I was wondering what would I do if I need to pass multiple properties. Also using generic method in finding controls, how can pass a parent control to a child control. Like if I am doing a hover and i need to pass this parent control to my child control for me to find it. Looking up a child control using an ID or Name does not work. How do is it done?

    Thanks!

    1. For your questions

      1. Passing multiple properties.
      For a small example, I have passed only one property as string. If you have multiple properties, then pass it as string array String[] or any collection

      2. Parent control
      For this example, I have passed Parent as a property, which is already set while the browser window is found and set, you your parents are likely going to change, may be an iframe or window or something else, then you need to write a mechanism to change it as well.

      Hope this answers your questions !!!

      Thanks,
      Karthik KK

      1. Hi Karthik,

        Sorry for a very late reply, but thank you. I have updated my script and it is now working.
        I have passed the main control as a property to the child control.

        Thanks!

      2. Passing multiple properties

        I have multiple properties, then how to pass it as string array String[] or any collection

        can you please give me syntax for above .

        Thanks,
        Mahesh

    2. Hi,
      First of all thanks a Ton to Karthik for such a wonderful and helpful blog :-). Regarding the query, I would say that as per suggested by kartik we can use the collection to pass the multiple properties. Below is the code that I follow in my scripts to passs the multiple properties.
      First I created an enum to store the diferent control types. These control types have be later used in the generic method.

      *** PLEASE CHECK MY QUERY AT THE END OF THIS ANSWER, AND IF ANYONE CAN RESOLVE IT, I MUCH APPRECIATE FOR THE HELP.
      // FIRST WE CREATE THE ENUM
      public enum htmlControls_PropertyTypes
      {
      Class,
      ClassName,
      FriendlyName,
      Id,
      InnerText,
      Name,
      TechnologyName,
      TagName,
      ControlType
      }

      // BELOW IS THE GENERIC METHOD WHERE THE ABOVE DEFINED CONTROL TYPES HAS BEEN USED.
      public HtmlControl html_Control(object controlType, List propertyType, List propertyValue)
      {
      HtmlControl htmlElement = null;
      Hashtable ht = new Hashtable();
      for (var i = 0; i < propertyType.Count; i++)
      {
      ht.Add(propertyType[i], propertyValue[i]);
      }

      if (controlType is HtmlImage)
      {
      htmlElement = new HtmlImage(ParentBrowserWindowInstance);
      }
      else if (controlType is HtmlSpan)
      {
      htmlElement = new HtmlSpan(ParentBrowserWindowInstance);
      }
      else if (controlType is HtmlHyperlink)
      {
      htmlElement = new HtmlHyperlink(ParentBrowserWindowInstance);
      }

      for (var i = 0; i < propertyType.Count; i++)
      {
      // Identifying the control on basis of its search properties.
      if (propertyType[i] == htmlControls_PropertyTypes.Class)
      {
      //htmlElement.SearchProperties[htmlElement.] = propertyValue[i];
      htmlElement.SearchProperties.Add("Class", propertyValue[i]);
      }
      else if (propertyType[i] == htmlControls_PropertyTypes.ClassName)
      {
      // htmlElement.SearchProperties[htmlElement.ClassName] = propertyValue[i];
      htmlElement.SearchProperties.Add("ClassName", propertyValue[i]);
      }
      else if (propertyType[i] == htmlControls_PropertyTypes.FriendlyName)
      {
      // htmlElement.SearchProperties[htmlElement.FriendlyName] = propertyValue[i];
      htmlElement.SearchProperties.Add("FriendlyName", propertyValue[i]);
      }
      else if (propertyType[i] == htmlControls_PropertyTypes.Id)
      {
      // htmlElement.SearchProperties[htmlElement.Id] = propertyValue[i];
      htmlElement.SearchProperties.Add("Id", propertyValue[i]);
      }
      else if (propertyType[i] == htmlControls_PropertyTypes.InnerText)
      {
      // htmlElement.SearchProperties[htmlElement.InnerText] = propertyValue[i];
      htmlElement.SearchProperties.Add("InnerText", propertyValue[i]);
      }
      else if (propertyType[i] == htmlControls_PropertyTypes.Name)
      {
      //htmlElement.SearchProperties[htmlElement.Name] = propertyValue[i];
      htmlElement.SearchProperties.Add("Name", propertyValue[i]);
      }
      else if (propertyType[i] == htmlControls_PropertyTypes.TechnologyName)
      {
      //htmlElement.SearchProperties[htmlElement.TechnologyName] = propertyValue[i];
      htmlElement.SearchProperties.Add("TechnologyName", propertyValue[i]);
      }
      else if (propertyType[i] == htmlControls_PropertyTypes.TagName)
      {
      //htmlElement.SearchProperties[htmlElement.TechnologyName] = propertyValue[i];
      htmlElement.SearchProperties.Add("TagName", propertyValue[i]);
      }
      else if (propertyType[i] == htmlControls_PropertyTypes.ControlType)
      {
      //htmlElement.SearchProperties[htmlElement.TechnologyName] = propertyValue[i];
      htmlElement.SearchProperties.Add("ControlType", propertyValue[i]);
      }
      }
      //Mouse.Click(htmlElement);
      return htmlElement;
      }

      MY QUERY :- How can I use the same concept for the window based and wpf based controls. The main problem is that when dealing with 'window' based and 'Wpf' based applications, I want to pass an extra parameter as the 'ParentWindow' name. There is no issue with the web applications, as here we are using the same browser instance across the methods, but in 'Wpf' and 'Window' based applications, there is nothing like same browser instance. Also, note that I am declaring this method in some 'Common Class A' and wants to use the method in some other class say 'ClassB'.

  5. Hi Karthik,
    All your videos has been very helpful in learning. However, I have the below query, which is the major hurdle in proceeding in one of my tasks. Can you please suggest something in it.
    Referring to the above video I am trying to create the generic method for wpf & window application. Below is the line which is causing me the huge problem. Please suggest.

    public controlTypeEntered wpf_ControlcontrolTypeEntered(ListwpfControls_PropertyTypes propertyType, ListString propertyValue, WpfControl controlWindow) where controlTypeEntered WpfControl
    {
    WpfControl wpfElement = (controlTypeEntered)Activator.CreateInstance(typeof(controlTypeEntered), new object[] { controlWindow });
    //I want to know as of what is the issue with the above line, as using the above line always shows me the error. In above line, the ‘controlWindow’ is the name of the ‘wpf window’ or ‘Windows window’ that would be passed in the calling class.
    .
    .
    .
    return (controlTypeEntered)convert.ChangeType(wpfElement, typeof(controlTypeEntered))
    }

  6. Hi Karthik,

    i am using the above specified code using generics, but when i am using PropertyType i am getting error “the type or namespace for ‘propertytype’ could not be found” even on using System.Reflection namespace.

    i am also looking for, typecast htmldiv to htmlcmbobox control. can we do this? if yes, could please share some inputs on this.

    Thanks in advance

Leave a Reply to Kishor Gaur Cancel reply

Your email address will not be published. Required fields are marked *