您的位置:首页 > 其它

VS2010中使用 SpecFlow + Selenium.WebDriver

2016-05-29 00:20 274 查看

安装(VS扩展、程序包)

【工具】->【扩展管理器】,安装SpecFlow

【工具】->【库程序包管理】->【程序包管理器控制台】

PM> Install-Package SpecFlow -Version 1.9.0
PM> Install-Package NUnit
PM> Install-Package Selenium.WebDriver
PM> Install-Package Should


使用中文

# language: zh-CN

and* ,而且,并且,同时
background背景
but* ,但是
examples例子
feature功能
given* ,假如,假设,假定
scenario场景,剧本
scenarioOutline场景大纲,剧本大纲
then* ,那么  
when* ,当

我想开始新游戏

作为破译者/我想开始新游戏.feature



作为破译者/我想开始新游戏Steps.cs

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using Should;
using TechTalk.SpecFlow;

namespace CodeBreakerGame.Specs.作为破译者
{
[Binding]
public class 我想开始新游戏Steps
{
private IWebDriver driver = new FirefoxDriver();

[Given(@"游戏还没有开始")]
public void 假如游戏还没有开始()
{
driver.Navigate().GoToUrl("http://localhost:1387/Game/Index");
}

[When(@"我开始新游戏")]
public void 当我开始新游戏()
{
driver.FindElement(By.TagName("button")).Click();
}

[Then(@"我应该看到""(.*)""")]
public void 那么我应该看到(string message)
{
driver.FindElement(By.TagName("div")).Text.ShouldContain(message);
}

[AfterScenario]
public void AfterScenario()
{
driver.Quit();
}
}
}


我想提交猜测的密码

作为破译者/我想提交猜测的密码.feature



作为破译者/我想提交猜测的密码Steps.feature

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using Should;
using TechTalk.SpecFlow;

namespace CodeBreakerGame.Specs.作为破译者
{
[Binding]
public class 我想提交猜测的密码Steps
{
private IWebDriver driver = new FirefoxDriver();

[Given(@"真实密码是""(.*)""")]
public void 假如真实密码是(int code)
{
driver.Navigate().GoToUrl("http://localhost:1387/Game/Guess/" + code.ToString());
}

[When(@"我猜""(.*)""")]
public void 当我猜(int guess)
{
driver.FindElement(By.Id("Guess")).SendKeys(guess.ToString());
driver.FindElement(By.TagName("button")).Click();
}

[Then(@"标记为""(.*)""")]
public void 那么标记为(string mark)
{
driver.FindElement(By.TagName("strong")).Text.ShouldEqual(mark);
}

[AfterScenario]
public void AfterScenario()
{
driver.Quit();
}
}
}


Action

[HttpPost]
public ActionResult Guess(string code, FormCollection collection)
{
var guess = collection["Guess"];

var mark = "";
for (int i = 0; i < code.Length; i++)
{
if (code[i] == guess[i])
mark += "+";
}

for (int i = 0; i < code.Length; i++)
{
for (int j = 0; j < code.Length; j++)
{
if (i != j && code[i] == guess[j])
mark += "-";
}
}

return View(new[] { guess, mark });
}


测试结果



源代码

CodeBreakerGame.rar

说明:由于受文件大小的限制,压缩包里删除了文章开头提到的4个库程序包

Selenium和Firefox版本兼容性对照表

SeleniumFirefox
2.53.147.0.1
说明:需要注意Selenium.WebDriver和Firefox的版本,如果不兼容可能导致测试运行不了

参考文献

[1] http://www.specflow.org/

[2] https://github.com/cucumber/gherkin/blob/master/gherkin-languages.json

[3] https://github.com/SeleniumHQ/selenium


[4] http://seleniumhq.github.io/selenium/docs/api/dotnet/

[5] http://www.specflow.org/documentation/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: