Handle HTTP Authentication – Authentication required dialog

I’m using Selenium with the Firefox Webdriver to test different events on my Website.

One event leads to a HTTP authentication dialog.
Is there a way to set selenium to always press cancel or to reach a timeout so I don’t hang around and get back the control of the driver?

johnsmithv2 Default Asked on February 25, 2015 in Selenium WebDriver.
Add Comment
4 Answer(s)

Hi John,

You can handle it using the Alert interface.

Code would look something like this

 WebDriver driver = <Your code to initialize>
 WebDriverWait wait = new WebDriverWait(driver, 10);
 Alert alert = wait.until(ExpectedConditions.alertIsPresent());
 alert.authenticateUsing(new UserAndPassword("username", "password");
 

You can get more details about the Alert interface here

https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/Alert.html

Do let us know if it works out for you.

Thanks
Virender

Virender Singh Professor Answered on February 26, 2015.
Add Comment

Hello,
I tried the following code but I’m not sure about the correct order of the coe lines.
Is it correct this way:


FirefoxProfile profile = new FirefoxProfile();

WebDriver driver = new FirefoxDriver(profile);
WebDriverWait wait = new WebDriverWait(driver, 10);

Alert alert = wait.until(ExpectedConditions.alertIsPresent());

alert.authenticateUsing(new UserAndPassword("username", "password"));
driver.get("http://demo.tanmaysarkar.com/authentication/");

Thanks
John

johnsmithv2 Default Answered on February 26, 2015.
Add Comment
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(profile);
WebDriverWait wait = new WebDriverWait(driver, 10);
 driver.get("http://demo.tanmaysarkar.com/authentication/");  //First reach to the page.

Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword("username", "password"));
Virender Singh Professor Answered on February 26, 2015.
Add Comment

Hello,
I found my own solution for the problem. I’m using a modified version of the Firefox add-on AutoAuth. Normally this clicks the OK button when you visited a HTTP Authentication dialog before and saved username and password. I modified the plugin so that it always cancels the dialog.
It’s working very well on Firefox 35.

My Java Code:


public class Main {
public static void main(String[] args) {
// create profile

FirefoxProfile profile = new FirefoxProfile();
//add a extension to firefox

File extension = new File("autoauth-2.1-fx+fn.xpi");

try {

profile.addExtension(extension);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}
WebDriver driver = new FirefoxDriver(profile);

driver.get("http://demo.tanmaysarkar.com/authentication/");

driver.get("http://demo.tanmaysarkar.com/authentication/");
try {

Thread.sleep(10000);

} catch (InterruptedException ex) {

Thread.currentThread().interrupt();

}
driver.quit();
}
}

You have to modify the firefox addon https://addons.mozilla.org/en-us/firefox/addon/autoauth/. In there is the file chrome/content/overlay.js
Replace the overlay.js with my version on http://pastebin.com/XRVxJdey

I hope this helps somebody!

johnsmithv2 Default Answered on February 26, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.