近在做WIFI吞吐率的测试,需要登录到无线路由器的Web管理页面对无线参数的频段和模式等进行更改。更改无线参数之后,再使用IxChariot等工具对WIFI吞吐率进行测试。项目组想把这个过程自动化起来,IxChariot工具自动化很容易实现,对无线参数的更改决定使用Selenium工具进行自动化更改。遇到的问题是,访问http://192.168.1.1时,无法解决登录问题,因为Selenium不支持Windows安全对话框(windows security dialog)!对话框上面的信息为:位于Mercury Wireless Router MW548R的服务器192.168.1.1 要求用户名和密码。警告:此服务器要求以不安全的方式发送您的用户名和密码(没有安全连接的基本认证)。
来自www.openqa.org的解决方式为:
How do I use Selenium to login to sites that require HTTP basic authentication (where the browser makes a modal dialog asking for credentials)?
Use a username and password in the URL, as described in RFC 1738:
Test Type
open http://myusername:myuserpassword@myexample.com/blah/blah/blah
Note that on Internet Explorer this won’t work, since Microsoft has disabled usernames/passwords in URLs in IE. However, you can add that functionality back in by modifying your registry, as described in the linked KB article. Set an “iexplore.exe” DWORD to 0 in HKEY_CURRENT_USERSoftwareMicrosoftInternet ExplorerMainFeatureControlFEATURE_HTTP_USERNAME_PASSWORD_DISABLE.
If you don’t want to modify the registry yourself, you can always just use Selenium Remote Control, which automatically sets that that registry key for you as of version 0.9.2.
中文解释如下:
可以把用户名和密码加到URL中进行解决,比如http://admin:admin@192.168.1.1这样不会再需要登录,直接进去操作即可。对于IE,需要修改注册表,把下属内容复制到reg格式的文件双击执行即可。
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USERSoftwareMicrosoftInternet ExplorerMainFeatureControlFEATURE_HTTP_USERNAME_PASSWORD_DISABLE]
“iexplore.exe”=dword:00000000
下面给出相关的Selenium的源代码:
package dw.junit;
import org.junit.*;
import com.thoughtworks.selenium.*;
public class MercuryTesting extends SeleneseTestBase {
private static Selenium selenium;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
selenium = new DefaultSelenium("localhost", 4444, "*iexplore",
http://192.168.1.1);
System.out.println("正在启动Selenium。。。");
selenium.start();
selenium.setTimeout(60 * 1000 + "");
selenium.windowMaximize();
selenium.open(http://admin:admin@192.168.1.1/);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
if (selenium != null) {
System.out.println("停止Selenium!");
selenium.stop();
}
}
@Test
public void testaa() {
// 点击左侧的无线参数导航链接
selenium.click("//a[text()='无线参数']");
// 切换模式
selenium.select("//select[@name='mode']", "label=11Mbps (802.11b)");
}
}