ExecuteAutomation

Creating custom reusable methods for different controls in Coded UI Testing

In this post we are going to start creating Custom reusable methods for some of the controls like The custom reusable methods are going to perform just two operations
  1. Entering text in the textbox, it can be either HtmlEdit or HtmlTextArea, but still I want to enter text in these controls needless of what type of control it is.
  2. I want to click the button, it can be either an HtmlInputButton or HtmlButton.
The complete explanation of writing the code for above criteria is given in the video below Here is the complete code HtmlEdit/HtmlTextArea Control Custom method
public void EnterText(object controlType, PropertyType type, string propertyvalue, string text)
{
    //Moving parts 
    //1. Parent 
    //2. Control Type 
    //3. Type of Property 
    //4. Property Value 

    if (controlType is HtmlEdit)
    {
        HtmlEdit edit = new HtmlEdit(ParentWindow);
        if (type == PropertyType.Name)
        {
            edit.SearchProperties[HtmlEdit.PropertyNames.Name] = propertyvalue;
        }
        else if (type == PropertyType.Id)
        {
            edit.SearchProperties[HtmlEdit.PropertyNames.Id] = propertyvalue;
        }
        //Typing in the UI 
        Keyboard.SendKeys(edit, text);

    }
    else if (controlType is HtmlTextArea)
    {
        //Perform operations for HtmlTextArea control
    }

} 
HtmlButton/HtmlInputButton Control Custom method
public void Click(object controlType, PropertyType type, string propertyvalue)
{
    if (controlType is HtmlInputButton)
    {
        HtmlInputButton btn = new HtmlInputButton(ParentWindow);
        if (type == PropertyType.Name)
        {
            btn.SearchProperties[HtmlInputButton.PropertyNames.Name] = propertyvalue;
        }
        else if (type == PropertyType.Id)
        {
            btn.SearchProperties[HtmlInputButton.PropertyNames.Id] = propertyvalue;
        }

        Mouse.Click(btn);
    }
    else if (controlType is HtmlButton)
    {

    }
} 
Calling the above method To call the above methods use the code snippets below
EnterText(new HtmlEdit(), PropertyType.Name, "UserName", "Karthik"); 
 
Click(new HtmlInputButton(), PropertyType.Name, "Login");
Thanks for reading the post and watching the video !!! Please leave your comments and let me know if there is anything need to be improved in post and video !!! Thanks, Karthik KK