在SpecFlow的示例表中具有表 [英] Having Tables in Example Table in SpecFlow

查看:75
本文介绍了在SpecFlow的示例表中具有表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将SpecFlow与VisualStudio 2013结合使用以测试具有以下布局的特定Web表单
********************开始:表格******************
姓名:________________
年龄:_________________
经验:
工作地点年数|
___________ | _________________ |
___________ | _________________ |
___________ | _________________ |
___________ | _________________ |
**********************结束:表格******************

I am using SpecFlow with VisualStudio 2013 to test a particular web form which has the following layout
******************** Start: Form ******************
Name:________________
Age:_________________
Experience:
Work place | Number of years |
___________ | _________________ |
___________ | _________________ |
___________ | _________________ |
___________ | _________________ |
******************** End: Form ******************

如何编写它以便可以从示例中获取内容?

How do I write it so that I can feed from Example?

Given ...
When user fills 'Name-Box' with '<name>'
And user fills 'Age-Box' with '<age>'
And user fills experience with
|  Workplace   | NumberOfYears |
|  <workplace> |  <years>      |
And user clicks the 'Save' button
Then ...

Examples:
name|age
Sam|40
#note that there will be always 1 here.

Examples:
workplace|years
abc|2
xyz|3
pqr|4

之所以要这样做,是因为我可以通过编程方式从外部电子表格生成示例表,并对其进行动态更改.是否有可能做到这一点?还是有什么问题?

The reason why I want to do it this way, is so that I can generate the example table from an external spreadsheet programmatically and change it dynamically. Is it possible to do this? or is there anything wrong?

推荐答案

可以,但是必须重新构造示例表:

You can, but you'll have to restucture your example table:

Given ...
When user fills 'Name-Box' with '<name>'
And user fills 'Age-Box' with '<age>'
And user fills experience with
    | Workplace     | NumberOfYears |
    | <workplace 1> | <years 1>     |
    | <workplace 2> | <years 2>     |
    | <workplace 3> | <years 3>     |
    | <workplace 4> | <years 4>     |
And user clicks the 'Save' button
Then ...

Examples:
    | name | age | workplace 1 | years 1 | workplace 2 | years 2 | workplace 3 | years 3 | workplace 4 | years 4 |
    | Sam  | 40  | abc         | 2       | xyz         | 3       | pqr         | 4       |             |         |
    | Sam  | 40  | aaa         | 3       | bbb         | 12      |             |         |             |         |
    | Sam  | 40  | nnn         | 2       | ooo         | 20      | ppp         | 1       | qqq         | 3       |

表格中的每一行在示例表中都需要一个工作区X"和年份X"列.

Each row in the form needs a "workspace X" and "years X" column in your examples table.

STR说:

但是可以说,我们希望让工作场所的数量不受限制.然后,我如何为在3个工作场所工作的人以及另一个在15个工作场所工作的人定​​义行.通过固定数量的列,我们是否限制了测试的可能性?

But lets say we want to let the number of workplaces unlimited. Then, how can I define rows for a person with experience in 3 workplaces along with another person with experience in 15 workplaces. By having fixed number of columns, aren't we restricting the possibilities to test?

是的.如果您需要无限数量的行,则仍然可以重组测试,但我认为您不能使用方案大纲.

That is true. If you need an unlimited number of rows, you can still restructure your test, but I don't think you can use a Scenario Outline.

您可以使用:

And user fills experience with
    | Row | Workplace | Number of Years |
    | 1   | a         | 2               |
    | 2   | b         | 4               |
    | 3   | c         | 10              |
    | ... | ...       | ...             |
    | 15  | o         | 1               |

步骤定义将分为两个部分:

The step definition would be two parts:

public class WorkExperienceRow
{
    public int RowNumber { get; set; }
    public string Workplace { get; set; }
    public int NumberOfYears { get; set; }
}

使用TechTalk.SpecFlow.Assist命名空间中的SpecFlow TableExtensions,上面的类可用于表示SpecFlow表中的每一行.

Using the SpecFlow TableExtensions in the TechTalk.SpecFlow.Assist namespace, the class above can be used to represent each row in the SpecFlow table.

using TechTalk.SpecFlow;
using TechTalk.SpecFlow.Assist;

[Binding]
public class FooSteps
{
    [When(@"user fills experience with")]
    public void WhenUserFillsExperienceWith(Table table)
    {
        IEnumerable<WorkExperienceRow> experiences = table.CreateSet<WorkExperienceRow>();
        // Find the <table>

        foreach (var experience in experiences)
        {
            // Find the <tr> by index using experience.RowNumber
            // Find the text box in column #1
            // Fill in the text box with experience.Workplace
            // Find the text box in column #2
            // Fill in the text box with experience.NumberOfYears
        }
    }
}

这篇关于在SpecFlow的示例表中具有表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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