Specflow:为许多场景大纲维护一个示例表 [英] Specflow: maintain one Examples table for many Scenario Outlines

查看:72
本文介绍了Specflow:为许多场景大纲维护一个示例表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法将示例"数据表保存在与场景大纲不同的文件中?

Is there a way of keeping the "Examples" data table in a separate file from the Scenario Outline?

我想要实现的是每个浏览器一个接一个地运行整个场景.

What I am trying to achieve is to run the entire scenario once per browser, one after the other.

我有以下功能:

Feature: OpenGoogleInChrome

Scenario Outline: Open Google in Chrome
    Given a browser '<browser>'
    When the browser points to 'https://www.google.co.uk/'
    Then the title should be 'Google'

Examples:
    | browser |
    | Chrome  |
    | Edge    |
    | Firefox |

但这意味着如果我添加了另一个浏览器,则在每个测试中都维护示例表.是否可以从每个场景大纲中引用单个示例"表?

But this would mean maintaining the Examples table across every single test if I added another browser. Is it possible to reference a single "Examples" table from each Scenario Outline?

或者从步骤定义中调用场景大纲,并附上示例表?

Or call a Scenario Outline, complete with an examples table, from a Step Definition?

(在 NUnit 中使用 Specflow 和 Selenium WebDriver)

(Using Specflow and Selenium WebDriver with NUnit )

虽然背景"允许您定义共享表,但它似乎是针对具有多个场景的单个功能.我正在寻找一种在不同 (.feature) 文件中的每个功能中使用相同表的方法.

While "Background" lets you define a shared table, it appears to be for a single feature with multiple scenarios. What I'm looking for is a way to use the same table in every feature across different (.feature) files.

根据经验,我知道使用像 @Chrome 这样的标签可以适用于单个浏览器(这就是我编写大部分测试的方式),但使用多个标签会导致所有浏览器同时运行,而不是一个接一个.

From experience I know that using tags like @Chrome can work for individual browsers (this is how I have written most of my tests) but using multiple tags results in all the browsers running at the same time, not one after the other.

推荐答案

我认为最易于维护的方法是在运行测试之前通过设置环境变量来参数化所使用的浏览器.在您的 Given 步骤定义中,您可以使用 Environment.GetVariable("...") 在运行时检索值.

I think the most maintainable approach is to parameterize the browser that gets used by setting an environment variable before running the test. Inside your Given step definition, you can use Environment.GetVariable("...") to retrieve the value at runtime.

另一种替代方法是创建一个包含要使用的浏览器名称的特殊文本文件.打开此文件并读取 Given 步骤定义中的内容.

Another alternative is to create a special text file that contains the name of the browser to use. Open this file and read the contents in your Given step definition.

如果您有自动构建,您可以设置一个 Power Shell 或批处理文件,将这个文本文件设置为第一个浏览器,运行所有测试,然后将文本文件设置为下一个浏览器并重新运行测试.冲洗并重复您要使用的每个浏览器.

If you have an automated build, you could set up a power shell or batch file that sets this text file to the first browser, runs all the tests, then sets the text file to the next browser and reruns the tests. Rinse and repeat for each browser you want to use.

您可以将其放入 Visual Studio 中测试项目的 并利用配置转换.从命令行运行 NUnit 测试时,您可以切换配置:

You could throw this into the <appSettings> of the test project in Visual Studio and utilize config transformations. When running the NUnit tests from the command line you can switch the configuration:

nunit-console nunit.tests.csproj /config:Firefox
nunit-console nunit.tests.csproj /config:InternetExplorer
nunit-console nunit.tests.csproj /config:Chrome
nunit-console nunit.tests.csproj /config:Safari

缺点是您为每个浏览器创建一个构建配置,但它应该可以解决问题.

The downside is you create one build configuration for each browser, but it should do the trick.

这篇关于Specflow:为许多场景大纲维护一个示例表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