I have a very simple selenium-webdriver script. I would like to do HTTP authentication using webdriver.
Script:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.httpwatch.com/httpgallery/authentication/");
driver.findElement(By.id("displayImage")).click();
Thread.sleep(2000);
driver.switchTo().alert().sendKeys("httpwatch");
Issue:
driver.switchTo().alert().sendKeys("httpwatch");
throws
org.openqa.selenium.NoAlertPresentException: No alert is present
Question:
EDIT
Alert has below method and does not seem to have been implemented yet.
driver.switchTo().alert().authenticateUsing(new UsernameAndPassword("username","password"))
Handling login pop-up in Selenium by passing the credential in URL. The Basic authentication pop-up is like the alert pop-up . When we navigated to a specific web page that asks for the credential. To handle this, we can pass credentials (Username + Password) to the web page's URL.
Pass username and password in the URL Passing username and password in the URL helps to avoid the login prompt. This is achieved by encoding the username and password in the URL, that is, prepending username:password@ to the hostname in the URL.
The basic authentication pop-up is similar to the alert that pop-ups when the browser is navigated to a specific web page. To handle the basic authentication popup, we can pass the username and password along with the web page's URL. The syntax for handling this login pop up is: https://username:password@URL.
The problem is that this is not a javascript popup hence you cannot manipulate it via selenium's alert()
.
If both AutoIt and submitting credentials in the URL (the easiest option - just open up the url and click "Display Image") are not options for you, another approach could be to use AutoAuth firefox addon to automatically submit the previously saved credentials:
AutoAuth automatically submits HTTP authentication dialogs when you’ve chosen to have the browser save your login information. (If you’ve already told the browser what your username and password are, and you’ve told it to remember that username and password, why not just have it automatically submit it instead of asking you each time?)
Following the answer suggested in HTTP Basic Auth via URL in Firefox does not work? thread:
- Install AutoAuth Firefox plugin;
- Visit the site where the authentication is needed. Enter your username and password and make sure to choose to save the credentials;
- Save AutoAuth installation file at your hard drive: at the plugin page, right click at “Add to Firefox” and “Save link as”;
- Instantiate Firefox webdriver as following:
FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
File pluginAutoAuth = new File("src/test/resources/autoauth-2.1-fx+fn.xpi");
firefoxProfile.addExtension(pluginAutoAuth);
driver = new FirefoxDriver(firefoxProfile);
Also, in a way similar to AutoIt option - you can use sikuli
screen recognition and automation tool to submit the credentials in the popup.
Also see other ideas and options:
The Basic/NTLM authentication pop-up is a browser dialog window. WebDriver (Selenium 2.0) cannot interact with such dialog windows. The reason for this is because WebDriver aims solely at mimicking user interaction with websites, and browser dialog windows are currently not in that scope. JavaScript dialog windows, are part of the website, so WebDriver can handle those. In Selenium 1.0 it is possible to do basic authentication.
So how to solve this issue? 1) Authentication via URL http://username:[email protected]
2) Use a browser plugin that will handle the Basic/NTLM autentication 3) Use a local proxy that will modify the request header and pass along the username/password and 4) Make use of a robot, like AutoIt, or some Java
library.
Option 1: is the easiest and has the least impact on the system/test. Option 2: has a high browser impact as your loading plugins. Also every browser uses its own plugin and it's possible that the required plugin for a certain browser is not available. Option 3: Works well with HTTP, but HTTPS requires custom certicates thus not always an option. Not much impact on both system and test. Option 4: Mimics keyboard presses, its a go to solution but prone to errors. Only works when the dialog window has focus and it is possible that this is not always the case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With