安装(VS扩展、程序包)

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

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

PM> Install-Package SpecFlow -Version 1.9.
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 = ; i < code.Length; i++)
{
if (code[i] == guess[i])
mark += "+";
} for (int i = ; i < code.Length; i++)
{
for (int j = ; 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

说明:需要注意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/

VS2010中使用 SpecFlow + Selenium.WebDriver的更多相关文章

  1. Selenium_用selenium webdriver实现selenium RC中的类似的方法

    最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...

  2. Selenium WebDriver中一些鼠标和键盘事件的使用

    转自:http://www.ithov.com/linux/133271.shtml 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击 ...

  3. Java中通过Selenium WebDriver定位iframe中的元素

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 问题:有一些元素,无论是通过id或是xpath等等,怎么都定位不到. 分析:这很可能是因为你要定位 ...

  4. Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)

    本文将总结 Selenium WebDriver 中的一些鼠标和键盘事件的使用,以及组合键的使用,并且将介绍 WebDriver 中没有实现的键盘事件(Keys 枚举中没有列举的按键)的扩展.举例说明 ...

  5. 转:总结Selenium WebDriver中一些鼠标和键盘事件的使用

    在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...

  6. Selenium WebDriver 中鼠标和键盘事件分析及扩展[转载]

    原文:http://www.ibm.com/developerworks/cn/java/j-lo-keyboard/ 概念 在使用 Selenium WebDriver 做自动化测试的时候,会经常模 ...

  7. 总结Selenium WebDriver中一些鼠标和键盘事件的使用

    在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...

  8. selenium.webdriver.common.keys 模块中常用的变量

    表11-5 selenium.webdriver.common.keys 模块中常用的变量属性 含义Keys.DOWN, Keys.UP, Keys.LEFT,Keys.RIGHT 键盘箭头键Keys ...

  9. Selenium WebDriver 中鼠标和键盘事件分析及扩展

    [From] http://www.51testing.com/html/18/631118-861557.html 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和 ...

随机推荐

  1. SQL server 数据库 操作及简单查询

    使用SQL Sever语言进行数据库的操作 常用关键字identity 自增长primary key 主键unique 唯一键not null 非空references 外键(引用) 在使用查询操作数 ...

  2. Mysql字段属性应该尽量设置为not null

    除非你有一个很特别的原因去使用 NULL 值,你应该总是让你的字段保持 NOT NULL.这看起来好像有点争议,请往下看. 所谓的NULL就是什么都没有,连\0都没有,\0在字符串中是结束符,但是在物 ...

  3. 【Flask】WTForms基本使用

    # WTForms笔记:这个库一般有两个作用.第一个就是做表单验证,把用户提交上来的数据进行验证是否合法.第二个就是做模版渲染. ### 做表单验证:1. 自定义一个表单类,继承自wtforms.Fo ...

  4. 手动漏洞挖掘-SQL注入(安全牛笔记)

    substring_index(USER(),"@",l)-- #是将查询出来的结果进行切分,以@符号的方式切分 ’ union select table_name,table_s ...

  5. 一键配置IP地址脚本

    #/bin/bash NETPWD='/etc/sysconfig/network-scripts/' read -p "please enten net num(1,2,3,4) : &q ...

  6. fabric文件上传打包与校验

  7. BZOJ:(270,300]

    9/30 BZOJ3038:线段树,不带lazy标记,直接修改叶子. BZOJ3211:同3038 BZOJ1406:将式子转换成[(x-1)*(x+1)%n==0]然后枚举i.当i=x-1时,i*( ...

  8. Coldfusion8 读取HASH工具

    #!/usr/bin/env python #-*- coding:utf- -*- import sys import threading import urllib import httplib ...

  9. Search In Rotated SortedArray2, 有重复数据的反转序列。例如13111.

    问题描述:反转序列,但是有重复的元素,例如序列13111. 算法思路:如果元素有重复,那么left-mid,就不一定是有序的了,所以不能利用二分搜索,二分搜索必须是局部有序.针对有序序列的反转,如果有 ...

  10. h5 audio播放音频文件

    h5 audio播放音频文件 注:下面html中样式及不相关的内容去掉了 第一个例子 播放没有防盗链的外网音频文件是可以的 <!doctype html> <html> < ...