我必须使用 Selenium 测试应用程序。应用程序本身根据事件目录服务器进行身份验证。所有用户帐户的格式均为“[email protected]”。我必须使用使用 selenium java 客户端库的 Java JUnit (4.x) 测试。
不幸的是,我无法将用户名和密码直接设置到网址中。据我所知,@ 符号必须在 url 中进行编码。
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://username%64domain.tld:password@server/appbase");
我得到的只是一些授权异常(exception):
com.thoughtworks.selenium.SeleniumException: XHR ERROR: URL = http://username%64domain.tld:password@server/appbase/mapage Response_Code = 401 Error_Message = Unauthorized
有没有办法在 URL 中设置用户凭据(对于具有给定命名方案的用户)?是否有其他方法以编程方式提供用户凭据?
提前感谢您的回答
问候,马可
最佳答案
我最近研究了使用 Selenium 进行身份验证,并了解到由于安全原因,大多数浏览器中不推荐使用这样的 url 身份验证。
我们的解决方案(运行 selenium rc 1.0.3)是在 user-extensions.js 中编写一个函数来查找登录控件(使用类名),如果找到,则插入 ID 和密码并登录。这当然取决于使用Forms身份验证,因此请求链是:
- 尝试获取您的页面
- 如果未登录,则重定向到登录页面
- 执行登录
- 如果通过身份验证,则重定向到您的 再次翻页
-
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');
}
关于authentication - 使用 JUnit 和 Selenium-RC 以编程方式设置凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4997540/