Selenium私房菜系列5 -- 第一个Selenium RC测试案例

来源:互联网 发布:oppor9s还原网络设置 编辑:程序博客网 时间:2024/05/12 01:41
《Selenium简介》中讲过,Selenium RC支持多种语言编写测试案例,如:C#,Python。在工作中,我倾向于是用Python这类动态语言编写测试案例,因为这样的测试案例无需编译:>,试想如果你有1000个测试案例,每个都要编译,那会给编译服务器很大的压力,而且案例修改后,还得重新编译才能运行:<。但在本系列的文章中,我还是打算使用C#编写示范例子。

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

写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]
publicclass NewTest
{
private ISelenium selenium;
private StringBuilder verificationErrors;

[SetUp]
publicvoid SetupTest()
{
selenium
=new DefaultSelenium("localhost",4444,"*chrome","http://change-this-to-the-site-you-are-testing/");
selenium.Start();
verificationErrors
=new StringBuilder();
}

[TearDown]
publicvoid TeardownTest()
{
try
{
selenium.Stop();
}
catch (Exception)
{
// Ignore errors if unable to close the browser
}
Assert.AreEqual(
"", verificationErrors.ToString());
}

[Test]
publicvoid 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

2.导入DLL引用

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

3.把上面自动生成的代码再改一下

using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Selenium;

namespace SeleniumTest
{
    [TestClass]
   
public class UnitTest1
    {
        [TestMethod]
       
publicvoid 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测试服务器。

5.运行测试案例
(1).运行测试案例:

(2).测试结果:


恩,案例Pass了,如果案例失败的话,Error Meesage会说明失败的原因。
(注意:和Firefox一样,IE下也有屏蔽弹出网页功能,修改设置方法:MenuBar->Tools->Popup Blocker->Turn off Popup Blocker,或者在Popup Blocker Settings里面配置。)


作者:hyddd
出处:http://www.cnblogs.com/hyddd/
本文版权归作者所有,欢迎转载,演绎或用于商业目的,但是必须说明本文出处(包含链接)。