【问题标题】:BDD C# error when i run feature does not contain a definition for 'keyword'当我运行功能时 BDD C# 错误不包含“关键字”的定义
【发布时间】:2017-01-13 01:17:11
【问题描述】:

我正在尝试使用 Specflow 的 BBD。运行功能文件时出现错误。
错误是:

Result Message: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'System.Dynamic.ExpandoObject' does not contain a definition for '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");
    }

我的功能文件实现是:

@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");
            }
            }


    }

我不确定为什么在运行该功能时会引发错误。解决方案构建没有错误。

完整的错误跟踪是:

  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

谢谢你的帮助,里亚兹

【问题讨论】:

  • 您使用的是哪个版本的 SpecFlow?您从哪里获得 CreateDynamicInstance 方法?我在 SpecFlow 源代码中找不到它。
  • @AndreasWillich 来自this project
  • 好的,那么根据报错就在这个包里面了。
  • 我有以下specflow包SpecFlow.Assist.Dynamic、specFlow.Assist;规范流; SpecFlow.Assist.Dynamic;我正在使用 SpecFlow 2.1.0

标签: c# selenium-webdriver bdd specflow


【解决方案1】:

简单地看一下code for that method,我认为它可能正在“修复”该属性的情况,所以尝试使用String key = tableDetail.Keyword

【讨论】:

  • 我试过了。我仍然得到同样的错误。结果消息:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:'System.Dynamic.ExpandoObject' 不包含'keyword 的定义
【解决方案2】:

I rewrote Baseclass.Contrib.SpecFlow.Selenium.NUnit for 2.1 support.

新的代码库、对 nunit3 的 @ignore 标签支持以及 BrowserStack、SauceLabs、TestingBot 等多个测试服务。以防万一您想升级到 2.1

【讨论】:

    【解决方案3】:

    这是有问题的场景:

    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 |
    

    我只想指出:

    • 您的whenstatement 有一个带有Keyword 列(带有大写K)的表,
    • 您的then 语句有一个表,其中包含keyword 列(带有小写k)。

    这意味着如果您同时更新tableDetails.Keyword 的两种用法(从keywordKeyword,反之亦然),其中一个总是会抛出。


    调试建议

    如果这对您不起作用,请记住 ExpandoObject 实现了 IDictionary<string, object>IEnumerable<KeyValuePair<string, object>>,这意味着您始终可以将可用键打印到控制台以了解 @987654333 实际创建的内容@

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 2019-12-08
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      相关资源
      最近更新 更多