• 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.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]
            
    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里面配置。)


  • 相关阅读:
    SQL处理重复数据
    ASP.NET下载网站上的地图图片(精)
    js应用大全
    vs2003运行过程中出现的一些错误汇总
    Google卫片下载(转)
    JS获取网页中HTML元素的几种方法
    8位QQ大放送
    .net 读取Xml文件并进行DropDownList数据绑定
    C# Stream 和 byte[] 之间的转换
    【C#4.0】中的dynamic与var的区别
  • 原文地址:https://www.cnblogs.com/hyddd/p/1488377.html
Copyright © 2020-2023  润新知