开发者

Set credentials programatically using JUnit and Selenium-RC

开发者 https://www.devze.com 2023-02-10 05:51 出处:网络
I have to test an application using selenium. The application itself authenticates against an active directory server. All user accounts are of the form \"username@domain.tld\"开发者_Python百科. I hav

I have to test an application using selenium. The application itself authenticates against an active directory server. All user accounts are of the form "username@domain.tld"开发者_Python百科. 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:

  1. try to get your page
  2. if not logged in, redirect to login page
  3. perform login
  4. if authenticated, redirect to your page again

-

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');
}


Magomi,

We recently stumbled across the same issue (including usernames being email-addresses and the need for encoding them);

The described solution: http://wiki.openqa.org/display/SEL/Selenium+Core+FAQ#SeleniumCoreFAQ-HowdoIuseSeleniumtologintositesthatrequireHTTPbasicauthentication%28wherethebrowsermakesamodaldialogaskingforcredentials%29%3F

is principally correct, however it kinda lacks some detail, thus your approach failed.

We got it working by: (following your example): a) config/setup: only the selenium base url without credentials b) start all tests with the following command: open http://username%64domain.tld:password@server/appbase

From then onwards Selenium (actually the Browser within it's current session) will send all subsequents requests including the above credentials and your AUT will feel fine ;-)

regards georg

PS: Using the above method in Selenium-IDE will still require you to click a pop-up; this pop-up however is no problem withing the latest Selenium-RC

0

精彩评论

暂无评论...
验证码 换一张
取 消