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]
    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

2.导入DLL引用

把selenium-dotnet-client-driver-1.0-beta-2目录中的ThoughtWorks.Selenium.Core.dll,ThoughtWorks.Selenium.IntegrationTests.dll,ThoughtWorks.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]
        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测试服务器。

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

(2).测试结果:


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

Selenium私房菜系列5 -- 第一个Selenium RC测试案例的更多相关文章

  1. Selenium私房菜系列7 -- 玩转Selenium Server

    本篇主要是想更进一步介绍Selenium Server的工作原理,这次我们从Selenium Server的交互模式开始. 在<第一个Selenium RC测试案例>中,我们以命令“jav ...

  2. Selenium私房菜系列6 -- 深入了解Selenium RC工作原理(1)

    前一篇已经比较详细讲述了如何使用Selenium RC进行Web测试,但到底Selenium RC是什么?或者它由哪几部分组成呢?? 一.Selenium RC的组成: 关于这个问题,我拿了官网上的一 ...

  3. Selenium私房菜系列--总章

    前言 在这段期间,我一直在找关于服务器的端测试方案,自动化工具等等,无意间我发现了Selenium这个工具.在试用一段时间后,觉得Selenium确实是一个很不错的Web测试工具.在和强大的QTP比较 ...

  4. Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

    Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

  5. Selenium私房菜系列9 -- 我遇到的问题及解决问题的方法

    Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法

  6. Selenium私房菜系列4 -- Selenium IDE的使用

    (转自http://www.cnblogs.com/hyddd/archive/2009/05/24/1487967.html) 前面说过,Selenium IDE是Firefox的一个插件,是可以进 ...

  7. Selenium私房菜系列1 -- Selenium简介

    一.Selenium是什么? Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案 ...

  8. Selenium私房菜系列3 -- Selenium API参考手册【ZZ】

    大家在编写测试案例想查阅Selenium API说明时,可以查阅本文,否则请跳过! (注:这里API版本比较老,新版本的Selenium的API在这里不一定能找到.) Selenium API说明文档 ...

  9. Selenium私房菜系列9 -- Selenium RC服务器命令行参数列表【ZZ】

    本文转载自:http://wiki.javascud.org/display/SEL/Selenium+Remote+Control+-+options 使用示例: java -jar seleniu ...

随机推荐

  1. Android开发---开发文档翻译

    2014.11.24 1:ClipData类:用于表示剪切的数据,此剪切的数据可以是复杂类型,包括一个或多个条目实例 (1)基础知识 >公共类:public class >嵌套类:Clip ...

  2. HDOJ-1251

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  3. Flutter实战视频-移动电商-10.首页_FlutterSwiper轮播效果制作

    10.首页_FlutterSwiper轮播效果制作 博客地址: https://jspang.com/post/FlutterShop.html#toc-5c2 flutter_swiper http ...

  4. UVa 242 Stamps and Envelope Size (无限背包,DP)

    题意:信封上最多贴S张邮票.有N个邮票集合,每个集合有不同的面值.问哪个集合的最大连续邮资最 大,输出最大连续邮资和集合元素. 最大连续邮资是用S张以内邮票面值凑1,2,3...到n+1凑不出来了,最 ...

  5. Git 移除某些文件

    一.前言 在使用 Git 版本控制中,有些文件是不需要加入到版本控制中的.如 日志( log ).编译的文件.这些随时都在变的文件,使用用一个代码库的用户.只要稍稍修改一点,或者启动一下,就会变.容易 ...

  6. OpenGL Geometry Shader

    http://blog.csdn.net/bugrunner/article/details/5455324 Geometry Shader可以处理Vertex Shader和Fragment Sha ...

  7. C - How Many Tables

    #include <cstdio> #include <cstdlib> #include <iostream> #include <algorithm> ...

  8. Vue文件 引入.js文件 的组件

    Vue.component('remote-script', { render: function (createElement) { var self = this; return createEl ...

  9. python快排

    代码: def partition(data,left,right): tmp = data[left] while left<right: while left < right and ...

  10. python3错误之TypeError: 'dict_items' object is not callable

    这种错误出现在循环结构中套循环结构,而循环时内部循环为字典,外部循环为该字典调用items方法后取到的值,内部循环调用外部循环中遍历的结果: 解决方案: 将外部循环的items()方法调用改为.key ...