简体   繁体   English

我运行功能时的BDD C#错误不包含“关键字”的定义

[英]BDD C# error when i run feature does not contain a definition for 'keyword'

I am trying out BBD using Specflow. 我正在使用Specflow尝试BBD。 I am getting an error when I run the feature file. 运行功能文件时出现错误。
The error is: 错误是:

Result Message: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'System.Dynamic.ExpandoObject' does not contain a definition for 'keyword'

The error is somewhere in this method: 错误在此方法中:

 [Then(@"I should see the result for keyword")]
    public void ThenIShouldSeeTheResultForeyword(Table table)
    {
        dynamic tableDetail = table.CreateDynamicInstance();
        String key = tableDetail.keyword;
        if (currentDriver.FindElement(By.PartialLinkText(key)).Displayed == true)
            Console.WriteLine("Control Exist");
        else
            Console.WriteLine("Control not exist");
    }

My feature file implementation is: 我的功能文件实现是:

@SmokeTest
@Browser:Chrome
Scenario: Google Search for Execute Automation
    Given I have navigated to Google page
    Given I see the Google page fully loaded
    When I type search keyword as
    | Keyword     |      
    | Formula One |
    Then I should see the result for keyword
    | keyword     |       
    | Formula One |


My steps defincition file implementation is:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using SpecFlow.Assist.Dynamic;
    using OpenQA.Selenium;
    using System.Configuration;
    using TechTalk.SpecFlow;
    using Baseclass.Contrib.SpecFlow.Selenium.NUnit.Bindings;
    using TechTalk.SpecFlow.Assist;
    using SpecFlow.Assist;
    using SpecFlow;

    namespace SpecFlowFirst.Steps
    {
        [Binding]
        class GoogleSearchSteps
        {
            IWebDriver currentDriver = null;

            [Given(@"I have navigated to Google page")]
            public void GivenIhaveNavigatedToGooglePage()
            {
                Browser.Current.Navigate().GoToUrl(ConfigurationManager.AppSettings["seleniumBaseURL"]);
                currentDriver = Browser.Current;
            }

            [Given(@"I see the Google page fully loaded")]
            public void GivenISeeTheGooglePageFullyLoaded()
            {
                if (currentDriver.FindElement(By.Name("q")).Displayed == true)
                    Console.WriteLine("Page loaded fully");
                else
                    Console.WriteLine("Page failed to load");
            }

            [When(@"I type search keyword as")]
            public void WhenITypSsearchKeywordAs(Table table)
            {
                dynamic tableDetail = table.CreateDynamicInstance();
                currentDriver.FindElement(By.Name("q")).SendKeys(tableDetail.keyword);
            }

            [Then(@"I should see the result for keyword")]
            public void ThenIShouldSeeTheResultForeyword(Table table)
            {
                dynamic tableDetail = table.CreateDynamicInstance();
                String key = tableDetail.keyword;
                if (currentDriver.FindElement(By.PartialLinkText(key)).Displayed == true)
                    Console.WriteLine("Control Exist");
                else
                    Console.WriteLine("Control not exist");
            }
            }


    }

I am not sure why the error is being thrown when I run the feature. 我不确定为什么在运行功能时会引发错误。 The solution builds without errors. 该解决方案的构建没有错误。

The full error trace is: 完整的错误跟踪是:

  Test Name:    GoogleSearchForExecuteAutomation on Chrome
Test FullName:  SpecFlowFirst.Features.GoogleSearchFeature.GoogleSearchForExecuteAutomation on Chrome
Test Source:    e:\RL Fusion\projects\BDD\C# BDD\SpecFlowFirst\SpecFlowFirst\SpecFlowFirst\Features\GoogleSearch.feature : line 20
Test Outcome:   Failed
Test Duration:  0:00:11.715

