您的位置:首页 > 其它

Vs2008采用Selenium进行Web 功能测试

2009-07-28 14:47 274 查看
1.采用selenium录制脚本。

录制的脚本如下:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using Selenium;

namespace SeleniumTests
{
	[TestFixture]
	public class Untitled 2
	{
		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 TheUntitled 2Test()
		{
			selenium.Open("/");
			selenium.Click("link=新 闻");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("link=网页");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("link=贴 吧");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("link=MP3");
			selenium.Click("link=MP3");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("//center");
			selenium.Click("link=图 片");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("link=视 频");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("link=网 页");
			selenium.WaitForPageToLoad("30000");
		}
	}
}




在Vs2008中新建一个C#测试项目

添加引用



当然,实际可能用不了这么多dll,但是刚开始还是都加上。

把Selenium IDE生成的C#程序拷贝到这个测试程序里,然后做下修改。

添加using Microsoft.VisualStudio.TestTools.UnitTesting;

注释掉//using NUnit.Framework;

[TestFixture] -->[TestClass]

[SetUp]--->[TestInitialize]

[TearDown]--> [TestCleanup]

[Test]--->[TestMethod]

具体代码如下:

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
//using NUnit.Framework;
using Selenium;

namespace SeleniumTests
{
	//[TestFixture]
    [TestClass]
	public class Untitled
	{
		private ISelenium selenium;
		private StringBuilder verificationErrors;
		
		//[SetUp]
        [TestInitialize]
		public void SetupTest()
		{
			//selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://change-this-to-the-site-you-are-testing/");
            selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.baidu.com");
			selenium.Start();
			verificationErrors = new StringBuilder();
		}
		
		//[TearDown]
        [TestCleanup]
		public void TeardownTest()
		{
			try
			{
				selenium.Stop();
			}
			catch (Exception)
			{
				// Ignore errors if unable to close the browser
			}
			Assert.AreEqual("", verificationErrors.ToString());
		}
       // [Test]
		[TestMethod]
		public void TheUntitledTest()
		{
			selenium.Open("/");
			selenium.Click("link=新 闻");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("link=网页");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("link=贴 吧");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("link=MP3");
			selenium.Click("link=MP3");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("//center");
			selenium.Click("link=图 片");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("link=视 频");
			selenium.WaitForPageToLoad("30000");
			selenium.Click("link=网 页");
			selenium.WaitForPageToLoad("30000");
		}
	}
}


CTRL+F5运行。Ok.

关于FireFox测试的时候配置问题可以参考网站。特别是Firefox通过设置代理上网的时候,Selenium启动FireFox访问外网的时候是采用FireFox安装的默认配置,需要自己设置一个给FireFox用的配置文件。

http://girliemangalo.wordpress.com/2009/02/05/creating-firefox-profile-for-your-selenium-rc-tests/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: