【问题标题】:Can I use background with examples in cucumber?我可以在黄瓜中使用背景和示例吗?
【发布时间】:2019-03-31 22:59:32
【问题描述】:

我想通过一个所有场景的第一步的例子。但是所有场景的第一步都是一样的,只有一个元素不同。所以我想在后台进行所有这一步,并想添加带有背景的示例。 简而言之,假设我想导航到 facebook、twitter、google 和 youtube 页面并单击一个元素。我可以用下面的东西吗?

Background: Open the web-page
Given Open the <web_page> website
Examples:
|web_page|
|facebook|
|twitter|
|google|
|youtube|

Scenario: ...
Scenario: ...
Scenario: ...
Scenario: ...

【问题讨论】:

    标签: cucumber


    【解决方案1】:

    这看起来很奇怪,但是如果您使用gherkin with qaf,则可以如下:

    Background: Open the web-page
    Given Open the <web_page> website
    
    
    Scenario: ...
    Examples:
    |web_page|
    |facebook|
    |twitter|
    |google|
    |youtube|
    Scenario: ...
    Examples:
    |web_page|
    |facebook|
    |twitter|
    |google|
    |youtube|
    Scenario: ...
    Examples:
    |web_page|
    |facebook|
    |twitter|
    |google|
    |youtube|
    

    使用 qaf 你还可以在特征文件之外使用external data-source,例如:

    Scenario: ...
    Examples:Examples: {"dataFile":"resources/data/sitedata.csv"}
    
    Scenario: ...
    Examples:Examples: {"dataFile":"resources/data/sitedata.csv"}
    
    Scenario: ...
    Examples:Examples: {"dataFile":"resources/data/sitedata.csv"}
    

    使用BDD2 syntax,您可以拥有以下内容:

    @dataFile:resources/data/sitedata.csv
    Background: Open the web-page
        Given Open the <web_page> website
    
    Scenario: ...
    
    Scenario: ...
    
    Scenario: ...
    

    【讨论】:

      【解决方案2】:

      您应该能够使用如下数据表实现背景 -

      Background: Open the web-page 
      Given Open the <web_page> website
      |web_page| 
      |facebook| 
      |twitter| 
      |google| 
      |youtube|
      

      如何将正确的网页值从数据表传递到步骤实现方法?

      这样做的方法可能很少,其中之一是检查您的场景名称,并根据该名称从要加载的网页的数据表中找到相应的值。

      【讨论】:

      • 我不认为上面的例子会迭代背景和每个场景,而是将数据表或映射或列表作为参数传递给步骤。
      • 它将为每个场景打开站点并从数据表中获取站点名称,我们可以根据需要进行修改。
      猜你喜欢
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 2012-06-21
      相关资源
      最近更新 更多