使用 JUnit 和 Selenium-RC 以编程方式设置凭据

发布于 2024-10-17 12:38:12 字数 762 浏览 0 评论 0原文

我必须使用硒测试应用程序。应用程序本身根据活动目录服务器进行身份验证。所有用户帐户的格式均为“[email protected]”。我必须使用使用 selenium java 客户端库的 Java JUnit (4.x) 测试。

不幸的是,我无法将用户名和密码直接设置到网址中。据我所知,@ 符号必须在 url 中进行编码。

selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://username%64domain.tld:password@server/appbase");

我得到的只是一些授权异常:

com.thoughtworks.selenium.SeleniumException: XHR ERROR: URL = http://username%64domain.tld:password@server/appbase/mapage Response_Code = 401 Error_Message = Unauthorized

是否有任何方法可以在 url 中设置用户凭据(对于具有给定命名方案的用户)?是否有其他方法以编程方式向用户提供凭据?

预先感谢您的任何答复

问候,Marco

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风筝有风,海豚有海 2024-10-24 12:38:12

我最近研究了使用 Selenium 进行身份验证,并了解到由于安全原因,大多数浏览器中不推荐使用这样的 url 身份验证。

我们的解决方案(运行 selenium rc 1.0.3)是在 user-extensions.js 中编写一个函数来查找登录控件(使用类名),如果找到,则插入 id 和密码并登录。这当然取决于使用表单身份验证,因此请求链是:

  1. 则尝试获取您的页面
  2. 如果未登录,
  3. ,重定向到登录页面,如果通过身份验证,则执行登录
  4. ,重定向到您的页面
    再次翻页

——

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

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');
}
流绪微梦 2024-10-24 12:38:12

Magomi,

我们最近偶然发现了同样的问题(包括用户名是电子邮件地址以及对它们进行编码的需要);

所描述的解决方案:
http://wiki.openqa.org /display/SEL/Selenium+Core+FAQ#SeleniumCoreFAQ-HowdoIuseSeleniumtologintositesthatrequireHTTPbasicauthentication%28wherethebrowsermakeamodaldialogaskingforcredentials%29%3F

基本上是正确的,但它有点缺乏一些细节,因此您的方法失败了。

我们通过以下方式使其工作:(按照您的示例):
a) 配置/设置:只有 selenium 基本 url,没有凭据
b) 使用以下命令启动所有测试:
打开
http://username%64domain.tld:password@server/appbase

从那时起 Selenium (实际上是当前会话中的浏览器)将发送所有后续请求,包括上述凭据,并且您的 AUT 会感觉良好;-)

问候 georg

PS:在 Selenium-IDE 中使用上述方法仍然需要您单击弹出窗口;然而,这个弹出窗口对于最新的 Selenium-RC 来说是没有问题的

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文