Using UISpy to Handle the Windows Security Dialog in Windows 7

In the previous post, I gave a solution for automating the dreaded “Windows Security” dialog in IE using UIAutomation. Reader Ven left this comment:

“Hey, This is exactly what I’ve been looking for, but when I seem to run it on IE, the code breaks when it reaches
AutomationElement userList = windowsSecurityDialog.FindFirst(TreeScope.Children, userListCondition);
and gives me a null value, I’m not entirely sure why, but I was also wondering where did you obtain these parameters from?”

Turns out that my code works great on Windows 8, but fails on Windows 7 (Ven’s config). Originally I mentioned that I would fix up my code and update the gist. But it got me thinking: I should probably explain how I even figured out the code to begin with. And what better way to do it, than to show how I used UISpy.exe

The key to figuring out the code was to use a tool called UISpy. This tool is very similar to Spy++ in that both navigate the UI elements within a window. Here’s a good explanation from the MSDN forums:

1) Is there any difference between UISpy.exe and Spy++ ?

UISpy uses UI Automation API to acquire user interface accessibility information. Spy++ uses other Win32 API to acquire window (HWND) object information on the desktop. The level of details and types of information are different because of the different information source. You can learn more about UISpy at http://msdn.microsoft.com/en-us/library/ms727247.aspx and Spy++ at http://msdn.microsoft.com/en-us/library/aa242713(VS.60).aspx and  http://blogs.msdn.com/vcblog/archive/2007/01/16/spy-internals.aspx .

The first problem is acquiring UISpy.exe. You can try to find it in one of the older Windows SDKs (supposedly), or you can download from my SkyDrive: http://sdrv.ms/13Ky16o. It’s just an executable and doesn’t require any installation.

There are lots of really good tutorials on using UISpy, so I’ll just be presenting highlights in this post. I highly recommend visiting the docs on MSDN or just Bing it. This post is going to show how I used UISpy on Win8 to solve the original problem, and then augment my previous code to work with Windows 7. So let’s start by navigating IE to my page under test and fire up UISpy on Win8:

image

The UI is quite straightforward. The Content View shows a tree control where the root node is the Desktop, and the children are all the child windows from the Desktop. The Properties window shows all of the UIAutomation properties of the item selected in the Content View tree.

Now, let try to find the “Windows Security” dialog in the UISpy content tree. To do this, make sure the “Focus Tracking Mode” button (the icon looks like a keyboard) is selected. This allows you to click on any control in any window and have the tree automatically select the item that has focus. When I select the User Name edit control in the Windows Security dialog, UISpy now looks like this:

image

As you can see, the Content View now has “edit” “User name” selected and there’s a red outline around the User name control in the security dialog. If you click around to different controls (or windows) you’ll see the same behavior where it gets a red outline and is selected in UISpy. So what are we really looking at?

UISpy is showing us the element hierarchy in the window. We see that Windows Internet Explorer is a “pane” that has a child “dialog” called “Windows Security”, that has a child “list”, that has a child “list item” called “Use another account”, which has a child “edit” control called “User name”.

At this point, I’d like to refer to the excellent tutorial on http://www.mathpirate.net/log/2009/09/27/swa-straight-outta-redmond/. He walks thru using UISpy to automate calc with UIAutomation. Go take a few minutes and read that, then come back here. Go on, I’ll wait patiently…

Great, you’re back! Good read,eh? Let’s see how that relates to my Gist.

We need to set up the Condition objects in order to create the queries for navigating the elements. As described above, we need to find IE –> Windows Security –> List –> ListItem called “Use another account” –> edit controls –> OK button. Once again, here’s the gist:

Now let’s take a look at Ven’s comment from the previous post again:

“…the code breaks when it reaches
AutomationElement userList = windowsSecurityDialog.FindFirst(TreeScope.Children, userListCondition);
and gives me a null value”

 

Interesting. This implies that there’s no user list.

So, let’s start my Win7 VM and fire up UISpy.exe. Once I navigate my IE to my website under test, UISpy looks like the following:

UISpy

Ok, this is basically the same as Windows 8.

Now, let try to find the “Windows Security” dialog in the UISpy content tree. When I select the User Name edit control in the Windows Security dialog, UISpy now looks like this:UISpy_UserName_Selected

We see that now Windows Internet Explorer has a child Dialog called “Windows Security”, that has a child “list item” called “Use another account”, which has a child “edit” control called “User name”. And how does this compare to Windows 8?

Win7: IE “pane” –> Windows Security “dialog” –>  ListItem called “Use another account” –> edit controls –> OK button

Win8: IE “pane” –> Windows Security “dialog” –> List –> ListItem called “Use another account” –> edit controls –> OK button

Doh! The Windows 8 version of the dialog has the “User another account” listitem as a child of a “List” element, whereas on Windows 7 the listitem is a direct child of the dialog itself. If we dig in a little more, there are also a few other differences:

  1. The class name for the “Use another account” listitem is different
  2. The class name and AutomationId for the username edit box is different
  3. The class name and AutomationId for the password edit box is different

Armed with this info, let’s fix up the code to work on Windows 8 and Windows 7. The new code is as follows:

So with a little investigation, we were able to make the code work across Windows 8 and Windows 7. I’ll leave Vista and XP as an exercise to the reader. 🙂

Advertisements
This entry was posted in testing, Work and tagged , , , , , , . Bookmark the permalink.

4 Responses to Using UISpy to Handle the Windows Security Dialog in Windows 7

  1. Ven says:

    This is actually great tutorial and will definitely come in handy in the future!

  2. Ashka Zaveri says:

    Hi,
    I have tried this code for windows security dialog of IE 9, but its not working, its getting null for “userTile”. While it is able to click submit button.
    Can you please help me out for this?

  3. Rajesh Sivadasan says:

    Do you have VBA code to achieve the above funcitonality

  4. Prasad says:

    Hello – I came across this post while googling for a solution to handle the Windows security pop-up.
    I have an automated solution for handling the same pop-up using AutoIT V3. Compiled it in to an exe and plugged the exe in to selenium (was running it using Runtime.getRuntime().exec()). It was failing as i was running my selenium scripts in threads. When the code sees more than 1 dialog on screen, it fails to identify and enter details in all.

    I believe UIspy, spy++ etc are using the same methodology for identifying the windows objects (class, hwnd etc).

    Do you think the above code can handle the multiple pop-ups displayed at same time. I am going to try your code, but just thought of knowing if you have came across similar situation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s