我必须使用selenium测试应用程序。应用程序本身根据active directory服务器进行身份验证。所有用户帐户的格式均为"username@domain.tld“。我必须使用使用selenium Java客户端库的java JUnit (4.x)测试。
不幸的是,我不能直接在url中设置用户名和密码。据我所知,@符号必须编码在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中设置用户凭证(对于具有给定命名方案的用户)?是否有其他方法可以通过编程方式提供用户凭据?
提前感谢您的回复
向你致敬,马可
发布于 2011-02-15 17:12:48
我最近研究了使用Selenium进行身份验证,并了解到由于安全原因,在大多数浏览器中都不建议使用这种url身份验证。
我们的解决方案(运行selenium rc 1.0.3)是在user-extsions.js中编写一个函数,该函数查找登录控件(使用类名),如果找到,则插入id和密码并登录。当然,这取决于使用Forms身份验证,因此请求链是:
<代码>G29
-
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');
}发布于 2011-03-10 07:30:04
马戈米
我们最近遇到了同样的问题(包括用户名是电子邮件地址,需要对它们进行编码);
描述的解决方案:http://wiki.openqa.org/display/SEL/Selenium+Core+FAQ#SeleniumCoreFAQ-HowdoIuseSeleniumtologintositesthatrequireHTTPbasicauthentication%28wherethebrowsermakesamodaldialogaskingforcredentials%29%3F
原则上是正确的,但是它有点缺乏一些细节,因此你的方法失败了。
我们通过以下方式使其正常工作:(按照您的示例操作):a) config/setup:仅使用无凭据的selenium基本url b)使用以下命令启动所有测试: open http://username%64domain.tld:password@server/appbase
从那时起,Selenium (实际上是它当前会话中的浏览器)将发送所有后续请求,包括上面的凭据,您的AUT将感觉良好;-)
关于格奥尔格
PS:在Selenium-IDE中使用上面的方法仍然需要你点击一个弹出窗口;但是这个弹出窗口对于最新的Selenium-RC来说是没有问题的
https://stackoverflow.com/questions/4997540
复制相似问题