TAG | Basic Authentication
8
Selenium, BASIC authentication, and how to get it to work in BrowserMob
No comments · Posted by Patrick Lightbody in Uncategorized
BASIC authentication is used to provide minimal, low-security protection from anonymous visitors hitting your website. It is frequently used by companies to ensure that their staging or development environments are not accessibly by the general public prior to pushing the changes to production. Typical authentication dialog prompts look like so:

The challenge here is that this dialog box is the kind of dialog that Selenium cannot automate. You cannot issue “type” or “click” commands on it. In fact, if this box comes up, your script is guaranteed to time out because Selenium will continue to wait for the page to load, not realizing a login is required and unable to populate it.
Traditionally Selenium users have worked around this problem by using a URL pattern in which the username and password was encoded:
http://username:password@example.com
Recently, however, this is not working across all browsers. IE no longer supports it and recent versions of Firefox add a confirmation box that breaks the automation with a slightly different popup. And while we’re working on as solution for Selenium 2.0, there aren’t a lot of options for Selenium today.

But when it comes to BrowserMob, we do have a solution available today, which will be soon back-porting to the Selenium 2 codebase. Simply edit your script to look like the following:
var selenium = browserMob.openBrowser(); var c = browserMob.getActiveHttpClient(); c.autoBasicAuthorization("example.com", "username", "password"); browserMob.beginTransaction(); browserMob.beginStep("Step 1"); selenium.open("http://example.com"); // rest of script...
This code tells BrowserMob to automatically put in the required Authorization headers for any HTTP request issued to example.com. And soon this capability will be added to Selenium 2 (after we get the first alpha out in time for the holidays).
