使用 Selenium 實現基于 Web 的自動化測試(3)

發表于:2017-07-24來源:未知作者:jib點擊數: 標簽:selenium
圖 5. 通過確認是否包含某些文字來驗證是否成功登錄 使用 Selenium IDE 導出 JUnit 測試用例 在 Selenium IDE 中執行成功后,就可以把測試腳本導出成 JUnit 測試用

圖 5. 通過確認是否包含某些文字來驗證是否成功登錄

使用 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(); 

}

說明:首先實例化一個 DefaultSelenium 對象,傳入的參數分別是 RC 服務器 IP、端口、瀏覽器類型和待測試的 Server 的信息。然后在測試方法里調用 Selenium 1 的 API,這里的代碼完全由 IDE 生成,就為我們省去了很多重復代碼工作。

運行測試用例:

有了基于 JUnit 的運行測試用例就可以把它導入到 Java IDE 中執行測試了。執行中既需要客戶端驅動支持(用于 Eclipse 編譯),也需要啟動 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 啟動 Selenium RC Server:

圖 7. 啟動 Selenium RC Server

啟動后就可以直接在 Eclipse 中運行測試用例,RC Server 就會啟動新窗口并自動按照錄制腳本進行測試。并可在 Eclipse 中查看運行結果。

下面讓我們看看怎樣脫離 Eclipse 自己搭建一個可以持續測試的容器。

原文轉自:http://www.uml.org.cn/Test/201707182.asp

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97