第一個(gè)Selenium RC測(cè)試案例
《Selenium簡(jiǎn)介》中講過(guò),Selenium RC支持多種語(yǔ)言編寫測(cè)試案例,如:C#,Python。在工作中,我傾向于是用Python這類動(dòng)態(tài)語(yǔ)言編寫測(cè)試案例,因?yàn)檫@樣的測(cè)試案例無(wú)需編譯:>,試想如果你有1000個(gè)測(cè)試案例,每個(gè)都要編譯,那會(huì)給編譯服務(wù)器很大的壓力,而且案例修改后,還得重新編譯才能運(yùn)行:<。但在本系列的文章中,我還是打算使用C#編寫示范例子。
Selenium RC下載:http://seleniumhq.org/download/
	
寫Selenium RC的測(cè)試案例
	     上一篇《Selenium IDE的使用》中,提到了Selenium IDE可以把錄制的腳本轉(zhuǎn)為其他語(yǔ)言的腳本,所以我繼續(xù)用上一篇的腳本為例子,下面是把腳本語(yǔ)言轉(zhuǎn)換為C#后的代碼:
	復(fù)制代碼
	using System;
	using System.Text;
	using System.Text.RegularExpressions;
	using System.Threading;
	using NUnit.Framework;
	using Selenium;
	namespace SeleniumTests
	{
	    [TestFixture]
	    public class NewTest
	    {
	        private ISelenium selenium;
	        private StringBuilder verificationErrors;
	       
	        [SetUp]
	        public void SetupTest()
	        {
	            selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://change-this-to-the-site-you-are-testing/");
	            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 TheNewTest()
	        {
	            selenium.Open("/");
	            selenium.Type("kw", "hyddd");
	            selenium.Click("sb");
	            selenium.WaitForPageToLoad("30000");
	            try
	            {
	                Assert.IsTrue(selenium.IsTextPresent("hyddd - 博客園"));
	            }
	            catch (AssertionException e)
	            {
	                verificationErrors.Append(e.Message);
	            }
	            selenium.Click("//table[@id='1']/tbody/tr/td/a/font");
	        }
	    }
	}
	復(fù)制代碼
	在這里,轉(zhuǎn)換后的腳本使用了NUnit測(cè)試框架,為了簡(jiǎn)化,我用VS的Test Project代替(當(dāng)然你也可以用Console Application建立測(cè)試工程的),步驟如下:
	1.建立Test Project
	 
2.導(dǎo)入DLL引用
把selenium-dotnet-client-driver-1.0-beta-2目錄中的ThoughtWorks.Selenium.Core.dll,ThoughtWorks.Selenium.IntegrationTests.dll,ThoughtWorks.Selenium.UnitTests.dll加入項(xiàng)目:
	
	3.把上面自動(dòng)生成的代碼再改一下
	復(fù)制代碼
	using System;
	using System.Text;
	using System.Collections.Generic;
	using Microsoft.VisualStudio.TestTools.UnitTesting;
	using Selenium;
	namespace SeleniumTest
	{
	    [TestClass]
	    public class UnitTest1
	    {
	        [TestMethod]
	        public void Test()
	        {
	            //127.0.0.1為Selenium測(cè)試服務(wù)器位置。
	            //4444為Selenium測(cè)試服務(wù)器監(jiān)聽(tīng)端口。
	            //*iexplore為啟動(dòng)瀏覽器類型,我把它改為了IE瀏覽器。
	            //http://www.baidu.com為源地址。
	            ISelenium selenium = new DefaultSelenium("127.0.0.1", 4444, "*iexplore", "http://www.baidu.com");
	            selenium.Start();
	            selenium.Open("/");
	            selenium.Type("kw", "hyddd");
	            selenium.Click("sb");
	            selenium.WaitForPageToLoad("30000");
	            Assert.IsTrue(selenium.IsTextPresent("hyddd - 博客園"));
	            selenium.Click("//table[@id='1']/tbody/tr/td/a/font");
	      selenium.Close();
	            selenium.Stop();
	        }
	    }
	}
	復(fù)制代碼
	4.啟動(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ù)器。
	
	5.運(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里面配置。)