Selenium私房菜系列5 -- 第一个Selenium RC测试案例
《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测试案例的更多相关文章
- Selenium私房菜系列7 -- 玩转Selenium Server
本篇主要是想更进一步介绍Selenium Server的工作原理,这次我们从Selenium Server的交互模式开始. 在<第一个Selenium RC测试案例>中,我们以命令“jav ...
- Selenium私房菜系列6 -- 深入了解Selenium RC工作原理(1)
前一篇已经比较详细讲述了如何使用Selenium RC进行Web测试,但到底Selenium RC是什么?或者它由哪几部分组成呢?? 一.Selenium RC的组成: 关于这个问题,我拿了官网上的一 ...
- Selenium私房菜系列--总章
前言 在这段期间,我一直在找关于服务器的端测试方案,自动化工具等等,无意间我发现了Selenium这个工具.在试用一段时间后,觉得Selenium确实是一个很不错的Web测试工具.在和强大的QTP比较 ...
- Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法
Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法
- Selenium私房菜系列9 -- 我遇到的问题及解决问题的方法
Selenium私房菜系列10 -- 我遇到的问题及解决问题的方法
- Selenium私房菜系列4 -- Selenium IDE的使用
(转自http://www.cnblogs.com/hyddd/archive/2009/05/24/1487967.html) 前面说过,Selenium IDE是Firefox的一个插件,是可以进 ...
- Selenium私房菜系列1 -- Selenium简介
一.Selenium是什么? Selenium是ThroughtWorks公司一个强大的开源Web功能测试工具系列,本系列现在主要包括以下4款: 1.Selenium Core:支持DHTML的测试案 ...
- Selenium私房菜系列3 -- Selenium API参考手册【ZZ】
大家在编写测试案例想查阅Selenium API说明时,可以查阅本文,否则请跳过! (注:这里API版本比较老,新版本的Selenium的API在这里不一定能找到.) Selenium API说明文档 ...
- Selenium私房菜系列9 -- Selenium RC服务器命令行参数列表【ZZ】
本文转载自:http://wiki.javascud.org/display/SEL/Selenium+Remote+Control+-+options 使用示例: java -jar seleniu ...
随机推荐
- MongoDB Oplog
Capped Collections MongoDB有一种特殊的Collection叫Capped collections,它的插入速度非常快,基本和磁盘的写入速度差不多,并且支持按照插入顺序高效的查 ...
- HttpPost 传输Json数据并解析
转自:https://blog.csdn.net/qq_35114086/article/details/52317311 这里写个测试用例模拟外部调用,通过httppost 传递一个json封装的表 ...
- java:calendar类及一些比较实用的utils(一)
在java编程中经常会用到时间日期的计算.比较.格式化等等操作,刚开始接触Calendar类时,还是在初学习期间,小小白一枚,看着这个好复杂,懒惰心理作祟也就没有怎么去学习,后来在项目中经常用到,索性 ...
- Flutter实战视频-移动电商-41.详细页_数据接口的调试
41.详细页_数据接口的调试 建立数据模型层,我们的业务逻辑分开,然后进行后台数据的调试 生成model类 json数据: { ", "message": "s ...
- 洛谷 - P4997 - 不围棋 - 并查集 - 模拟
https://www.luogu.org/problemnew/show/P4997 首先是改变气的定义,使得容易计算,这个很好理解. 然后使用并查集,因为要维护整个连通块的性质. 最后的难点在于, ...
- art-template在项目中的应用
art-template 是一个简约.超快的模板引擎.它采用作用域预声明的技术来优化模板渲染速度,从而获得接近 JavaScript 极限的运行性能,并且同时支持 NodeJS 和浏览器. 下面介绍在 ...
- Forward Rendering 正向渲染
Forward Rendering 正向渲染 正向渲染一个基于着色器的渲染路径.它支持逐像素计算光照(包括法线贴图和灯光Cookies)和来自一个平行光的实时阴影.在默认设置中,少数最亮 ...
- 阿里巴巴开源性能监控神器Arthas初体验
如果问性能测试中最难的是哪部分,相信很多人会说“性能调优”.确实是这样,性能调优是一个非常复杂.技术含量很高的工作.涉及到的知识面很广.以我多年从业经验来看,在企业里,大多数的性能调优都是由开发架构师 ...
- Android NFC P2P
http://www.nfc.cc/2011/12/28/development-android-beam-and-nfc-peer-2-peer/
- JDBC连接池一 自定义连接池
package com.mozq.jdbc; import java.io.IOException; import java.io.InputStream; import java.sql.Conne ...