集成C#测试框架和Selenium对于Web系统实现自动化测试

来源:互联网 发布:苹果电脑流程图软件 编辑:程序博客网 时间:2024/05/16 13:49

系统环境:
软件需求: Visual C# 2010,Mozilla Firefox,Selenium
硬件需求: Pentium III 450以上的CPU处理器,64MB以上的内存,200MB的自由硬盘空间
内容简介:
1. 利用Spy++进行Windows对象识别,查找Windows计算器中的3类不同的对象,列出这些对象的常规属性。
2. 利用Selenium测试脚本录制以下操作:
(1) 在Firefox地址栏中输入淘宝网主页网址http://www.taobao.com/,回车载入网页;
(2) 点击“搜索”旁边的“店铺”;
(3) 在搜索栏中输入“考拉”,点击“搜索”按钮;
(4) 鼠标右键点击链接“考拉一家”,在弹出菜单中点击“verifyElementPresent link=考拉一家”;
(5) 点击“考拉一家”左边的图片,进入该网页。
(6) 结束脚本录制。
3. 利用C#测试框架和NUNIT测试框架,实现自动化测试,测试和回放实验内容第2项中的脚本。

实施过程
(1) 用selenium录制相应脚本,要将所有selectwindows选项的target选项设为null,脚本运行成功后作为C#语言导出
源代码如下

using System;using System.Text;using System.Text.RegularExpressions;using System.Threading;using NUnit.Framework;using Selenium;namespace SeleniumTests{[TestFixture]public class 77777{private ISelenium selenium;private StringBuilder verificationErrors;[SetUp]public void SetupTest(){selenium = new DefaultSelenium("localhost", 4444, "*chrome", "https://www.taobao.com/");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 The77777Test(){            selenium.Open("/");            selenium.Click("css=li.J_SearchTab.shop-search-tab");            selenium.Click("id=q");            selenium.Type("id=q", "考拉");            selenium.Click("css=button.btn-search");            selenium.WaitForPageToLoad("30000");            selenium.SelectWindow("null");            try            {                Assert.IsTrue(selenium.IsElementPresent("link=考拉一家"));            }            catch (AssertionException e)            {                verificationErrors.Append(e.Message);            }            selenium.SelectWindow("null");            selenium.Click("css=img");}}}

(2) 新建VS中C#测试项目,在类中导入ThoughtWorks.Selenium.Core.dll,添加相应头文件后将代码修改如下

using System;using Microsoft.VisualStudio.TestTools.UnitTesting;using System.Text;using System.Collections.Generic;using System.Text.RegularExpressions;using System.Threading;using Selenium;using System.Linq;namespace UnitTestProject1{    [TestClass]    public class UnitTest1    {        [TestMethod]        public void TestMethod1()        {            ISelenium selenium = new DefaultSelenium("localhost", 4444, "*chrome", "https://www.taobao.com/");            selenium.Start();            //verificationErrors = new StringBuilder();            selenium.Open("/");            selenium.Click("css=li.J_SearchTab.shop-search-tab");            selenium.Click("id=q");            selenium.Type("id=q", "考拉");            selenium.Click("css=button.btn-search");            selenium.WaitForPageToLoad("30000");            selenium.SelectWindow("null");            Assert.IsTrue(selenium.IsElementPresent("link=考拉一家"));            selenium.SelectWindow("null");            Thread.Sleep(5000);            selenium.Click("css=img");            Thread.Sleep(5000);            selenium.Close();            selenium.Stop();        }    }}

(3) 打开 cmd 进入 selenium-server-1.0-beta-2目录,输入“java -jar selenium-server.jar”,启动 Selenium测试服务器
在测试项目中运行测试。
(4) 对于NUNIT的测试框架,在VS中新建C#控制台程序项目,导入nunit.framework和ThoughtWorks.Selenium.Core的类库文件,然后在主函数外添加三种C#脚本代码中的方法,并在主函数中调用。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Selenium;using NUnit.Framework;using System.Threading;namespace ConsoleApplication2{    //[TestFixture]    public class SeleniumTest    {        class Program        {            static void Main(string[] args)            {                Program p = new Program();                p.SetupTest();                p.TheSeleniumTest();                Thread.Sleep(500);                p.TeardownTest();            }            private ISelenium selenium;            private StringBuilder verificationErrors;            //[SetUp]            public void SetupTest()            {                selenium = new DefaultSelenium("localhost", 4444, "*chrome", "https://www.taobao.com/");                selenium.Start();                verificationErrors = new StringBuilder();            }            //[TearDown]            public void TeardownTest()            {                try                {                    Thread.Sleep(500);                    selenium.Stop();                }                catch (Exception)                {                    // Ignore errors if unable to close the browser                }                Assert.AreEqual("", verificationErrors.ToString());            }            //[Test]            public void TheSeleniumTest()            {                selenium.Open("/");                selenium.Click("css=li.J_SearchTab.shop-search-tab");                selenium.Type("id=q", "考拉");                selenium.Click("css=button.btn-search");                selenium.WaitForPageToLoad("30000");                selenium.SelectWindow("null");                try                {                    Assert.IsTrue(selenium.IsElementPresent("link=考拉一家"));                }                catch (AssertionException e)                {                    verificationErrors.Append(e.Message);                }                selenium.SelectWindow("null");                selenium.Click("link=考拉一家");            }        }    }}

(5)同理先启动服务器,然后将项目启动,完成测试

总结:自动化测试是单元测试、集成乃至系统测试的实际操作方式中运用很广泛的一种,因为现实中因为测试用例量太大,测试工作繁琐或者系统规模过大不得不开发出自动化测试的理论和技术。在测试过程中,最困难的步骤是脚本转化为代码再结合到测试项目中,对于VS测试框架,需要用脚本代码中代表核心操作的部分添加到测试类中,并注意不要引入NUNIT框架;对于NUNIT测试项目,需要多导入一个nunit.framework.dll引入框架,测试时启动项目而非运行测试。本测试过程中需要注意头文件的引入正确,启动服务器时端口不能被占用,而且相关的jar包必须是版本符合要求,不然服务器会报异常。整个系统的逻辑是将测试脚本嵌入测试代码中,通过selenium sever服务器连接脚本、浏览器和selenium插件,实现脚本在框架中接受测试并通过浏览器运行。

0 0