在 WebDriver 方法中获取 Specflow 标签

2024-03-28

我正在使用 C#、selenium 和 Specflow 运行自动化测试套件。如果可能的话,我希望能够查看分配给当前场景的标签,以便我可以为每个场景实例化某种浏览器类型。使用 XUnit 是否可以实现这一点?

登录功能文件:

Feature: Login
    In order to login to DRIVE
    As a user
    We have to enter login details

Background:
    Given I am on the login page

@headless
Scenario: Logging in as a valid user
    And I enter a valid user and password
    When I submit the login form
    Then The user should be logged in

WebDriverContext.cs 文件

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.PhantomJS;

namespace Drive.Acceptance.Tests
{
    public interface IWebDriverContext {
        IWebDriver GetDriver();
    }

    public class WebDriverContext : IWebDriverContext
    {
        private static volatile WebDriverContext _instance;
        private static readonly object Lock = new object();

        public static IWebDriverContext Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (Lock)
                    {
                        if (_instance == null)
                            _instance = new WebDriverContext();
                    }
                }

                return _instance;
            }

        }

        public IWebDriver GetDriver()
        {
            lock (Lock)
            {
                // TODO: create headless browser if scenario is tagged with @headless
                if (!TagName.Contains("headless")) {
                    return new ChromeDriver();
                }
                else {
                    return new PhantomJSDriver();
                }
            }
        }
    }
}

您可以在 ScenarioContext 中获取该 Scenario 的标签列表。

ScenarioContext.ScenarioInfo.Tags

see https://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/ScenarioInfo.cs https://github.com/techtalk/SpecFlow/blob/master/TechTalk.SpecFlow/ScenarioInfo.cs

您可以通过 Context-Injection 获取实际的 ScenarioContext (http://specflow.org/documentation/Context-Injection/ http://specflow.org/documentation/Context-Injection/) 或通过 ScenarioContext.Current (http://specflow.org/documentation/ScenarioContext/ http://specflow.org/documentation/ScenarioContext/).
如果可能的话,通过上下文注入获取它。这样,如果您想并行运行测试,将来就不会出现问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 WebDriver 方法中获取 Specflow 标签 的相关文章

  • 跳转到文件行c#

    我如何跳到文件中的某些行 例如 c text txt 中的第 300 行 using var reader new StreamReader c test txt for int i 0 i lt 300 i reader ReadLine
  • 使 minGW 控制台程序在没有控制台的情况下运行

    我在 MinGW 中制作了一个控制台程序 它执行网络和文件操作 然而 它是一个控制台程序 如何让它安静地运行 没有控制台 没有窗口 尝试这个 ShowWindow GetConsoleWindowHandle SW HIDE GetCons
  • 为什么 strcat() 之后字符串会被改变?

    这是源代码 int main char str dance char str1 hello char str2 abcd strcat str1 str2 printf s str output bcd why str更改后strcat s
  • 在 C# 中,为什么从列表创建 HashSet 比从 HashSet 开始更快?

    我有一个方法 它采用上限 并返回达到该限制的素数列表 public static List
  • 如何在 Google 日历中创建“recurData”?

    我想使用 Google API 创建日历的重复事件 我正在关注链接 谷歌日历API http code google com apis calendar data 2 0 developers guide dotnet html Creat
  • 在 Windows 上实现堆栈跟踪 [关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我正在为我正在编写的游戏实现一个崩溃报告工具 并且我想为该报告提供 相当 详细的本机堆栈跟踪 我已经在 GNU Linux 上实现
  • lua_resume 的 from 参数的含义

    From Lua 5 2 参考手册 http www lua org manual 5 2 manual html lua resume int lua resume lua State L lua State from int nargs
  • MDI应用程序中父窗体的问题

    我使用按钮作为容器中的控件 父窗体 当子窗体出现时 父窗体中的控件 按钮 图片 标签 出现在子窗体上并将其覆盖 我看不到子窗体 有谁知道如何防止这种情况 我不想将这些控件设置为 Control Visible false 因为当我最小化子表
  • 接收UDP数据包

    假设我的程序通过网络 UDP 发送 1000 字节 它是否保证接收方将 一批 接收 1000 个字节 或者他可能需要执行多次 读取 直到收到完整的消息 如果后者为真 我如何确保同一消息的数据包顺序不会 混淆 按顺序 或者协议可能保证这一点
  • std::vector 错误 C2582:“operator =”函数在以下位置不可用

    我使用简单的向量push back到类型A的对象 并收到此错误 这是我的代码 class A public A int a int b int c include A h std vector a vec objects new std v
  • 为 C# 和 C++ 应用程序编写 DLL

    我需要编写几个 DLL 它们都可以从 C 应用程序和 C 应用程序访问 最初 我认为通过用 C 编写 DLL 并从 C 和 C 应用程序链接到它们可以节省时间 精力 这种方法明智吗 还是应该使用 C 编写 DLL 我的建议是在您最舒服的地方
  • 不区分大小写“包含(字符串)”

    有没有办法让下面的返回为真 string title ASTRINGTOTEST title Contains string 似乎没有过载允许我设置区分大小写 目前我将它们都大写 但这很愚蠢 我指的是i18n http en wikiped
  • 当无法处理指定的情况时,在 switch 语句中抛出异常

    假设我们有一个函数可以在 MVC 应用程序的系统中更改用户的密码 public JsonResult ChangePassword string username string currentPassword string newPassw
  • dev_t 和 ino_t 是否必须是整数类型?

    glibc 的文档保留它们是整数类型 不比 unsigned int 窄 但我没有找到说明它们必须是整数类型的标准参考 另请参阅 time t 所以最后 问题就变成了 include
  • 位运算符,而不是在分支中使用异或

    问完后这个问题 https stackoverflow com questions 22336015 why use xor with a literal instead of inversion bitwise not 我收到了 Ando
  • LINQ 表达式树 Any() 位于Where() 内

    我正在尝试生成以下 LINQ 查询 Query the database for all AdAccountAlerts that haven t had notifications sent out Then get the entity
  • 绑定到外部库中基本方法的方法无法处理“之间”的新虚拟方法

    假设我有一个库 版本 1 0 0 包含以下内容 public class Class1 public virtual void Test Console WriteLine Library Class1 Test Console Write
  • 缓存行对齐(需要文章澄清)

    我最近在我的应用程序中遇到了我认为是错误共享的问题 我查了一下关于如何将我的数据与缓存行对齐 他建议使用以下 C 代码 C using C 0x alignment syntax template
  • Security.h 中结构的 macOS 文档

    我正在尝试使用Security h通过 Java 和 JNA 的 macOS 框架 这意味着我需要将某些结构重建为 Java 类 问题是 当我查看文档中的结构时 this one https developer apple com refe
  • Phong 着色问题

    我正在根据以下内容编写着色器冯模型 http en wikipedia org wiki Phong reflection model 我正在尝试实现这个方程 其中 n 是法线 l 是光线方向 v 是相机方向 r 是光反射 维基百科文章中更

随机推荐