近在做WIFI吞吐率的測試,需要登錄到無線路由器的Web管理頁面對無線參數(shù)的頻段和模式等進行更改。更改無線參數(shù)之后,再使用IxChariot等工具對WIFI吞吐率進行測試。項目組想把這個過程自動化起來,IxChariot工具自動化很容易實現(xiàn),對無線參數(shù)的更改決定使用Selenium工具進行自動化更改。遇到的問題是,訪問http://192.168.1.1時,無法解決登錄問題,因為Selenium不支持Windows安全對話框(windows security dialog)!對話框上面的信息為:位于Mercury Wireless Router MW548R的服務(wù)器192.168.1.1 要求用戶名和密碼。警告:此服務(wù)器要求以不安全的方式發(fā)送您的用戶名和密碼(沒有安全連接的基本認證)。
來自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,需要修改注冊表,把下屬內(nèi)容復(fù)制到reg格式的文件雙擊執(zhí)行即可。
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USERSoftwareMicrosoftInternet ExplorerMainFeatureControlFEATURE_HTTP_USERNAME_PASSWORD_DISABLE]
“iexplore.exe”=dword:00000000
下面給出相關(guān)的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() {
// 點擊左側(cè)的無線參數(shù)導航鏈接
selenium.click("//a[text()='無線參數(shù)']");
// 切換模式
selenium.select("//select[@name='mode']", "label=11Mbps (802.11b)");
}
}