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. Linux 无法登陆172.***.***.***的子网

    1. sudo dhclient -r 这条命令重复执行几次 2. dhclient - 3.查看ifconfig

  2. git搭建私有仓库

    git gui参考 https://ask.helplib.com/git/post_1004941

  3. c++拷贝函数详解(转)

    一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如 int a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变 ...

  4. 【eclipse插件开发实战】Eclipse插件开发1——eclipse内核结构、扩展点机制

    Eclipse插件开发实战1--eclipse内核结构.扩展点机制 一.前言 本系列总体介绍eclipse插件开发基本理论.插件项目结构及开发步骤,最后再给出两个插件开发实例. 总体安排结构如下: 1 ...

  5. POJ - 3190 Stall Reservations 贪心+自定义优先级的优先队列(求含不重叠子序列的多个序列最小值问题)

    Stall Reservations Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one w ...

  6. AttributeCollection.Add(String, String) Method

    <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Trans ...

  7. python-os.walk()使用举例

    文件目录结构 dir 1 1 1.txt 2.txt 3.txt 2 2.txt 3 4 4.txt 3.txt 1.txt 2 2.txt 3 3.txt dir.txt 代码: import os ...

  8. 参考手册(html css)

    HTML CSS DOM XML DOM   XML DOM Document JavaScript JavaScript RegExp JSP

  9. CodeForces 41A+43A【课上无聊刷水题系列】

    41Acode 好像只要前一个字符串存在下一个字符串的头单词就YES: #include <bits/stdc++.h> using namespace std; typedef __in ...

  10. 《C#入门经典》学习笔记(集合、比较和转换)

    http://xiang-ai-2002.blog.163.com/blog/static/8477933201041824429161/ 集合 C#中的数组是作为System.Array类的实例来执 ...