第一个Selenium RC测试案例

上一篇 / 下一篇  2013-01-09 18:27:15

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

2011-09-24 21:53:19| 分类:Selenium| 标签:|字号订阅

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

Selenium简介》中讲过,Selenium RC支持多种语言编写测试案例,如:C#,Python。在工作中,我倾向于是用Python这类动态语言编写测试案例,因为这样的测试案例无需编译:>,试想如果你有1000个测试案例,每个都要编译,那会给编译服务器很大的压力,而且案例修改后,还得重新编译才能运行:<。但在本系列的文章中,我还是打算使用C#编写示范例子。

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

Selenium私房菜系列5 -- 第一个Selenium RC测试案例 - swl632 - 我的博客

写Selenium RC的测试案例

上一篇《Selenium IDE的使用》中,提到了Selenium IDE可以把录制的脚本转为其他语言的脚本,所以我继续用上一篇的脚本为例子,下面是把脚本语言转换为C#后的代码:

usingSystem;
usingSystem.Text;
usingSystem.Text.RegularExpressions;
usingSystem.Threading;
usingNUnit.Framework;
usingSelenium;

namespaceSeleniumTests
{
[TestFixture]
publicclassNewTest
{
privateISelenium selenium;
privateStringBuilder verificationErrors;

[SetUp]
publicvoidSetupTest()
{
selenium
=newDefaultSelenium("localhost",4444,"*chrome","http://change-this-to-the-site-you-are-testing/");
selenium.Start();
verificationErrors
=newStringBuilder();
}

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

[Test]
publicvoidTheNewTest()
{
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

Selenium私房菜系列5 -- 第一个Selenium RC测试案例 - swl632 - 我的博客

2.导入DLL引用

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

Selenium私房菜系列5 -- 第一个Selenium RC测试案例 - swl632 - 我的博客

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

usingSystem;
usingSystem.Text;
usingSystem.Collections.Generic;
usingMicrosoft.VisualStudio.TestTools.UnitTesting;
usingSelenium;

namespaceSeleniumTest
{
[TestClass]
publicclassUnitTest1
{
[TestMethod]
publicvoidTest()
{
//127.0.0.1为Selenium测试服务器位置。
//4444为Selenium测试服务器监听端口。
//*iexplore为启动浏览器类型,我把它改为了IE浏览器。
//http://www.baidu.com为源地址
ISelenium selenium=newDefaultSelenium("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测试服务器。
Selenium私房菜系列5 -- 第一个Selenium RC测试案例 - swl632 - 我的博客

5.运行测试案例
(1).运行测试案例:
Selenium私房菜系列5 -- 第一个Selenium RC测试案例 - swl632 - 我的博客

(2).测试结果:

Selenium私房菜系列5 -- 第一个Selenium RC测试案例 - swl632 - 我的博客
恩,案例Pass了,如果案例失败的话,Error Meesage会说明失败的原因。
(注意:和Firefox一样,IE下也有屏蔽弹出网页功能,修改设置方法:MenuBar->Tools->Popup Blocker->Turn off Popup Blocker,或者在Popup Blocker Settings里面配置。)


TAG:

 

评分:0

我来说两句

Open Toolbar