安装(VS扩展、程序包)

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

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

  1. PM> Install-Package SpecFlow -Version 1.9.
  2. 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

  1. using OpenQA.Selenium;
  2. using OpenQA.Selenium.Firefox;
  3. using Should;
  4. using TechTalk.SpecFlow;
  5.  
  6. namespace CodeBreakerGame.Specs.作为破译者
  7. {
  8. [Binding]
  9. public class 我想开始新游戏Steps
  10. {
  11. private IWebDriver driver = new FirefoxDriver();
  12.  
  13. [Given(@"游戏还没有开始")]
  14. public void 假如游戏还没有开始()
  15. {
  16. driver.Navigate().GoToUrl("http://localhost:1387/Game/Index");
  17. }
  18.  
  19. [When(@"我开始新游戏")]
  20. public void 当我开始新游戏()
  21. {
  22. driver.FindElement(By.TagName("button")).Click();
  23. }
  24.  
  25. [Then(@"我应该看到""(.*)""")]
  26. public void 那么我应该看到(string message)
  27. {
  28. driver.FindElement(By.TagName("div")).Text.ShouldContain(message);
  29. }
  30.  
  31. [AfterScenario]
  32. public void AfterScenario()
  33. {
  34. driver.Quit();
  35. }
  36. }
  37. }

我想提交猜测的密码

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

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

  1. using System;
  2. using OpenQA.Selenium;
  3. using OpenQA.Selenium.Firefox;
  4. using Should;
  5. using TechTalk.SpecFlow;
  6.  
  7. namespace CodeBreakerGame.Specs.作为破译者
  8. {
  9. [Binding]
  10. public class 我想提交猜测的密码Steps
  11. {
  12. private IWebDriver driver = new FirefoxDriver();
  13.  
  14. [Given(@"真实密码是""(.*)""")]
  15. public void 假如真实密码是(int code)
  16. {
  17. driver.Navigate().GoToUrl("http://localhost:1387/Game/Guess/" + code.ToString());
  18. }
  19.  
  20. [When(@"我猜""(.*)""")]
  21. public void 当我猜(int guess)
  22. {
  23. driver.FindElement(By.Id("Guess")).SendKeys(guess.ToString());
  24. driver.FindElement(By.TagName("button")).Click();
  25. }
  26.  
  27. [Then(@"标记为""(.*)""")]
  28. public void 那么标记为(string mark)
  29. {
  30. driver.FindElement(By.TagName("strong")).Text.ShouldEqual(mark);
  31. }
  32.  
  33. [AfterScenario]
  34. public void AfterScenario()
  35. {
  36. driver.Quit();
  37. }
  38. }
  39. }

Action

  1. [HttpPost]
  2. public ActionResult Guess(string code, FormCollection collection)
  3. {
  4. var guess = collection["Guess"];
  5.  
  6. var mark = "";
  7. for (int i = ; i < code.Length; i++)
  8. {
  9. if (code[i] == guess[i])
  10. mark += "+";
  11. }
  12.  
  13. for (int i = ; i < code.Length; i++)
  14. {
  15. for (int j = ; j < code.Length; j++)
  16. {
  17. if (i != j && code[i] == guess[j])
  18. mark += "-";
  19. }
  20. }
  21.  
  22. return View(new[] { guess, mark });
  23. }

测试结果

源代码

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. js 三元表达式 复杂写法

    a = 0 b = 0 a === 0 && (a = 1,b = 2) a === 1 ? (a = 3,alert(b)) : (b = 4) a === 1 || alert(a ...

  2. Loopback接口的作用

    Loopback接口是虚拟接口,是一种纯软件性质的虚拟接口.任何送到该接口的网络数据报文都会被认为是送往设备自身的.大多数平台都支持使用这种接口来模拟真正的接口.这样做的好处是虚拟接口不会像物理接口那 ...

  3. zabbix通过snmp监控网络设备

    首先需要在zabbix的server端或proxy端安装snmpd服务 安装: yum -y install net-snmp* 查看版本: [root@Check3 ~]# snmpd -v NET ...

  4. 带SSL证书的httpclient 远程接口工具类

    package com.iups.wx.util; import java.io.IOException; import java.io.UnsupportedEncodingException; i ...

  5. Contest-hunter 暑假送温暖 SRM08

    01-07都没写...然后突然来写貌似有点突兀啊...不管了,难得前排记录一下... 吐槽一下赛制...不得不说很强... cf 套oi...很创新...不过还是兹磁ACM或者CF A-1 数据才2& ...

  6. NumPy矩阵库

    NumPy - 矩阵库 NumPy 包包含一个 Matrix库numpy.matlib.此模块的函数返回矩阵而不是返回ndarray对象. matlib.empty() matlib.empty()函 ...

  7. 运行jar_测试代码

    1.Eclipse 将 工程 导出成 jar Eclipse --> Export... --> 界面中树状图形中选择" Java下的'JAR file' "(不知道这 ...

  8. JNI_Z_07_方法的操作(没有String类型的参数)_参数的传递方式

    1. 2.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_TjniMethod02.h" JNIEXPORT void ...

  9. mysql create database and user 新建数据库并为其创建专用账号

    DROP DATABASE `wordpress`;------------------------------------------------------------------ CREATE ...

  10. 将hibernate.cfg.xml文件都放到spring中时报错

    报错如下所示: 私以为是配置文件出现问题了. <?xml version="1.0" encoding="UTF-8"?> <beans xm ...