DEV Community


Posted on

Handling OS-level Authentication Dialogues in Internet Explorer using Selenium + AutoIt

For a few weeks, I've been working on a Selenium project and I'd to figure out a way to handle authentication dialogues in Internet Explorer (IE). The dilemma is that IE dialogues are non-browser dialogues, i.e., Windows-based dialogues. Therefore, they can't be handled using Selenium since Selenium doesn't recognize OS-level dialogues. I'll walk you through my solution which consists of two parts.

I used Selenium C# WebDriver. For other programming languages users, the concepts in this article are still applicable. You may need to import or use AutoIt package in a different way but nothing is really hard.

1- IE Settings:

I want IE to prompt the users to enter their credentials each time they open the browser because I'd like to run my tests against different users.

In your IE browser, click on Settings > Internet options > Security tab > check Enable Protected Mode > click on Custom level… > check Prompt for user name and password. Repeat the last three steps on all four zones (Internet, Local internet, Trusted sites, Restricted sites).

IE settings demo #1

IE settings demo #2

After applying the previous steps, IE should prompt the user to enter their credentials every time they open it.

2- C# code

To tackle the problem of Windows-based dialogues, I utilized a scripting language called AutoIt. By integrating AutoIt with Selenium, I was able to handle Windows- based dialogues smoothly. Let's dive into the C# code.

First step is to add AutoIt NuGet Package in Visual Studio. Refer to Microsoft Documentation. After referencing AutoIt NuGet Package (using AutoIt;), you will get a class named AutoItX with static methods ready to be used.

I created a method that takes the user credentials and handles the windows-based dialogues.

public static void SetAuthenticationCredentials(string UserName, string Pws)
            AutoItX.WinWaitActive("Windows Security");

A number of people ran into an exception after this step, so here's one way to use the previous method properly.

Parallel.Invoke(() => driver.Navigate().GoToUrl(""), () => SetAuthenticationCredentials("usertest2", "password2"));

In this way, you'll navigate to your website and invoke the SetAuthenticationCredentials method simultaneously.

Congratulations!!! right now you should be able to handle Windows-based dialogues easily. If you have any question, please don't hesitate asking me.

Top comments (0)