圖 5. 通過(guò)確認是否包含某些文字來(lái)驗證是否成功登錄
使用 Selenium IDE 導出 JUnit 測試用例
在 Selenium IDE 中執行成功后,就可以把測試腳本導出成 JUnit 測試用例了,如圖 6 所示:
圖 6. 導出 JUnit 代碼
導出用例如下:
清單 1. VerifyLogin.java
import com.thoughtworks.selenium.*; import org.junit.After; import org.junit.Before; import org.junit.Test; public class VerifyLogin extends SeleneseTestCase { @Before public void setUp() throws Exception { selenium = new DefaultSelenium ("localhost", 4444, "*chrome", "localhost:8422/"); selenium.start(); } @Test public void testVerifyDirectorLogin() throws Exception { selenium.setTimeout("300000"); selenium.open("/ibm/console/logon.jsp"); selenium.type("id=j_username", "test"); selenium.type("id=j_password", "test"); selenium.click("id=other"); selenium.waitForPageToLoad("300000"); verifyTrue(selenium.isTextPresent("IBM Systems Director")); } @After public void tearDown() throws Exception { selenium.stop(); } } |
說(shuō)明:首先實(shí)例化一個(gè) DefaultSelenium 對象,傳入的參數分別是 RC 服務(wù)器 IP、端口、瀏覽器類(lèi)型和待測試的 Server 的信息。然后在測試方法里調用 Selenium 1 的 API,這里的代碼完全由 IDE 生成,就為我們省去了很多重復代碼工作。
運行測試用例:
有了基于 JUnit 的運行測試用例就可以把它導入到 Java IDE 中執行測試了。執行中既需要客戶(hù)端驅動(dòng)支持(用于 Eclipse 編譯),也需要啟動(dòng) RC Server:
Selenium RC Server 下載:http://seleniumhq.org/download/
Selenium Client Driver:http://seleniumhq.org/download/
執行命令 java –jar selenium-server-standalone-2.5.0.jar 啟動(dòng) Selenium RC Server:
圖 7. 啟動(dòng) Selenium RC Server
啟動(dòng)后就可以直接在 Eclipse 中運行測試用例,RC Server 就會(huì )啟動(dòng)新窗口并自動(dòng)按照錄制腳本進(jìn)行測試。并可在 Eclipse 中查看運行結果。
下面讓我們看看怎樣脫離 Eclipse 自己搭建一個(gè)可以持續測試的容器。
原文轉自:http://www.uml.org.cn/Test/201707182.asp