如何让Selenium支持http basic和Digest HTTP认证

来源:互联网 发布:医院监护仪数据怎么看 编辑:程序博客网 时间:2024/05/20 11:52
如果使用Selenium进行自动测试,如果服务器要求http/https basic或者Digest认证,IE或者firefox会弹出一个对话框,要求用户输入用户名和密码。
现在Selenium也没有计划实现他(详见:http://code.google.com/p/selenium/issues/detail?id=34&redir=1)

就IE来说,我们有下面几种解决方式:
1,让IE支持 "http://<user>:<password>@<host>" 的格式。这样用户名和密码就可以和URL在一起。
方法:导入下面的格式到注册表
REGEDIT4[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl][HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_HTTP_USERNAME_PASSWORD_DISABLE]"iexplore.exe"=dword:00000000"explorer.exe"=dword:00000000

然后使用

下面方式访问网页:

driver.get("http://username:password@www.basicauthprotected.com/")

2,参照http://code.google.com/p/selenium/issues/detail?id=34&redir=1

Comment 54 by raghukir...@gmail.com,Jun 21, 2012

I am attaching the AutoIT script i wrote to use as a work around for my automation. It does not work in Linux. The script is generic takes command line arguments to click on OK or Cancel button in authentication window.

This is just a work around 'not' a permanent / perfect solution, I am still working on it. Feel free to use if it helps.

Please follow below steps if you intend to use it.
1. Download AutoIT software and install it.
2. download loginFF.au3 script in this post attachments.
3. Open "Compile Script to .exe" utility that comes with AutoIT and store the executable in a conveninet location.


Include below steps in selenium Java Code.
String[] dialog =  new String[]{ "C:\\loginFF.exe","Authentication Required","user1", "Test@123", "ok" };
Process pp1 = Runtime.getRuntime().exec(dialog);
selenium.open("/"); // Open url that gives a auth pop-up!
System.out.println("###### " + pp1.exitValue());