Selenium私房菜系列5-- 第一個Selenium RC測試案例

http://www.cnblogs.com/hyddd/archive/2009/05/24/1488377.html

    《Selenium簡介》 中講過,Selenium RC支持多種語言編寫測試案例,如:C#,Python。在工作中,我傾向於是用Python這類動態語言編寫測試案例,因爲這樣的測試案例無需編 譯:>,試想如果你有1000個測試案例,每個都要編譯,那會給編譯服務器很大的壓力,而且案例修改後,還得重新編譯才能運行:<。但在本系 列的文章中,我還是打算使用C#編寫示範例子。

Selenium RC下載:http://seleniumhq.org/download/

zz:  Selenium私房菜系列5 -- 第一個Selenium RC測試案例 - life wave - Life wave

寫Selenium RC的測試案例

     上一篇《Selenium IDE的使用》中,提到了Selenium IDE可以把錄製的腳本轉爲其他語言的腳本,所以我繼續用上一篇的腳本爲例子,下面是把腳本語言轉換爲C#後的代碼:

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");
        }
    }
}

在這裏,轉換後的腳本使用了NUnit測試框架,爲了簡化,我用VS的Test Project代替(當然你也可以用Console Application建立測試工程的),步驟如下:
1.建立Test Project

zz:  Selenium私房菜系列5 -- 第一個Selenium RC測試案例 - life wave - Life wave

2.導入DLL引用

    把selenium-dotnet-client-driver-1.0-beta-2目錄中的ThoughtWorks.Selenium.Core.dllThoughtWorks.Selenium.IntegrationTests.dllThoughtWorks.Selenium.UnitTests.dll加入項目:

zz:  Selenium私房菜系列5 -- 第一個Selenium RC測試案例 - life wave - Life wave

3.把上面自動生成的代碼再改一下

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測試服務器位置。
            
//4444爲Selenium測試服務器監聽端口。
            
//*iexplore爲啓動瀏覽器類型,我把它改爲了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();
        }
    }
}

4.啓動Selenium測試服務器
    打開cmd進入selenium-server-1.0-beta-2目錄,輸入“java -jar selenium-server.jar”
(需要先安裝JRE),啓動Selenium測試服務器。
zz:  Selenium私房菜系列5 -- 第一個Selenium RC測試案例 - life wave - Life wave

5.運行測試案例
(1).運行測試案例:
zz:  Selenium私房菜系列5 -- 第一個Selenium RC測試案例 - life wave - Life wave

(2).測試結果:

zz:  Selenium私房菜系列5 -- 第一個Selenium RC測試案例 - life wave - Life wave
恩,案例Pass了,如果案例失敗的話,Error Meesage會說明失敗的原因。
(注 意:和Firefox一樣,IE下也有屏蔽彈出網頁功能,修改設置方法:MenuBar->Tools->Popup Blocker->Turn off Popup Blocker,或者在Popup Blocker Settings裏面配置。)


 評論這張
轉發至微博

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