selenium rc : rc是remote control的縮寫(xiě),這種方式是使用具體的語(yǔ)言來(lái)編寫(xiě)測(cè)試 類(lèi),然后進(jìn)行測(cè)試,它的功能就是用來(lái)模擬一個(gè)瀏覽器,主要測(cè)試的就是web方面的東西。它支持的語(yǔ)言非常多,C#,Java都行,看網(wǎng)上好多例子都是Java的。這個(gè)東西好像一個(gè)庫(kù)文件一樣,自己編程調(diào)用的。
Selenium RC就是使用程式語(yǔ)言編寫(xiě)腳本,通過(guò)Selenium RC服務(wù)器作為代理服務(wù)器去訪問(wèn)應(yīng)用從而達(dá)到測(cè)試的目的.由于Selenium RC不再需要依附Firefox,所以其可以在其它更多的瀏覽器上進(jìn)行測(cè)試,而這也是我們做WEB測(cè)試的一個(gè)比較重要的問(wèn)題(解決了因?yàn)閾?dān)心瀏覽器兼容問(wèn)題而重要做測(cè)試的問(wèn)題). RC的腳本由于是程序語(yǔ)言編寫(xiě),所以更為靈活強(qiáng)大.并且它支持的語(yǔ)言極為豐富.所以RC是Selenium測(cè)試工具中的應(yīng)用最廣的.同時(shí),它對(duì)測(cè)試人員編程水平要求也較高.
Selenium 的版本
Selenium 現(xiàn)在存在2個(gè)版本,一個(gè)叫 selenium-core, 一個(gè)叫selenium-rc 。
selenium-core 是使用HTML的方式來(lái)編寫(xiě)測(cè)試腳本,你也可以使用 Selenium-IDE來(lái)錄制腳本,但是目前Selenium-IDE
只有 FireFox 版本。
Selenium-RC 是 selenium-remote control 縮寫(xiě),是使用具體的語(yǔ)言來(lái)編寫(xiě)測(cè)試類(lèi)。
selenium-rc 支持的語(yǔ)言非常多,這里我們著重關(guān)注java的方式。這里講的也主要是 selenium-rc,因?yàn)閭(gè)人還是喜歡這種
方式 :-)
windows下安裝selenium-RC
1.安裝
解壓后,打開(kāi)cmd。
Selenium Server 是用Java語(yǔ)言編寫(xiě)的,需要在JRE 1.5.0或者更高的版本下運(yùn)行。
檢查是否安裝了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)
相反,如果看到的是錯(cuò)誤信息,則需要安裝JRE,或者是將它填加到PATH環(huán)境變量中去。
selenium-remote-control-1.0.3.rar解壓出來(lái)selenium-remote-control-1.0.3
把里面的:selenium-server-1.0.3
把他放在:
開(kāi)始-運(yùn)行cmd,切換到selenium-server.jar所在的目錄下 執(zhí)行命令:java -jar selenium-server.jar 啟動(dòng)成功。
selenium安裝完成。
selenium-rc 一些使用方法
在 selenium-remote-control-0.9.0/server 目錄里,我們運(yùn)行 java -jar selenium-server.jar
之后你就會(huì)看到一些啟動(dòng)信息。要使用 selenium-rc ,啟動(dòng)這個(gè)server 是必須的。
當(dāng)然,啟動(dòng)的時(shí)候有許多參數(shù),這些用法可以在網(wǎng)站里看看教程,不過(guò)不加參數(shù)也已經(jīng)足夠了。
selenium server 啟動(dòng)完畢了,那么我們就可以開(kāi)始編寫(xiě)測(cè)試類(lèi)了!
我們先有個(gè)概念,selenium 是模仿瀏覽器的行為的,當(dāng)你運(yùn)行測(cè)試類(lèi)的時(shí)候,你就會(huì)發(fā)現(xiàn)selenium 會(huì)打開(kāi)一個(gè)
瀏覽器,然后瀏覽器執(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=悼模友“王自武”不飛"); } } }
代碼十分簡(jiǎn)單,作用就是初始化一個(gè) Selenium 對(duì)象。其中:
url : 就是你要測(cè)試的網(wǎng)站
localhost: 可以不是localhost,但是必須是 selenium server 啟動(dòng)的地址
*iexplore 或者*chrome (IE或者火狐瀏覽器): 可以是其它瀏覽器類(lèi)型,可以在網(wǎng)站上看都支持哪些。
下面我就要講講怎么使用selenium 這個(gè)對(duì)象來(lái)進(jìn)行測(cè)試。
1、測(cè)試文本輸入框
假設(shè)頁(yè)面上有一個(gè)文本輸入框,我們要測(cè)試的內(nèi)容是 在其中輸入一些內(nèi)容,然后點(diǎn)擊一個(gè)按鈕,看看頁(yè)面的是否跳轉(zhuǎn)
到需要的頁(yè)面。
[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(); }
代碼解釋?zhuān)?nbsp;
1、調(diào)用 selenium.open 方法,瀏覽器會(huì)打開(kāi)相應(yīng)的頁(yè)面
2、使用 type 方法來(lái)給輸入框輸入文字
3、等待頁(yè)面載入-selenium.WaitForPageToLoad("2000");
4、看看頁(yè)面中的文本框中填入的是不是我們輸入的內(nèi)容呢?
將Assert.AreEqual(selenium.GetValue("xpath=//input[@id='ctl00_holderLeft_txt_userName']"), "dupeng0812");
更改后
2、測(cè)試下拉框
[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"); }
代碼注釋?zhuān)?/p>
1、使用selenium.Select("xpath=//select[@id='city']", "index=2"); 來(lái)尋找頁(yè)面中的下拉框。
2、使用selenium.GetSelectedIndex("xpath=//select[@id='city']"), "2")來(lái)獲取下拉框的內(nèi)容。
可以看到,我們可以使用 select 方法來(lái)確定選擇下拉框中的哪個(gè)選項(xiàng)。
3、測(cè)試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']"); }
代碼注釋?zhuān)?/p>
1、使用selenium.Check來(lái)尋找checkBox
2、xpath下還是使用=//input
4、判斷頁(yè)面是否存在一個(gè)元素
[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); }
代碼注釋?zhuān)?/p>
1、使用selenium.IsElementPresent來(lái)判斷是否存在該元素。
selenium 還有更多的用法,例如彈出頁(yè)面等等。當(dāng)面對(duì)沒(méi)見(jiàn)過(guò)的測(cè)試要求時(shí),我最笨的方法就是按照api文檔一個(gè)一個(gè)找,
好在不多,肯定能找到。
啟動(dòng)Selenium測(cè)試服務(wù)器
打開(kāi)cmd進(jìn)入selenium-server-1.0-beta-2目錄,輸入“java -jar selenium-server.jar”(需要先安裝JRE),啟動(dòng)Selenium測(cè)試服務(wù)器。
運(yùn)行測(cè)試案例
(1).運(yùn)行測(cè)試案例:
(2).測(cè)試結(jié)果:
恩,案例Pass了,如果案例失敗的話,Error Meesage會(huì)說(shuō)明失敗的原因。
(注意:和Firefox一樣,IE下也有屏蔽彈出網(wǎng)頁(yè)功能,修改設(shè)置方法:MenuBar->Tools->Popup Blocker->Turn off Popup Blocker,或者在Popup Blocker Settings里面配置。)