簡體   English   中英

我運行功能時的BDD C#錯誤不包含“關鍵字”的定義

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

我正在使用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

感謝您的幫助,里亞茲

簡要查看該方法代碼,我認為它可能正在“修復”屬性的大小寫,因此請嘗試使用String key = tableDetail.Keyword

我將Baseclass.Contrib.SpecFlow.Selenium.NUnit重寫為2.1支持。

新的代碼庫,對nunit3的@ignore標記支持以及一些測試服務,例如Br​​owserStack,SauceLabs,TestingBot。 萬一你想升級到2.1

這是有問題的方案:

 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 | 

我只想指出:

  • 您的when語句具有一個包含Keyword列(大寫字母K)的表,
  • 然后,您的then語句就有一個帶有keyword列的表(小寫字母k)。

這意味着,如果您同時更新tableDetails.Keyword兩種用法(從keywordKeyword ,反之亦然),則其中之一將始終拋出。


調試建議

如果這對您不起作用,請記住ExpandoObject實現IDictionary<string, object>IEnumerable<KeyValuePair<string, object>> ,這意味着您始終可以在控制台上打印可用鍵以了解實際的內容由CreateDynamicInstance創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM