在Visual Studio单元测试项目中运行Gherkin功能

时间:2017-09-08 11:47:38

标签: c# selenium specflow gherkin

我正在尝试设置并运行一个简单的测试场景,我在Chrome中打开谷歌并搜索谷歌并单击第一个链接。我是从这里得到的,对这些工具一无所知。

我正在使用SpecFlow,Gherkin和Selenium。

我目前创建了我的.feature文件和steps.cs文件:

SpecFlowFeature.feature:

Feature: SpecFlowFeature
    In order to avoid silly mistakes
    As a math idiot
    I want to be told the sum of two numbers

Scenario Outline: Browese to Google page
    Given I am on the Google home page
    When I type <search> into textbox
    Then I should see link for Google

    Examples: 
        | Search     |
        | Google     |

SpecFlowFeatureSteps.cs:

using System;
using TechTalk.SpecFlow;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace UnitTestProject2
{
    [Binding]
    public class SpecFlowFeatureSteps
    {
        private IWebDriver driver;

        [BeforeScenario]
        public void InitScenario()
        {
            driver = new ChromeDriver();
        }

        [AfterScenario]
        public void TearDownScenario()
        {
            driver.Dispose();
        }

        [Given(@"I am on the Google home page")]
        public void GivenIAmOnTheGoogleHomePage()
        {
            driver.Navigate().GoToUrl("http://google.co.uk");
        }

        [When(@"I type (.*) into textbox")]
        public void WhenITypeIntoTextbox(string p0)
        {
            driver.FindElement(By.Id("lst-ib")).SendKeys("google");
            driver.FindElement(By.XPath("//*[@id='tsf']/div[2]/div[3]/center/input[1]")).Click();
        }

        [Then(@"I should see link for Google")]
        public void ThenIShouldSeeLinkForGoogle()
        {
            driver.FindElement(By.LinkText("Googlelkj;lkhpbgpiugfytdturwxesugh;k'k#';")).Click();
        }
    }
}

当我尝试在测试资源管理器中单击“全部运行”时,它似乎没有发现任何测试。

1 个答案:

答案 0 :(得分:3)

默认情况下,单元测试提供程序是NUnit。我假设您正在使用MSTest。 您需要将app.config文件中的默认适配器更改为

EditText