selenium rc : rc是remote control的縮寫,這種方式是使用具體的語言來編寫測試 類,然后進(jìn)行測試,它的功能就是用來模擬一個瀏覽器,主要測試的就是web方面的東西。它支持的語言非常多,C#,Java都行,看網(wǎng)上好多例子都是Java的。這個東西好像一個庫文件一樣,自己編程調(diào)用的。
Selenium RC就是使用程式語言編寫腳本,通過Selenium RC服務(wù)器作為代理服務(wù)器去訪問應(yīng)用從而達(dá)到測試的目的.由于Selenium RC不再需要依附Firefox,所以其可以在其它更多的瀏覽器上進(jìn)行測試,而這也是我們做WEB測試的一個比較重要的問題(解決了因為擔(dān)心瀏覽器兼容問題而重要做測試的問題). RC的腳本由于是程序語言編寫,所以更為靈活強大.并且它支持的語言極為豐富.所以RC是Selenium測試工具中的應(yīng)用最廣的.同時,它對測試人員編程水平要求也較高.
Selenium 的版本
Selenium 現(xiàn)在存在2個版本,一個叫 selenium-core, 一個叫selenium-rc 。
selenium-core 是使用HTML的方式來編寫測試腳本,你也可以使用 Selenium-IDE來錄制腳本,但是目前Selenium-IDE
只有 FireFox 版本。
Selenium-RC 是 selenium-remote control 縮寫,是使用具體的語言來編寫測試類。
selenium-rc 支持的語言非常多,這里我們著重關(guān)注java的方式。這里講的也主要是 selenium-rc,因為個人還是喜歡這種
方式 :-)
windows下安裝selenium-RC
1.安裝
解壓后,打開cmd。
Selenium Server 是用Java語言編寫的,需要在JRE 1.5.0或者更高的版本下運行。
檢查是否安裝了JRE,操作如下: 在命令行中執(zhí)行:
java –version
可以看到如下的關(guān)于你安裝的java的版本信息:
C:\Documents and Settings\Administrator>java -version
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03)
Java HotSpot(TM) Client VM (build 14.1-b02, mixed mode, sharing)
相反,如果看到的是錯誤信息,則需要安裝JRE,或者是將它填加到PATH環(huán)境變量中去。
selenium-remote-control-1.0.3.rar解壓出來selenium-remote-control-1.0.3
把里面的:selenium-server-1.0.3
把他放在:
開始-運行cmd,切換到selenium-server.jar所在的目錄下 執(zhí)行命令:java -jar selenium-server.jar 啟動成功。
selenium安裝完成。
selenium-rc 一些使用方法
在 selenium-remote-control-0.9.0/server 目錄里,我們運行 java -jar selenium-server.jar
之后你就會看到一些啟動信息。要使用 selenium-rc ,啟動這個server 是必須的。
當(dāng)然,啟動的時候有許多參數(shù),這些用法可以在網(wǎng)站里看看教程,不過不加參數(shù)也已經(jīng)足夠了。
selenium server 啟動完畢了,那么我們就可以開始編寫測試類了!
我們先有個概念,selenium 是模仿瀏覽器的行為的,當(dāng)你運行測試類的時候,你就會發(fā)現(xiàn)selenium 會打開一個
瀏覽器,然后瀏覽器執(zhí)行你的操作。
using System;using System.Text;using NUnit.Framework;using Selenium;namespace SeleniumTests { [TestFixture] public class Untitled { private ISelenium selenium; private StringBuilder verificationErrors; [SetUp] public void SetupTest() { selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://localhost:16447/News.aspx"); selenium.Start(); verificationErrors = new StringBuilder(); } [TearDown] public void TeardownTest() { try { selenium.Stop(); } catch (Exception) { // Ignore errors if unable to close the browser } Assert.AreEqual("", verificationErrors.ToString()); } [Test] public void TheUntitledTest() { selenium.Open("/News.aspx"); try { Assert.IsTrue(selenium.IsTextPresent("悼模友“王自武”不飛")); } catch (AssertionException e) { verificationErrors.Append(e.Message); } selenium.Click("link=悼模友“王自武”不飛"); } } }
代碼十分簡單,作用就是初始化一個 Selenium 對象。其中:
url : 就是你要測試的網(wǎng)站
localhost: 可以不是localhost,但是必須是 selenium server 啟動的地址
*iexplore 或者*chrome (IE或者火狐瀏覽器): 可以是其它瀏覽器類型,可以在網(wǎng)站上看都支持哪些。
下面我就要講講怎么使用selenium 這個對象來進(jìn)行測試。
1、測試文本輸入框
假設(shè)頁面上有一個文本輸入框,我們要測試的內(nèi)容是 在其中輸入一些內(nèi)容,然后點擊一個按鈕,看看頁面的是否跳轉(zhuǎn)
到需要的頁面。
[Test] public void CnblogTest() { selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://passport.cnblogs.com/register.aspx"); selenium.Start(); selenium.Open("http://passport.cnblogs.com/register.aspx"); selenium.Type("xpath=//input[@id='ctl00_holderLeft_txt_userName']","dupeng0811"); //selenium.WaitForPageToLoad("2000"); Assert.AreEqual(selenium.GetValue("xpath=//input[@id='ctl00_holderLeft_txt_userName']"), "dupeng0812"); selenium.Stop(); }
代碼解釋:
1、調(diào)用 selenium.open 方法,瀏覽器會打開相應(yīng)的頁面
2、使用 type 方法來給輸入框輸入文字
3、等待頁面載入-selenium.WaitForPageToLoad("2000");
4、看看頁面中的文本框中填入的是不是我們輸入的內(nèi)容呢?
將Assert.AreEqual(selenium.GetValue("xpath=//input[@id='ctl00_holderLeft_txt_userName']"), "dupeng0812");
更改后
2、測試下拉框
[Test] public void SelectTest() { selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.webkey.cn/demo/docs/index2.asp?url=/demo/docs/menuselect/"); selenium.Start(); selenium.Open("http://www.webkey.cn/demo/docs/index2.asp?url=/demo/docs/menuselect/"); selenium.Select("xpath=//select[@id='city']", "index=2"); Assert.AreEqual(selenium.GetSelectedIndex("xpath=//select[@id='city']"), "2"); }
代碼注釋:
1、使用selenium.Select("xpath=//select[@id='city']", "index=2"); 來尋找頁面中的下拉框。
2、使用selenium.GetSelectedIndex("xpath=//select[@id='city']"), "2")來獲取下拉框的內(nèi)容。
可以看到,我們可以使用 select 方法來確定選擇下拉框中的哪個選項。
3、測試check box
[Test] public void CheckBoxTest() { selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://passport.cnblogs.com/register.aspx"); selenium.Start(); selenium.Open("http://passport.cnblogs.com/register.aspx"); selenium.Check("xpath=//input[@id='ctl00_holderLeft_cblPosition_2']"); }
代碼注釋:
1、使用selenium.Check來尋找checkBox
2、xpath下還是使用=//input
4、判斷頁面是否存在一個元素
[Test] public void isExistElementTest() { selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://passport.cnblogs.com/register.aspx"); selenium.Start(); selenium.Open("http://passport.cnblogs.com/register.aspx"); Assert.AreEqual(selenium.IsElementPresent("xpath=//input[@id='ctl00_holderLeft_cblPosition_2']"),true); }
代碼注釋:
1、使用selenium.IsElementPresent來判斷是否存在該元素。
selenium 還有更多的用法,例如彈出頁面等等。當(dāng)面對沒見過的測試要求時,我最笨的方法就是按照api文檔一個一個找,
好在不多,肯定能找到。
啟動Selenium測試服務(wù)器
打開cmd進(jìn)入selenium-server-1.0-beta-2目錄,輸入“java -jar selenium-server.jar”(需要先安裝JRE),啟動Selenium測試服務(wù)器。
運行測試案例
(1).運行測試案例:
(2).測試結(jié)果:
恩,案例Pass了,如果案例失敗的話,Error Meesage會說明失敗的原因。
(注意:和Firefox一樣,IE下也有屏蔽彈出網(wǎng)頁功能,修改設(shè)置方法:MenuBar->Tools->Popup Blocker->Turn off Popup Blocker,或者在Popup Blocker Settings里面配置。)