VS2010中使用 SpecFlow + Selenium.WebDriver

安装(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

 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 = 0; i < code.Length; i++)
 8     {
 9         if (code[i] == guess[i])
10             mark += "+";
11     }
12 
13     for (int i = 0; i < code.Length; i++)
14     {
15         for (int j = 0; 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版本兼容性对照表

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/

转载于:https://www.cnblogs.com/tuty/p/5538735.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值