Specflow-如何在方案中使用多个表的组合 [英] Specflow - How to use combination of multiple tables in scenario

查看:141
本文介绍了Specflow-如何在方案中使用多个表的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Specflow执行BDD测试.我正在尝试在多个浏览器上测试我的导航菜单.尤其要确保在浏览器中都显示按钮.我不想为每个菜单项专门在每个浏览器上创建一堆测试,也不想创建一个遍历每个浏览器/菜单项组合的大表.有什么方法可以指定2个表,然后创建一个执行两个表的组合的场景?

I am using Specflow to perform BDD tests. I am trying to test my navigation menu on multiple browsers. Particularly making sure that buttons are showing up accross browsers. I do not want to create a bunch of tests for each menu item specifically on each browser and I do not want to create a large table that goes over every browser/menu item combination. Is there any way to specify 2 tables and then create a single scenario that performs the combinations of both?

例如:

菜单项表

| menuItem |
| Home     |
| About    |
| Contact  |

浏览器表

| browser |
| Chrome  |
| Firefox |
| IE      |

场景

Scenario Outline: I can see menu item
    Given I navigate to the "/" page using <browser>
    Then I can see the menu item <menuItem>

预期结果是,当它运行时它将运行9个测试.

The expected result is that when this is run it would run 9 tests.

推荐答案

我个人更喜欢列出所有组合.可能会不需要一个或两个,或者您需要为每个指定特殊的期望值,等等.

Personally I prefer enlisting all of the combinations. It can happen that one or two is not needed or you need to specify special expected values for each, etc.:

Scenario: Flat scenario
    Given I have the following config:
    | menuItem | browser |
    | Home     | Chrome  |
   #| Home     | IE      | - ok, this is not needed
    | Home     | Firefox |
    | About    | Chrome  |
    | About    | IE      |
    | About    | Firefox |
    | Contact  | Chrome  |
    | Contact  | IE      |
    | Contact  | Firefox |
    Then something happens

如果您真的想创建完整的组合,我将在示例中使用方案大纲:

If you really want to create full combinations I would use Scenario Outline with examples:

Scenario Outline: Combined scenario
    Given I have the following config:
    | MenuItem | Browser   |
    | Home     | <browser> |
    | About    | <browser> |
    | Contact  | <browser> |
    Then something happens

    Examples:
    | browser |
    | Chrome  |
    | Firefox |
    | IE      |

更新:

在基础方法中,最后一个参数可以是Table.因此,在上面的示例中,您可以得到如下表:

In the underlying method the last parameter can be a Table. So in the example above you can get the table as follows:

[Given(@"I have the following config:")]
public void InitFromConfiguration(Table table)
{
    // now the table has MenuItem and Browser columns
}

在一个测试案例中,考虑到浏览器是不变的,我将其更改如下:

Considering browser is constant for one test case I would change it as follows:

Scenario Outline: Even better combined scenario
    Given I have the following items in the specified <browser>:
    | MenuItem |
    | Home     |
    | About    |
    | Contact  |
    When I test the browser with the given menuItems
    Then I have no errors

    Examples:
    | browser |
    | Chrome  |
    | Firefox |
    | IE      |

[Given(@"I have the following items in the specified (.*):")]
public void InitFromConfiguration(string browser, Table menuItems)
{
    // now the browser comes from the Examples and menuItems has 3 rows
}

如果您更喜欢强类型而不是Table,甚至可以为菜单项定义转换步骤:

You can even define a transform step for menu items if you prefer strong types instead of Table:

[Binding]
public class MyTransformations
{
    [StepArgumentTransformation]
    public MenuItem[] ToMenuItems(Table table)
    {
        return table.Rows.Select(row => new MenuItem(row[0])).ToArray();
    }        
}

现在您可以按以下方式定义Given:

And now you can define your Given as follows:

[Given(@"I have the following items in the specified (.*):")]
public void InitFromConfiguration(string browser, MenuItem[] menuItems)
{
    ScenarioContext.Current.Set(browser, nameof(browser));
    ScenarioContext.Current.Set(menuItems, nameof(menuItems));
}

When步骤中进行测试.毕竟,这是您将呼叫DoTest 9次的方法:

Do the test itself in the When step. This is how you will call DoTest 9 times after all:

[When(@"I test the (.*) with the given (.*)")]
public void InitFromConfiguration(string browserKey, string menuItemsKey)
{
    var browser = ScenarioContext.Current.Get<string>(browserKey);
    var menuItems = ScenarioContext.Current.Get<MenuItem[]>(menuItemsKey);

    // TODO: in DoTest you can collect and save the possible errors in the context
    foreach (MenuItem mi in menuItems)
        DoTest(browser, mi);
}

最后,在Then步骤中,可以声明在DoTest方法中收集并存储到上下文中的可能错误.

And finally in the Then step you can assert the possible errors you collected and stored into the context in the DoTest method.

这篇关于Specflow-如何在方案中使用多个表的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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