使用SpecFlow和Selenium

时间:2016-07-26 16:11:41

标签: unit-testing selenium visual-studio-2012 selenium-webdriver specflow

我的 SpecFlow 单元测试中出现错误:

  

System.Collections.Generic.KeyNotFoundException:字典中没有给定的键。      at TechTalk.SpecFlow.SpecFlowContext.Get [T](String key)      在F *****。Live.PS。*******。步骤。*******。在C:\ Git \ LivePeopleSystemTests \ Fourth.Live.PS中的EnterStagingDateAndSaveSteps.ThenUserSeesInNEXTRE_ENROLMENTWINDOWTextFieldInPENSIONASSESSMENTDATESPageInPENSIONModule(String p0) .AutomationTests \ F *****。Live.PS。*******。步骤******* \ EnterStagingDateAndSaveSteps.cs:第175行

以下是EnterStagingDateAndSaveSteps.cs

中引用的方法
[Then(@"user sees ""(.*)"" in NEXT RE-ENROLMENT WINDOW Text Field in PENSION ASSESSMENT DATES page in PENSION module")]
    public void ThenUserSeesInNEXTRE_ENROLMENTWINDOWTextFieldInPENSIONASSESSMENTDATESPageInPENSIONModule(string p0)
    {
        // GetNextReEnrollmentWindow
        string validator =
            ScenarioContext.Current.Get<PensionAssessmentDates>("_nextReEnrollmentWindow")
                .GetNextReEnrollmentWindow();
        Assert.IsTrue(validator == p0);
    }

(我也尝试了return Driver.WaitAndGetText(_nextReEnrollmentWindow);

因此看起来密钥_nextReEnrollmentWindow不存在,但此处在 PensionAssessmentDates.cs 中定义:

private readonly By _nextReEnrollmentWindow = By.Id("NextReEnrollmentWindow"); 

enter image description here

PensionAssessmentSteps设置为 ScenarioContext.Current ,如下所示:

    [Then(@"user sees ""(.*)"" in NEXT RE-ENROLMENT WINDOW Text Field in PENSION ASSESSMENT DATES page in PENSION module")]
    public void ThenUserSeesInNEXTRE_ENROLMENTWINDOWTextFieldInPENSIONASSESSMENTDATESPageInPENSIONModule(string p0)
    {
        // GetNextReEnrollmentWindow
        string validator =
            ScenarioContext.Current.Get<PensionAssessmentDates>("_nextReEnrollmentWindow")
                .GetNextReEnrollmentWindow();
        Assert.IsTrue(validator == p0);
    }

这是我试图测试的实际网页,显示该元素存在:

enter image description here

如果我错过了导致我的单元测试停止的指示或建议,我将不胜感激:

 EnterStagingDateAndSaveSteps.WhenUserClicksSAVEButtonUnderAUTOMATICENROLMENTATSTAGINGDATEFrameInPENSIONASSESSMENTDATESPageInPENSIONModule() (0.7s)
Then user sees "02 Oct 2019 to 02 Apr 2020" in NEXT RE-ENROLMENT WINDOW Text Field in PENSION ASSESSMENT DATES page in PENSION module
-> error: The given key was not present in the dictionary.

1 个答案:

答案 0 :(得分:2)

ScenarioContext.Current.GetSet方法只允许您使用键将对象添加到附加到当前场景的值字典中。

在一步中将元素添加到字典后,您可以在另一个步骤中检索它。您获得的异常意味着您没有使用密钥_nextReEnrollmentWindow向字典添加任何内容。

您是否在代码中的任何位置致电ScenarioContext.Current.Set("_nextReEnrollmentWindow", something);?我怀疑不是。

考虑到你问这个问题的方式,我怀疑你期望ScenarioContext.Current.Get<PensionAssessmentDates>("_nextReEnrollmentWindow")调用能够获得类型为PensionAssessmentDates的页面对象的当前实例,然后使用选择器获取元素_nextReEnrollmentWindow。这不是它的工作原理。

你想做我相信的两件事之一。将页面对象PensionAssessmentDates添加到ScenarioContext.Current,然后输出页面对象并调用使用私有字段_nextReEnrollmentWindow的方法。

或者(在我看来更好)完全放弃使用ScenarioContext.Current,而是创建保存页面对象的对象并让Specflows internal DI framework provide those to your step classes using context injection