无法在表中找到列名称关键字

时间:2015-11-22 19:25:13

标签: c# selenium cucumber bdd specflow

我在BDD C#Cucumber中编写了我的第一个特征文件。我的代码构建成功。我通过右键单击方案名称并从下拉列表中选择Run Selected Tests来运行Test Explorer中的功能。浏览器打开并导航到网站。但是,测试资源管理器中显示以下错误:

Message: System.indexOutOfRangeException: Could not find a column named 'keyword' in the table.

表格如下:

| Keyword  |
| PS4      |

我的功能文件如下:

Feature: PS4 Search

@mytag
Scenario: Verify the search Functionality of Search page    
Given I navigate to the page "http://localhost:8080/company"
And I see the page is loaded
When I enter Search Keyword in the Search Text box
| Keyword  |
| PS4      |
And I click on Search Button
Then Search items shows the items related to PS4

我的步骤代码如下:

using System;
using TechTalk.SpecFlow;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;

namespace PS4SearchTest
{
    [Binding]
    public class PS4SearchSteps
    {
        IWebDriver driver;

    [Given(@"I navigate to the page ""(.*)""")]
    public void GivenINavigateToThePage(string p0)
    {
        driver = new FirefoxDriver();
        driver.Navigate().GoToUrl("http://localhost:8080/company");

    }

    [Given(@"I see the page is loaded")]
    public void GivenISeeThePageIsLoaded()
    {
        Assert.AreEqual("PS4", driver.Title);
    }

    [When(@"I enter Search Keyword in the Search Text box")]
    public void WhenIEnterSearchKeywordInTheSearchTextBox(Table table)
    {
        string search_text = table.Rows[0]["keyword"].ToString();
        driver.FindElement(By.Name("q")).SendKeys(search_text);
    }

    [When(@"I click on Search Button")]
    public void WhenIClickOnSearchButton()
    {
        driver.FindElement(By.Name("BtnG")).Click();
        driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
    }

    [Then(@"Search items shows the items related to PS4")]
    public void ThenSearchItemsShowsTheItemsRelatedToSpecFlow()
    {
        Assert.AreEqual("PS4", driver.FindElement(By.XPath("//h3/a")).Text);
        driver.Close();
    }
    }
}

为什么找不到参数PS4?我的语法对于参数表是不正确的吗?

1 个答案:

答案 0 :(得分:3)

错误说

  

找不到名为'关键字'的列在表中。

您的表格有Keyword,但在您的代码中,您正在寻找keyword

要解决此问题,请更改

string search_text = table.Rows[0]["keyword"].ToString();

string search_text = table.Rows[0]["Keyword"].ToString();