【问题标题】:I want to execute multiple scenarios in a single SpecFlow feature file one after another (based on selenium) but it's not working and showing an error我想一个接一个地在单个 SpecFlow 功能文件中执行多个场景(基于 selenium),但它不工作并显示错误
【发布时间】:2021-07-08 19:06:03
【问题描述】:

我是 SpecFlow 的新手,并且正在使用 POC,因此我可以在我的项目中使用 VisualStudio+SpecFlow+Selenium 组合。我创建了一个功能文件并添加了两个场景。第一个用于登录,第二个用于页面创建。因此,我将使用一个用户登录并在同一会话中创建一个页面。我使用 SpecFlow 从功能文件生成了步骤定义文件。以下是我的 Feature 文件内容:

Feature: Login and create page configuration page
    This feature will test the login and the page configuration page creation functionality:

@Login
Scenario Outline: Login into Minerva Cloud
    Given User open up the "<URL>"
    When User login using "<UserName>" and "<Password>"
    Then User successfully logged into the Minerva Cloud
    And User selects the "<ClientName>"

Examples: 
|URL|UserName|Password|ClientName|
|https://####|####|####|## Global Services|


@PageConfigurationPageCreation
Scenario Outline: New Page Configuration page creation
    Given User click on the new button
    When User enters details like "<Name>" "<Type>" "<CRMtype>" "<URL>" "<Description>" "<AccessGroup>" in the creation page
    And click on the Save button
    Then the new page is created

Examples: 
|Name|Type|CRMtype|URL|Description|AccessGroup|
|PageConfiguration_Sunil1|CRM|Vertex|URL|Description for my Page|Access Group to be selected|

附上生成的step文件sn-p:

Code Generated by SpecFlow and i added the required Selenium code

因此,每当我运行功能文件时,它都会毫无问题地执行登录场景,但一旦到达属于第二个场景的第二个 Given 语句(在所附图像中标记为红色),它就会失败并引发错误。错误是:

System.NullReferenceException : 对象引用未设置为对象的实例。

看起来它以某种方式丢失了我在第一个场景中初始化的 Webdriver 的当前实例。

请帮忙!

【问题讨论】:

  • 请勿发布代码图片。在您的问题中包含编码为纯文本。
  • 这能回答你的问题吗? Using ChromeDriver across SpecFlow Tests
  • 当然,以后会处理的。不,我还没有找到任何答案。

标签: c# visual-studio selenium specflow


【解决方案1】:

如果您想要一个功能中所有场景的单个驱动程序实例,请在 [Beforefeature] 中初始化您的驱动程序并使用 [Afterfeature] 处理它,如下所示

namespcae Yourproject
[Binding]
public class BeforeAndAfterFeature
{
[BeforeFeature]
    public static void InitializeDriver()
    {
        //Place your driver initialize code here
    }
[AfterFeature]
    public static void QuitDriver()
    {
        //Dispose driver object
    }
}
  1. 或者,如果您想为每个场景打开和退出浏览器,请使用 [BeforeScenario] 和 [AfterScenario] 标签

【讨论】:

    【解决方案2】:

    您正在第一个 Given 方法中初始化驱动程序。这意味着它会在该场景运行后死亡。我不使用 SpecFlow,但从快速 search 看来您需要使用 [BeforeScenario] 来初始化驱动程序并使用 [AfterScenario] 来关闭它

    【讨论】:

    • 感谢您的回复,但是如果我在第一个场景之后关闭驱动程序,那么我最终会遇到与现在相同的情况。我的意思是它不能解决一个接一个地执行这两个场景的目的,因为我想先执行登录场景,然后在第一个场景之后立即执行创建场景。我想这样做是因为我想实现“登录一次并验证多个”场景。我希望它能提供某种澄清。
    • 这个想法是在你的任何场景执行之前使用一次[BeforeScenario],在最后一个场景执行之后使用一次[AfterScenario]。因此,您将对其进行一次初始化,并且在所有场景运行结束后才会关闭它
    • 我可能错了,但据我所知,这些钩子会在场景开始后自动执行。如果有办法控制它们,请告诉我。我想试一试。
    • 我们也有 [BeforeFeature] 和 [AfterFeature],所以我想使用它们,但看起来 [BeforeFeature[ 和 [AfterFeature] 不适合我。不知道为什么。也尝试过上下文注入,但只要第二个Given 开始,它就会再次调用网络驱动程序。
    猜你喜欢
    • 2021-08-24
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多