首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JUnit和Selenium-RC编程设置凭据

使用JUnit和Selenium-RC编程设置凭据
EN

Stack Overflow用户
提问于 2011-02-15 05:34:49
回答 2查看 2.1K关注 0票数 2

我必须使用selenium测试应用程序。应用程序本身根据active directory服务器进行身份验证。所有用户帐户的格式均为"username@domain.tld“。我必须使用使用selenium Java客户端库的java JUnit (4.x)测试。

不幸的是,我不能直接在url中设置用户名和密码。据我所知,@符号必须编码在url中。

代码语言:javascript
复制
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://username%64domain.tld:password@server/appbase");

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

代码语言:javascript
复制
com.thoughtworks.selenium.SeleniumException: XHR ERROR: URL = http://username%64domain.tld:password@server/appbase/mapage Response_Code = 401 Error_Message = Unauthorized

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

提前感谢您的回复

向你致敬,马可

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-15 17:12:48

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

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

  1. 尝试获取您的页面
  2. 如果未登录,请重定向至登录页面
  3. perform login
  4. 如果通过身份验证,请再次重定向至您的页面

<代码>G29

-

代码语言:javascript
复制
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');
}
票数 3
EN

Stack Overflow用户

发布于 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来说是没有问题的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4997540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档