c# - 带有两个标签的 Specflow 场景打开两个窗口

标签 c# selenium specflow

我试图在一个场景中使用多个标签,但它最终会为每个标签打开一个窗口,这会在 [AfterScenario] 步骤中导致问题。例如,我有一个场景:

@Tag01 @Tag02
Scenario Outline: User Log In
Given I'm using the <ABC>
Given I Log in as AutomatedUser

Examples:
| ABC     |
| SiteOne |
| SiteTwo |

场景之前的 stepbase.cs 文件:

[BeforeScenario("Tag01", "Tag02")]
public static void BeforeScenario()
{
    StepBase.CreateBrowser(ConfigurationManager.AppSettings["BrowserType"]);
    Console.WriteLine("selenium open Called");
}

有没有办法在不为每个标签打开一个窗口的情况下使用多个标签?

最佳答案

您期望什么行为?

如果你有这个:

@Tag01
Scenario Outline: User Log In
... etc

您是否希望调用 BeforeScenario?或者仅当您同时拥有这两个标签时?

根据你的问题的声音,如果其中一个标签存在,你希望它被调用,但只调用一次。

我认为你必须自己处理这件事。应该这样做:

public class Hooks
{
    private bool BeforeScenarioDoneAlready{get;set;}
    [BeforeScenario("Tag01", "Tag02")]
    public void BeforeScenario()
    {
        if (!DoneBeforeScenarioAlready)
        {
             StepBase.CreateBrowser(ConfigurationManager.AppSettings["BrowserType"]);
             Console.WriteLine("selenium open Called");
             BeforeScenarioDoneAlready=true;
        }
    }
}

如果您希望仅在两个标签都存在时才执行此操作,那么您可以在 BeforeScenario 方法中进行检查:

[BeforeScenario()]
public void BeforeScenario()
{
    if(ScenarioContext.Current.ScenarioInfo.Tags.Contains("Tag01")
      && ScenarioContext.Current.ScenarioInfo.Tags.Contains("Tag02"))
    {
         StepBase.CreateBrowser(ConfigurationManager.AppSettings["BrowserType"]);
         Console.WriteLine("selenium open Called");
    }
}

关于c# - 带有两个标签的 Specflow 场景打开两个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33155787/

相关文章:

c# - 使用 Linq2Sql 在 C# asp.net MVC 中创建和更新多对多关系

python - 使用 selenium webdriver 作为基类 python

selenium - 如何测试模糊 - Firefox Selenium 驱动程序?

msbuild - 尝试生成测试执行报告时specflow失败

c# - lambda 表达式中带有 Join 的Where 子句

C# HttpWebRequest问题

c# - 隔离单元测试 Controller 的值(value)

java - 如何 "hover over" Selenium 中的按钮?

c# - 如何在 SpecFlow 中使用 Nlog 登录?

c# - Selenium:By.Id ("myelement") 和 By.Css ("#myelement") 之间是否存在性能差异?