VS2010中使用 SpecFlow + Selenium.WebDriver
安装(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 });
}
测试结果

源代码
说明:由于受文件大小的限制,压缩包里删除了文章开头提到的4个库程序包
Selenium和Firefox版本兼容性对照表
| Selenium | Firefox |
| 2.53.1 | 47.0.1 |
说明:需要注意Selenium.WebDriver和Firefox的版本,如果不兼容可能导致测试运行不了
参考文献
[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的更多相关文章
- Selenium_用selenium webdriver实现selenium RC中的类似的方法
最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法.目前封装了一个ActionDrive ...
- Selenium WebDriver中一些鼠标和键盘事件的使用
转自:http://www.ithov.com/linux/133271.shtml 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击 ...
- Java中通过Selenium WebDriver定位iframe中的元素
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 问题:有一些元素,无论是通过id或是xpath等等,怎么都定位不到. 分析:这很可能是因为你要定位 ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展(转)
本文将总结 Selenium WebDriver 中的一些鼠标和键盘事件的使用,以及组合键的使用,并且将介绍 WebDriver 中没有实现的键盘事件(Keys 枚举中没有列举的按键)的扩展.举例说明 ...
- 转:总结Selenium WebDriver中一些鼠标和键盘事件的使用
在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展[转载]
原文:http://www.ibm.com/developerworks/cn/java/j-lo-keyboard/ 概念 在使用 Selenium WebDriver 做自动化测试的时候,会经常模 ...
- 总结Selenium WebDriver中一些鼠标和键盘事件的使用
在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...
- selenium.webdriver.common.keys 模块中常用的变量
表11-5 selenium.webdriver.common.keys 模块中常用的变量属性 含义Keys.DOWN, Keys.UP, Keys.LEFT,Keys.RIGHT 键盘箭头键Keys ...
- Selenium WebDriver 中鼠标和键盘事件分析及扩展
[From] http://www.51testing.com/html/18/631118-861557.html 在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和 ...
随机推荐
- HIVE大数据出现倾斜怎么办
hive在跑数据时经常会出现数据倾斜的情况,使的作业经常reduce完成在99%后一直卡住,最后的1%花了几个小时都没跑完,通过YARN的管理界面配合日志,可以清楚其中的具体原因,这种情况就很可能 ...
- day1 python调用模块,密码加密
import getpass #加密密码 username = input("username:") password = getpass.getpass("passwo ...
- 【c++ primer, 5e】访问控制与封装
练习 7.16 无,类的接口定义在public说明符之后,类的实现细节定义在private说明符之后. 7.17 有.类成员的默认访问权限不同.class的类成员默认为private,struct的则 ...
- Zabbix Windos mysql 监控脚本
说明:判断mysql主进程是否关闭,如果关闭则返回0 创建文件:MySQL-ping.vbs Set objFS = CreateObject("Scripting.FileSystemOb ...
- 20145201《Java程序设计》第九周学习总结
20145201 <Java程序设计>第九周学习总结 教材学习内容总结 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操作,开发人员无须接触底层 ...
- Linux下挂载windows的共享文件夹
环境说明: 由于领导要求:现需要将某Linux服务器下的一个文件移动到某windows服务器下(服务器均在机房托管,要远程操作) 由于操作为一次性,则决定在windows下建立一个共享文件夹,linu ...
- sqlmap的简要使用方法
仅作为备忘!!! sqlmap使用: 注入类型有四种分别为:boolean-based blind.error-based.stacked queries.inline query. sqlmap - ...
- xcode9打包问题
xcode9打包找不到icon http://blog.csdn.net/yohunl/article/details/78065656 换用xcode8打包 https://www.cnblogs. ...
- git常用的语句
下面总结出开发中常用的指令: 1.git init:初始化git仓库 2.git add 文件名:把文件添加到暂存区中 3.git commit -m "提交信息":提交暂存区内容 ...
- Spring Cloud 微服务开放平台接口
github源码地址:https://github.com/spring-cloud/spring-cloud-security 前言: 什么是开放平台接口 场景 : 总公司与子公司 对接接口 还有 ...