Site Loader
Auckland, New Zealand
In this post we are going to start creating Custom reusable methods for some of the controls like
  • HtmlEdit
  • HtmlTextArea
  • HtmlInputButton
  • HtmlButton
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

Post Author: Karthik kk

7 Replies to “Creating custom reusable methods for different controls in Coded UI Testing”

  1. Hi,
    I am getting below error. Please advise:

    Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException: Unable to find Windows Internet Explorer window with ‘IE – Windows Internet Explorer’ in the title. —> Microsoft.VisualStudio.TestTools.UITest.Extension.UITestControlNotFoundException: The playback failed to find the control with the given search properties. Additional Details:
    TechnologyName: ‘MSAA’
    ControlType: ‘Window’
    Name: ‘IE – Windows Internet Explorer’
    ClassName: ‘IEFrame’
    —> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.
    Result StackTrace:
    at Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.IRPFPlayback.FindAllScreenElements(IScreenElement pScreenElementStart, String bstrQueryId, Object& pvarResKeys, Int32 cResKeys, Int32 nMaxDepth, Object[]& foundDescendants)
    at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindTopLevelWindowHelper(String queryId)
    at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.FindFromPartialQueryId(String queryId, Boolean resetSkipStep)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl..ctor(String queryId)
    — End of inner exception stack trace —
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapControlNotFoundException(COMException ex, IPlaybackContext context)
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowComException(COMException innerException, IPlaybackContext context)
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(Exception exception, IPlaybackContext context)
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(Exception exception, String queryId)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl..ctor(String queryId)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.SearchTopLevelWindow(ISearchArgument topLevelSearchArg, String topLevelElementQueryId)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetTitleUpdatedTopLevelWindow(ISearchArgument topLevelSearchArg, String queryId, IList`1 windowTitles)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetTopLevelElement(Boolean useCache, Boolean useCacheOnly, ISearchArgument searchArg, IList`1 windowTitles, Int32& timeLeft)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetElement(Boolean useCache, ISearchArgument searchArg)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.Search(ISearchArgument searchArg)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindInternal()
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.b__37()
    at Microsoft.VisualStudio.TestTools.UITesting.CodedUITestMethodInvoker.InvokeMethod[T](Func`1 function, UITestControl control, Boolean firePlaybackErrorEvent, Boolean logAsAction)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.Find()
    at Microsoft.VisualStudio.TestTools.UITesting.IEBrowserService.Locate(PropertyExpressionCollection searchProperties, PropertyExpressionCollection filterProperties, Boolean findAll)
    — End of inner exception stack trace —
    at Microsoft.VisualStudio.TestTools.UITesting.IEBrowserService.Locate(PropertyExpressionCollection searchProperties, PropertyExpressionCollection filterProperties, Boolean findAll)
    at Microsoft.VisualStudio.TestTools.UITesting.IEBrowserFactory.Locate(PropertyExpressionCollection searchProperties, PropertyExpressionCollection filterProperties)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.SearchTopLevelWindow(ISearchArgument topLevelSearchArg, String topLevelElementQueryId)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetTitleUpdatedTopLevelWindow(ISearchArgument topLevelSearchArg, String queryId, IList`1 windowTitles)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetTopLevelElement(Boolean useCache, Boolean useCacheOnly, ISearchArgument searchArg, IList`1 windowTitles, Int32& timeLeft)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetUITestControlRecursive(Boolean useCache, Boolean alwaysSearch, ISearchArgument searchArg, IList`1 windowTitles, Int32& timeLeft)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetUITestControlRecursive(Boolean useCache, Boolean alwaysSearch, ISearchArgument searchArg, IList`1 windowTitles, Int32& timeLeft)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.GetElement(Boolean useCache, ISearchArgument searchArg)
    at Microsoft.VisualStudio.TestTools.UITesting.SearchHelper.Search(ISearchArgument searchArg)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.FindInternal()
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.b__37()
    at Microsoft.VisualStudio.TestTools.UITesting.CodedUITestMethodInvoker.InvokeMethod[T](Func`1 function, UITestControl control, Boolean firePlaybackErrorEvent, Boolean logAsAction)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.Find()
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetPropertyPrivate(String propertyName)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.c__DisplayClass3b.b__3a()
    at Microsoft.VisualStudio.TestTools.UITesting.CodedUITestMethodInvoker.InvokeMethod[T](Func`1 function, UITestControl control, Boolean firePlaybackErrorEvent, Boolean logAsAction)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.GetProperty(String propertyName)
    at Microsoft.VisualStudio.TestTools.UITesting.ALUtility.GetTechElementFromUITestControl(UITestControl uiTestControl)
    at Microsoft.VisualStudio.TestTools.UITesting.ActionExecutorManager.GetActionExecutor(UITestControl uiControl)
    at Microsoft.VisualStudio.TestTools.UITesting.Keyboard.SendKeysImplementation(UITestControl control, String text, ModifierKeys modifierKeys, Boolean isEncoded, Boolean isUnicode)
    at Microsoft.VisualStudio.TestTools.UITesting.Keyboard.c__DisplayClass2.b__1()
    at Microsoft.VisualStudio.TestTools.UITesting.CodedUITestMethodInvoker.InvokeMethod[T](Func`1 function, UITestControl control, Boolean firePlaybackErrorEvent, Boolean logAsAction)
    at Microsoft.VisualStudio.TestTools.UITesting.Keyboard.SendKeys(UITestControl control, String text, ModifierKeys modifierKeys, Boolean isEncoded, Boolean isUnicode)
    at Microsoft.VisualStudio.TestTools.UITesting.Keyboard.SendKeys(UITestControl control, String text)
    at FirstRecordAndPlayback.CustomClass.EnterText[T](PropertyType type, String propertyValue, String text) in c:\Users\mohanr\Documents\Visual Studio 2013\Projects\FirstRecordAndPlayback\FirstRecordAndPlayback\CustomClass.cs:line 173
    at FirstRecordAndPlayback.CustomClass.CustomWebMethods() in c:\Users\mohanr\Documents\Visual Studio 2013\Projects\FirstRecordAndPlayback\FirstRecordAndPlayback\CustomClass.cs:line 72
    at FirstRecordAndPlayback.CodedUITest1.CodedUITestMethod1() in c:\Users\mohanr\Documents\Visual Studio 2013\Projects\FirstRecordAndPlayback\FirstRecordAndPlayback\CodedUITest1.cs:line 35

    1. I think the BrowserWindow is not recognized.

      Can you check if the browser property has changed.

      Thanks,
      Karthik KK

  2. HI Karthik,

    I am facing an issue on trying to click on a web control of a webpage. Issue is:-
    Microsoft.VisualStudio.TestTools.UITest.Extension.FailedToPerformActionOnHiddenControlException was unhandled by user code
    HResult=-268111863
    Message=Cannot perform ‘SetProperty of Text with value “test1234″‘ on the hidden control. Additional Details:
    TechnologyName: ‘Web’
    ControlType: ‘Edit’
    Id: ‘j_username’
    Name: ‘j_username’
    TagName: ‘INPUT’

    Source=Microsoft.VisualStudio.TestTools.UITesting
    BasicMessage=Cannot perform ‘SetProperty of Text with value “test1234″‘ on the hidden control.
    Action=SetProperty of Text with value “test1234”
    ObjectName=Username
    StackTrace:
    at Microsoft.VisualStudio.TestTools.UITesting.UITestLogGenerator.ExceptionRecorder(Exception exception, Boolean rethrow)
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowComException(COMException innerException, IPlaybackContext context)
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException exception, IPlaybackContext context)
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException exception, String actionName, UITestControl uiControl)
    at Microsoft.VisualStudio.TestTools.UITesting.Playback.MapAndThrowException(SystemException exception, String actionName, Object parameterValue, UITestControl uiControl)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.SetPropertyValue(String propertyName, Object propertyValue)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.SetProperty(String propertyName, Object value)
    at Microsoft.VisualStudio.TestTools.UITesting.HtmlControls.HtmlEdit.set_Text(String value)
    at CodedUITestProject10.CodedUITest1.CodedUITestMethod1() in c:\Users\C901797\Documents\Visual Studio 2012\Projects\CodedUITestProject10\CodedUITestProject10\CodedUITest1.cs:line 73
    InnerException: System.Runtime.InteropServices.COMException
    HResult=-268111870
    Message=Exception from HRESULT: 0xF004F002
    Source=Microsoft.VisualStudio.TestTools.UITest.Playback
    ErrorCode=-268111870
    StackTrace:
    at Microsoft.VisualStudio.TestTools.UITest.Playback.Engine.IScreenElement.SetValueAsEditBox(String bstrNewValue, Int32 nSetValueAsEditBoxFlag)
    at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.SetValueAsEditBox(String value, Boolean isEncoded, Int32 flag)
    at Microsoft.VisualStudio.TestTools.UITest.Playback.ScreenElement.SetValueAsEditBox(String value, Boolean isEncoded)
    at Microsoft.VisualStudio.TestTools.UITesting.TechnologyElementPropertyProvider.SetValueAsEditBox(UITestControl uiControl, String value, Boolean isEncoded, Boolean useCopyPaste)
    at Microsoft.VisualStudio.TestTools.UITesting.HtmlPropertyProvider.SetPropertyValue(UITestControl uiControl, String propertyName, Object value)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestPropertyProvider.SetPropertyValueWrapper(UITestControl uiControl, String propertyName, Object value)
    at Microsoft.VisualStudio.TestTools.UITesting.UITestControl.SetPropertyValue(String propertyName, Object propertyValue)
    InnerException:

    Microsoft Web Developer Tools contains the following components:
    Page Inspector: Tool that offers an efficient way to decompose Web Applications and diagnose front-end issues.
    Web Publishing: Extensions required for Web Publishing for both hosted servers as well as on premises.
    Web Form Templates: Includes the default templates for Web Form Applications.
    Editor Extensions: Includes HTML, CSS, and JS editor extensions that greatly enhance the development experience.

    Could you please explain me on how to proceed.

    1. It seems like your controls is not visible to Coded UI, make sure the control is visible or not hidden inside any other control.

      Thanks,
      Karthik KK

      1. Hi Karthik,

        I am able to set a focus on this control. But at the same time I am not able to do any kind of an action on it like a click or inputting a value into it.

        I tried capturing all the objects of the same type and filtering it by the object properties.
        Code used is:-
        HtmlControl controls = new HtmlControl(browser);
        controls.SearchProperties.Add(HtmlControl.PropertyNames.ClassName, “HtmlButton”);
        UITestControlCollection collection = controls.FindMatchingControls();
        foreach (UITestControl button in collection)
        {
        //cast the item to HtmlButton type
        HtmlInputButton myBtn = (HtmlInputButton)button;
        //get the innertext from the link, which inturn returns the link value itself

        if (myBtn.Name == “signIn”)// Button with name property is found
        {
        //myBtn.DrawHighlight();
        myBtn.SetFocus();// I am able to Set focus.
        Mouse.Click(myBtn);// not able to click the button. tool throwing the error posted above.
        }
        Thread.Sleep(5000);
        }

  3. hi
    how to call HtmlDiv/HtmlSpan Control Custom as a method for extracting values and clicking can anyone suggest me idea for this

  4. Most of my controls are identifying as HtmlDiv/HtmlSpan Control Custom.Could you please share any ideas on how to identify those. Ours is a EXT.JS based application

Leave a Reply

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