0

I've got web application with browser authentication before webpage is loaded so in automated test i am log in via http://user:password@domain but when i am entering wrong credentials, pop up would not disappear it would wait for correct credentials. But i want to test if there is a access to webpage with wrong credentials, every browser is closing without problem, but IE is throwing modal dialog present

i was trying to use

driver.SwitchTo().Alert().Dismiss();

but it doesn't work. any idea how to close that pop up authentication?

Yxop
  • 5
  • 3

2 Answers2

2

Authentication popup is NOT generated by Javascript / it is not a javascript alert. So It can not be handled by WebDriver.

You did not mention the programming language you use. Your sample code seems to be in C#. In Java - we have a java.awt.Robot to simulate keyboard events. You might have to find C# equivalent to press the ESC key.

    Robot robot = new Robot();
    //Press ESC key
    robot.keyPress(InputEvent.VK_ESCAPE);
    robot.keyRelease(InputEvent.VK_ESCAPE);
vins
  • 15,030
  • 3
  • 36
  • 47
0

In project I currently work in I decided to take completely another approach. I had similar situation of NTLM authentication but I'm pretty sure that for basic authentication it will work as well. I wrote simple chrome extension which utilizes listener on chrome.webRequest.onAuthRequired. Additionally, by putting additional methods in content script to communicate with background script I've managed a way to change credentials on the fly without caring about annoying windows.

background.js:

var CurrentCredentials = {
    user: undefined,
    password: undefined
}

chrome.runtime.onMessage.addListener(function(request) {
    if(request.type === 'SET_CREDENTIALS') {
        CurrentCredentials.user = request.user;
        CurrentCredentials.password = request.password;
    }
});

chrome.webRequest.onAuthRequired.addListener(function(details, callback) {
    if(CurrentCredentials.user !== undefined && CurrentCredentials.password !== undefined) {
            return {authCredentials: 
            {
                username: CurrentCredentials.user,
                password: CurrentCredentials.password
            }
        };
    }
}, {urls: ["http://my-server/*"]}, ["blocking"]);

and content-script.js:

var port = chrome.runtime.connect();

window.addEventListener("message", function(event) {
  if (event.source !== window)
    return;

  if (event.data.type && (event.data.type === 'SET_CREDENTIALS')) {
    chrome.runtime.sendMessage({
        type: 'SET_CREDENTIALS',
        user: event.data.user, 
        password: event.data.password
    });
  }
}, false);

Extension must be packed as crx and added to ChromeOptions prior to driver initialization. Additionally, it is required to set credentials BEFORE actual call to site that needs authentication, so I browse simple html file on the disk and post a chrome message while being on the page: window.postMessage({type: 'SET_CREDENTIALS', user: arguments[0], password: arguments[1]}, '*') by using IJavascriptExecutor.ExecuteScript method. After it is done, no authentication window shows up and user is authentication as expected.

Janek Królikowski
  • 1,101
  • 1
  • 9
  • 10