Visual Studio CodedUI – Cannot perform ‘Click’ on the hidden control

Introduction

A few weeks ago, a colleague of mine – Marcel de Vries – gave a presentation on a wednesday evening as he regularly does. This time it was about Visual Studio CodedUI and I was instantly sold. Think of the time we could save on our project by automating our regression test set! I immediately set about (in my spare time, since nobody asked for this – yet) to try and automate a full run-through of the project I’m currently working on.

I chose the manual route of automating the various controls on the web application we’re developing, rather than using the record and playback feature. As I’m doing this, I sometimes run into things that require some investigation. I will use this article, and perhaps a few of the following articles, to discuss the issues I ran into and how I solved them.

I am automating a web application that was not built with automation in mind. As such, we did not add IDs to elements that did not strictly require them. Instead of changing that, I chose to view this as a challenge and see if I can still build an efficient set of page objects.

Cannot perform ‘Click’? Why not?

That’s what I was wondering. I got the following error message:

Microsoft.VisualStudio.TestTools.UITest.Extension.FailedToPerformActionOnHiddenControlException: 
Cannot perform 'Click' on the hidden control. Additional Details: 
TechnologyName:  'Web'
ControlType:  'Button'
TagName:  'BUTTON'
Id:  ''
Name:  ''
DisplayText:  ' Save '
Type:  'submit'
 ---> System.Runtime.InteropServices.COMException: Exception from HRESULT: 0xF004F002

That is strange. While running the test and watching what was happening, I could clearly see the control on screen. So how come Visual Studio tells me it is hidden?

It turns out it had to do with the way I was finding the control. Since I, as mentioned before, am testing an application that was not built with automation in mind, I do not have the luxury of referencing controls by their ID. Instead, I had to look for the button based on its title property. That’s all well and good, but our screen is set up in such a way that there are several “command” bars with buttons on them, but only one of them is visible depending on the state the screen is in. It turns out that several of these bars have a Save button on them.

This problem was not immediately clear to me. The way I found out was by instead of trying to click on the control, highlight the controls that matched my search properties:

var button = new HtmlButton();
button.SearchProperties.Add(HtmlControl.PropertyNames.Title, " Save ");
foreach (var control in button.FindMatchingControls())
{
    control.DrawHighlight();
}

Note that since some of the controls that were found are hidden at this point, I only saw one highlight. However, knowing that the DrawHighlight method highlights the control for about seven seconds, I noticed there was a pause after highlighting the control on screen. This indicated to me that there would be more than one control. A quick debug session told me there were in fact three controls that matched!

By making use of the ability to provide a parent control, I managed to confine my search to the control I wanted:

var buttonBar = new HtmlDiv();
buttonBar.SearchProperties.Add(HtmlControl.PropertyNames.Id, "footercommandbarEdit");
var button = new HtmlButton(buttonBar);
button.SearchProperties.Add(HtmlControl.PropertyNames.Title, " Save ");
Mouse.Click(button);

Problem solved! Hope this helps someone.

Stay tuned for more things I run into!

Leave a Reply

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