使用Selenium Server測試無線路由器Web管理頁面
來自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_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_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_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_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("http://a[text()='無線參數']"); // 切換模式 selenium.select("http://select[@name='mode']", "label=11Mbps (802.11b)"); } } |
本文轉載自:http://loggingselenium.com/?p=335