Site Loading

Automating NTLM logins with Selenium

We have been automating the testing of our application using Selenium and TeamCity to automate the testing of an application we are developing. One of the challenges we have come across is how to automate logging on the test users when the application use NTLM authentication.

 

Problem:

Authentication_popup
Since the windows is not part of a web page but a separate window attached to the main firefox window, we can use a selenium to log in.

Solution:

uspy

 

To get around this issue, we used USpy to find out details about the Form. To do this see the screenshot above. We found the firefox window, selected that, found the authentication popup and selected the username textbox.

After that it became a little bit trickier. Using the UI automation framework and ATU_Handler we were able to send text to the textboxes and click the button.

Here is the code we used:

private void LogUserIn(string username, string password)
{
    WindowsHandler handler = new WindowsHandler();
    AutomationElement window = null;
    for (int i = 0; i < 20; i++)
    {
        try
        {
            // This works by selecting user name then pressing tab to get to other elements
            window = handler.FindWindowByName("Mozilla Firefox");
            AutomationElement authWindow = handler.GetElementByName(window, "Authentication Required");
            AutomationElement element = handler.GetElementByName(authWindow, "User Name:");
            handler.SendTextUsingUIAutomation(element, username);
            handler.SendTextUsingUIAutomation(element, char.ConvertFromUtf32(9));
            handler.SendTextUsingUIAutomation(element, password);
            handler.SendTextUsingUIAutomation(element, char.ConvertFromUtf32(9));
            handler.SendTextUsingUIAutomation(element, char.ConvertFromUtf32(32));
            break;
        }
        catch
        {
            //Do nothing
        }
        Thread.Sleep(1000);
    }
}

This works well up to the point that you cant click away when debugging this code since it wont focus on the textbox again. The other issue is that we battled to get it to find the password textbox and button. To fix this we send the tab command and the spacebar command to click the button.

This is based on a Blog post we found: http://automationtestingutilities.blogspot.co.uk/p/handling-windows-authentication.html

The Utilities can be found here: https://drive.google.com/folderview?id=0B7rZvkq9tkwPRmp5UG9DMUlJVDA&usp=sharing

Close