Result Message: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'System.Dynamic.ExpandoObject' does not contain a definition for 'keyword'
Result StackTrace:  
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at SpecFlowFirst.Steps.GoogleSearchSteps.WhenITypSsearchKeywordAs(Table table) in e:\RL Fusion\projects\BDD\C# BDD\SpecFlowFirst\SpecFlowFirst\SpecFlowFirst\Steps\GoogleSearchSteps.cs:line 42
at lambda_method(Closure , IContextManager , Table )
at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(StepInstance stepInstance)
at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
at TechTalk.SpecFlow.TestRunner.CollectScenarioErrors()
at SpecFlowFirst.Features.GoogleSearchFeature.ScenarioCleanup() in e:\RL Fusion\projects\BDD\C# BDD\SpecFlowFirst\SpecFlowFirst\SpecFlowFirst\Features\GoogleSearch.feature.cs:line 0
at SpecFlowFirst.Features.GoogleSearchFeature.GoogleSearchForExecuteAutomation(String browser) in e:\RL Fusion\projects\BDD\C# BDD\SpecFlowFirst\SpecFlowFirst\SpecFlowFirst\Features\GoogleSearch.feature:line 26

Thanks for help, Riaz 感谢您的帮助,里亚兹

简要查看该方法代码,我认为它可能正在“修复”属性的大小写,因此请尝试使用String key = tableDetail.Keyword

I rewrote Baseclass.Contrib.SpecFlow.Selenium.NUnit for 2.1 support. 我将Baseclass.Contrib.SpecFlow.Selenium.NUnit重写为2.1支持。

New codebase, @ignore tag support for nunit3 and several testing services like BrowserStack, SauceLabs, TestingBot. 新的代码库,对nunit3的@ignore标记支持以及一些测试服务,例如Br​​owserStack,SauceLabs,TestingBot。 Just in case you want to upgrade to 2.1 万一你想升级到2.1

Here's the scenario in question: 这是有问题的方案:

 Scenario: Google Search for Execute Automation Given I have navigated to Google page Given I see the Google page fully loaded When I type search keyword as | Keyword | | Formula One | Then I should see the result for keyword | keyword | | Formula One | 

I just want to point out that: 我只想指出:

  • Your when statement has a table with a Keyword column (with an upper case K), 您的when语句具有一个包含Keyword列(大写字母K)的表,
  • And your then statement has a table with a keyword column (with a lower case k). 然后,您的then语句就有一个带有keyword列的表(小写字母k)。

This means if you update both usages of tableDetails.Keyword at the same time (from keyword to Keyword , or vice versa), one of them will always throw. 这意味着,如果您同时更新tableDetails.Keyword两种用法(从keywordKeyword ,反之亦然),则其中之一将始终抛出。


Debugging advice 调试建议

If this doesn't work for you, keep in mind that ExpandoObject implements IDictionary<string, object> and IEnumerable<KeyValuePair<string, object>> , which means you can always print to the console the available keys to understand what is actually being created by CreateDynamicInstance 如果这对您不起作用,请记住ExpandoObject实现IDictionary<string, object>IEnumerable<KeyValuePair<string, object>> ,这意味着您始终可以在控制台上打印可用键以了解实际的内容由CreateDynamicInstance创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#var关键字混淆:类型&#39;对象&#39;不包含...错误的定义 - C# var keyword confusion: Type 'object' does not contain a definition for… error &#39;string&#39;不包含C#的定义/错误 - 'string' does not contain a definition for/ Error in C# C# 错误:CS1929 不包含“包含”的定义 - C# error: CS1929 does not contain a definition for 'Contains' c#数组错误-“不包含”的定义。 包含代码 - Error in c# array-“does not contain definition for”. Code included C#UWP应用程序错误:“&#39;InkStroke&#39;不包含&#39;StrokeStartedTime&#39;的定义” - C# UWP app error: “'InkStroke' does not contain a definition for 'StrokeStartedTime'” C# Roles.GetAllRoles() 错误 - 不包含定义 - C# Roles.GetAllRoles() error - does not contain a definition C# 错误 CS0117:“数组”不包含 - C# error CS0117: 'Array' does not contain a definition for 声明显式接口,并且不包含定义错误C# - Declaring Explicit interfaces and the does not contain a definition error C# 如何解决错误不包含定义 - C# 新手 - How to solve error does not contain a definition for - new to C# Selenium C#Webdriver:我收到错误消息:“ iWebDriver”不包含“ WaitforElement”的定义 - Selenium C# Webdriver: I am getting error: 'iWebDriver' does not contain definition for 'WaitforElement'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM