robotframework + selenium實現網站自動測試

jopen 9年前發布 | 37K 次閱讀 測試工具 Selenium

本文參考:How to use RobotFramework with the Selenium Library

先來介紹下背景。Selenium是個強大的自動化測試工具,可以手工編寫或自動錄制測試腳本,可以手工執行,也可以自動執行。而robotframework是個通用的自動化測試框架。 robotframework-seleniumlibrary是robotframework的一個測試庫,它可以集成Selenium,并提供一套簡單易懂的語法用于書寫測試用例(好處就是測試人員不用學任何編程語言也能寫測試用例了)。

用這套工具能實現:

  • 無需學習編程語言即可編寫測試腳本
  • 自動點擊網站鏈接、輸入文字、點擊按鈕、驗證結果
  • 可以放在cron里每日自動執行
  • 自動生成測試報告
  • 對于fail的測試用例,保存輸出結果并截圖

下面是配置方法。操作系統用的是Ubuntu 12.04。

首先裝個瀏覽器。

$ sudo apt-get install firefox

然后要安裝robotframework和robotframework-seleniumlibrary。

$ sudo pip install robotframework
$ sudo pip install robotframework-seleniumlibrary

接下來要安裝JRE以運行Selenium Server。

$ sudo apt-get install default-jre

然后去下載Selenium Server,文件名為selenium-server-standalone-2.25.0.jar。

如果你的服務器上沒有圖形界面,那么還要安裝個X-server和窗口管理器(我選擇了xfce4)。我原本打算使用Windows下的WeirdX作為X-Server, 但Selenium報告說它不支持自動截圖,只好裝了個xorg。

$ sudo apt-get install xorg
$ sudo apt-get install xfce4

這樣環境就都配置好了。接下來我們寫個小測試用例,保存成google.txt。它僅打開Google首頁并檢查里面有沒有'Google'字樣。 (測試腳本參考SeleniumLibrary Documentation。)

*** Settings ***
Library  SeleniumLibrary  5  localhost

*** Test Cases ***
Google Homepage Should Contain Google
    Open Browser  http://www.google.com/  *firefox
    Page Should Contain  Google

那么如何運行測試呢?首先要啟動X-server。如果你用ssh,那么務必回到控制臺上(ssh不行)去啟動X-server。

$ startx

啟動后如果xfce4沒有自動啟動,可以再執行如下命令:

$ startxfce4

然后去shell里(可以是ssh的shell)執行如下命令以啟動Selenium Server:

$ export DISPLAY=localhost:0.0
$ java -jar selenium-server-standalone-2.25.0.jar

最后,換另一個shell執行測試:

$ pybot google.txt
==============================================================================
Google
==============================================================================
Google Homepage Should Contain Google                                 | PASS |
------------------------------------------------------------------------------
Google                                                                | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================
Output:  /home/charlee/code/cmstest/test/output.xml
Log:     /home/charlee/code/cmstest/test/log.html
Report:  /home/charlee/code/cmstest/test/report.html

測試結束后可以打開report.html查看測試結果。

來自:http://blog.charlee.li/website-auto-test-with-robotframework-selenium/

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!