I have to test an application using selenium. The application itself authenticates against an active directory server. All user accounts are of the form "[email protected]". I have to use Java JUnit (4.x) tests that are using the selenium java client libs.
Unfortunately I'm not able to set the username and password directly into the url. As far as I know the @ sign has to be encoded within the url.
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://username%64domain.tld:password@server/appbase");
All I get is some authorization exceptions:
com.thoughtworks.selenium.SeleniumException: XHR ERROR: URL = http://username%64domain.tld:password@server/appbase/mapage Response_Code = 401 Error_Message = Unauthorized
Is there any way to set the user credentials (for a user with the given naming scheme) within the url? Are there other ways to give the user credentials programatically?
Thanks in advance for any answer(s)
Regards, Marco
I looked into doing authentication with Selenium recently and learned that url authentication like that is deprecated in most browsers due to security reasons.
Our solution (running selenium rc 1.0.3) was to write a function in user-extensions.js that looked for a login control (using a class name) and if it's found, inserts the id and password and logs in. This of course depends on using Forms authentication, so that the request chain is:
-
Selenium.prototype.doTryLogin = function(locator, text ) {
/**
* Tries to log in into a forms authentication site
* using the supplied locator and finding the first input[text]
* element for login name, the first input[password] element
* as password and the first input[submit] as submit button.
* Use TryLoginAndWait to ensure logging in is performed before
* next test step.
*
* @param locator the DOM container for login, password and button
* @param text part of login page name to check for
*
*/
// Check if the user has been redirected due to authentication
var location = this.getLocation().toLowerCase();
if (location.indexOf(text) > -1) {
this.doSetLoginName(locator + ' input[type=text]');
this.doSetPassword(locator + ' input[type=password]');
this.doClick(locator + ' input[type=submit]');
}
else {
this.doRefresh();
}
}
Selenium.prototype.doSetLoginName = function(locator, text ) {
var element = this.page().findElement(locator);
this.page().replaceText(element, 'your_id');
}
Selenium.prototype.doSetPassword = function(locator, text )
var element = this.page().findElement(locator);
this.page().replaceText(element, 'the_password');
}