selenium_第一個腳本_登錄新浪微博

假定各位以前看過前兩篇文章,已經搭建好了環境,現在開始錄製回放第一個selenium的腳本:登錄新浪微博

 

 

基本的步驟是:

1.  打開firefox,設置seleniumIDE選項,使之可以錄製下junit的代碼;

2.  使用seleniumIDE進行錄製,生成代碼;

3.  設置斷言;

4.  打開eclipse,創建工程,新建包,包下面創建類,貼入代碼;

5.  工程引用相關的庫文件;

6.  調試:一步步運行回放

 

 

第一步:

打開firefox,設置seleniumIDE選項,使之可以錄製下junit的代碼;

 

先在firefox的工具菜單中打開seleniumIDE,然後在seleniumIDE1.4.1窗口中,打開菜單<options-options…>,按照下圖設置:

 

001.JPG

 

 

設置好該項後,就可以錄製出junit的腳本,否則缺省錄製出selenium腳本,selenium腳本可以在seleniumIDE中回放,而junit的腳本就需要導入ecplise中來執行了。

 

002.JPG

 

 

 

 

第二步:使用seleniumIDE進行錄製,生成代碼;

 

seleniumIDE點擊錄製,在firefox中訪問網址,進行操作,操作完成後,頁面暫時不要關閉,seleniumIDE停止錄製,這時可以看到生成的junit代碼,記得我們的版本是junit4呀。

 

package com.example.tests;

 

import com.thoughtworks.selenium.*;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import java.util.regex.Pattern;

 

public class Untitled extends SeleneseTestCase {

         @Before

         public void setUp() throws Exception {

                   selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://weibo.com/login.php");

                   selenium.start();

         }

 

         @Test

         public void testUntitled() throws Exception {

                   selenium.open("/login.php");

                   selenium.click("id=loginname");

                   selenium.type("id=loginname", "[email protected]");

                   selenium.type("id=password", "xxx");

                   selenium.click("css=#login_submit_btn > img");

         }

 

         @After

         public void tearDown() throws Exception {

                   selenium.stop();

         }

}

 

 

 

第三步:設置斷言

 

斷言是代碼級別的稱謂,對於測試就是校驗點,假定以是否出現發微薄的文本框爲是否成功登錄的校驗點

 

 

打開firebug:

 

004.JPG

 

005.JPG

 

 

先點擊firebug的<點擊查看頁面中的元素>,將鼠標移動到文本框控件上面,會出現浮動的藍色框,點擊鼠標左鍵,如下圖,在文本上右鍵鼠標,選擇複製XPath,得到該文本框的唯一引用

 

006.JPG

 

在點擊了登錄按鈕後,新增加一句:

assertTrue(selenium.isElementPresent("xpath=/html/body/div/div[2]/div/div/div/div/div/div[4]/div/div/textarea"));

 

斷言是什麼?要不先翻翻手冊吧。

 

 

 

第四步:打開eclipse,創建工程,新建包,包下面創建類,貼入代碼;

 

創建工程:File—New—Projects…--java projects—項目名稱:prjSelenium1—Finish

創建包:如下圖選擇上新建的項目-- File—New—Package—包名:pkgTest

 

007.JPG:

類似上圖,選擇上包-- File—New—Class—類名:ClsTest—finish

 

把之前的代碼貼入ClsTest.java,修改下面標粗的內容:

package pkgTest;

 

import com.thoughtworks.selenium.*;

import org.junit.After;

import org.junit.Before;

import org.junit.Test;

import java.util.regex.Pattern;

 

public class ClsTest extends SeleneseTestCase {

    @Before

    public void setUp() throws Exception {

       selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://weibo.com/login.php");

       selenium.start();

    }

 

    @Test

    public void testUntitled() throws Exception {

       selenium.open("/login.php");

       selenium.click("id=loginname");

       selenium.type("id=loginname", "[email protected]");

       selenium.type("id=password", "xxx");

       selenium.click("css=#login_submit_btn > img");

      

        assertTrue(selenium.isElementPresent("xpath=/html/body/div/div[2]/div/div/div/div/div/div[4]/div/div/textarea"));

    }

 

    @After

    public void tearDown() throws Exception {

       selenium.stop();

    }

}

 

 

將包名和類名改的和新建的一致

 

 

第五步:工程引用相關的庫文件;

 

可以看到現在的代碼全是紅槓槓,那是因爲找不到相應的庫。

 

選擇項目名,右鍵選擇屬性,按照下圖設置:

 

008.JPG

 

此處的庫文件包括了前文下載的<Selenium Client Drivers,SeleniumRC,JDK1.6>相關的jar包。

三類jar包分別是模擬客戶端,模擬服務器端(細節請看Selenium私房菜(新手入門教程).pdf),虛擬機。

 

很快代碼的紅槓槓就沒了,有些變成了黃槓槓,都是建議的,可以不用管他。

 

 

 

第六步:調試:一步步運行回放

 

首先需要啓動seleniumRC,我自己創建了一個批處理,大家可以參考:

F:

cd F:\TOOL\java\prjSelenium\seleniumRC

java -jar selenium-server-standalone-2[1].12.0.jar

 

服務啓動完成後(啓動後啥樣子,前一篇文章已經講過),在代碼上設置一個斷點,光標停留在需要中斷的語句處,按下ctrl+shift+b,由於旁邊有黃色標記擋住了,有點看不出來是否設置成功了,仔細看看,會發現有那麼幾個象素點的變化的。我把斷點設置在了< selenium.start();>。現在可以清理一下環境了,關閉firefox和seleniumIDE(當然你喜歡亂點也無所謂),選擇ClsTest.java,右鍵選擇Debug As—Junit Test,代碼就開始運行了,停在了斷點處,後續就什麼F5,F6,F7(到時候看看ecplise的Run菜單)啥的往後一步步執行了。

 

 

好了,看看最後效果如何:

 

009.JPG

 

 

 

 

期間可能會遇到的問題:

 

問題1:

java.lang.RuntimeException: Could not contact Selenium Server; have you started it on 'localhost:4444' ?

Read more at http://seleniumhq.org/projects/remote-control/not-started.html

Connection refused: connect

 

解決方法:運行前需要先啓動seleniumRC,編寫批處理:

F:

cd F:\TOOL\java\prjSelenium\seleniumRC

java -jar selenium-server-standalone-2[1].12.0.jar

 

問題2:

java.lang.NoClassDefFoundError: com/google/common/base/Charsets

 

解決方法:添加:selenium-server-standalone-2[1].12.0.jar

 

問題3:

java.lang.RuntimeException: Could not start Selenium session: Failed to start new browser session: java.lang.RuntimeException: Firefox 3 could not be found in the path!

Please add the directory containing ''firefox.exe'' to your PATH environment

variable, or explicitly specify a path to Firefox 3 like this:

*firefox3 c:\blah\firefox.exe

 

解決方法:

我的電腦-屬性-高級-環境變量-系統變量-PATH

PATH=$PATH;D:\Program Files\Mozilla Firefox\

需要重新啓動一次eclipse

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章