c# - 在表中找不到列名关键字

标签 c# selenium cucumber bdd specflow

我已经用 BDD C# Cucumber 编写了我的第一个功能文件。我的代码构建成功。我通过右键单击场景名称并从下拉列表中选择“运行选定的测试”来从测试资源管理器运行该功能。浏览器打开并导航到该网站。但是随后在测试资源管理器中显示以下错误:

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?我的参数表语法不正确吗?

最佳答案

错误说

Could not find a column named 'keyword' in the table.

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

要修复它,改变

string search_text = table.Rows[0]["关键字"].ToString();

string search_text = table.Rows[0]["关键字"].ToString();

关于c# - 在表中找不到列名关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33859171/

相关文章:

python - 为什么 text、innerText、innerHTML 在 selenium 中返回空

python - Selenium-Python 与 chromium 浏览器(Windows)

C# Selenium - 有没有办法在不抛出 NoSuchElementException 的情况下检查元素是否存在

java - maven clean 和 eclipse clean 的区别

c# - 如何保证数据一致性

javascript - 如何缩小 SignalR

c# - 我可以将新表格加载到面板中吗?

c# - 是否可以增加窗口的边框区域以使用鼠标调整窗口大小?

cucumber - 为什么 Cucumber 会引发 AmbiguousStepDefinitionsException?

java - cucumber 与朱尼特