VS2010中使用 SpecFlow + Selenium.WebDriver
2016-05-29 00:20
344 查看
安装(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-CNand | * ,而且,并且,同时 |
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版本兼容性对照表
Selenium | Firefox |
2.53.1 | 47.0.1 |
参考文献
[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/
相关文章推荐
- 内部类的一些事
- 自定义控件知识储备-View的绘制流程
- 通过session_id限制仅一个用户登录
- 【LeetCode】226. Invert Binary Tree 解题报告
- LUA处理xml
- AI人工智能系列随笔:syntaxnet 初探(1)
- MySQL-tpch 测试工具简要手册
- 划分树
- bzoj2957 楼房重建
- Android MVP架构
- 计算偏度Skewness与峰度kurtosis的python程序——简单
- 数据库常见的面试题
- 《您的设计模式》(CBF4LIFE)之“多例模式”【整理】
- 版本管理工具:linux下svn的基本使用
- UrlRewrite实现url伪静态
- scala之trait详解二:选择性混入
- Python2与Python3共存的环境配置
- 基于Netty与RabbitMQ的消息服务
- 《您的设计模式》(CBF4LIFE)之“单例模式”【整理】
- bzoj2738 矩阵乘法