如何在Cucumber特性文件中传递具有不同参数数量的多个示例

huangapple go评论69阅读模式
英文:

How to pass multiple examples with different number of parameters in Cucumber feature file

问题

以下是翻译好的部分:

我的功能文件中的一项步骤需要两个参数,比如登录和密码,用于多个系统,单独运行正常,但是否有办法可以为各个步骤传递多个示例?

功能文件中的第一步是调用POST API并基于参数获取响应。

场景大纲:验证搜索结果

给定我在标头中设置授权令牌,其特征文件正文负载如下:

| Key       | Value           | 
|InstituteID| <InstitutionID> |         
|InstituteID|
|1234456    |
|1345679    |
|4564565    |

第二步是:

并且用户输入“<Username>”,“<Password>”并单击登录按钮

|Username|Password |
|test    |abc      |
|test2   |abc      | 

请问是否有办法实现这一点?我是否可以在一个场景大纲中传递多个示例,如上所述?我需要从步骤1中获得一些响应,以执行步骤2,因此无法拆分成两个场景。

谢谢。

英文:

One of the steps in my feature file requires two parameters like login and password for multiple systems which individually works fine but Is there any way I can pass multiple examples for individual steps?
The first step in the feature file is calling POST API and getting a response based on parameters.
Scenario Outline: Verify search results
Given I set the authorization token in Header with below feature file body payload

| Key       | Value           | 
|InstituteID| &lt;InstitutionID&gt; |         
|InstituteID|
|1234456    |
|1345679    |
|4564565    |

and second step is:

And User enters &quot;&lt;Username&gt;&quot;, &quot;&lt;Password&gt;&quot; and click on Login button

|Username|Password |
|test    |abc      |
|test2   |abc      | 

Could you please share if there is any way to achieve this? Can I pass multiple examples in one scenario outline like mentioned above? I need a few responses from step 1 to execute step 2 so I can not break into two scenarios.
Thank you.

答案1

得分: 1

是的,您可以传递多个示例。我会给您一个示例:

场景大纲:我想要登录
假设我在登录页面上
那么我使用“”和“”登录

示例:
| Username | Password |
| User1 | 12345678 |
| User2 | 12345679 |
| User3 | 12345670 |

现在,上面的场景将运行3次,分别针对这些示例。

在步骤文件中,您可以创建一个类似于以下的函数:

@Given("^我在登录页面上$")
public void navigate_to_login_page() {
//一些逻辑
}

@Then("^我使用“([^\"])”和“([^\"])”登录$")
public void login(String username, String pass) {
//一些逻辑
}

英文:

Yes, you can pass multiple examples. I'll give you an example:

Scenario Outline: I want to login
	Given I am on the login page
	Then I log in with &quot;&lt;Username&gt;&quot; and &quot;&lt;Password&gt;&quot;
	
	Examples:
		|  Username  |  Password  |
		|  User1	 |  12345678  |
		|  User2	 |  12345679  |
		|  User3	 |  12345670  |

Now, the above scenario will run 3 times, for each of these examples.

In the step file, you can create a function like this:

@Given(&quot;^I am on the login page$&quot;)
public void navigate_to_login_page() {
    //some logic
}

@Then(&quot;^I login with &quot;([^\&quot;]*)\&quot; and &quot;([^\&quot;]*)\&quot;$&quot;)
public void login(String username, String pass) {
    //some logic
}

答案2

得分: 0

你只能为整个场景大纲编写示例,而不能为单个步骤编写示例。

但你可以编写尽可能多的示例,并在其中混合使用你的参数。

Edit 1:

一个 DataTable 可能对你有帮助。

定义粘合代码如下,注意方法参数 DataTable:

@When("A POST API is called with parameters:")
public void callPostApi(DataTable table) {
    List<List<String>> rows = table.cells(1); // 跳过标题行
    for (List<String> row : rows) {
       String parameter = row.get(0);
       // 在这里调用参数的 POST API,保存,检查结果等。
    }
}

并在.feature 文件中使用它:

When A POST API is called with parameters:
| Parameter |
| 123       |
| 456       |
| 789       |
| abc       |
| def       |
英文:

You can't write Examples for individual steps, only for whole Scenario Outlines.

But you can write as much examples as you want and mix your parameters in them

Examples:
|Username|Password|InstituteID| 
|test    |abc      |1234456    |
|test    |abc      |1345679    |
|test    |abc      |4564565    |
|test2   |abc      |1234456    |
|test2   |abc      |1345679    | 
|test2   |abc      |4564565    | 

Edit 1:

А DataTable may help you

Define the glue code like this, mind the method parameter DataTable:

    @When(&quot;A POST API is called with parameters:&quot;)
    public void callPostApi(DataTable table) {
        List&lt;List&lt;String&gt;&gt; rows = table.cells(1); // skip the header
        for (List&lt;String&gt; row : rows) {
           String parameter = row.get(0);
           // Call the POST API for the parameter here, save, check the results etc.
        }
    }

And use it in the .feature file

  When A POST API is called with parameters:
  | Parameter | 
  | 123       |
  | 456       |
  | 789       |
  | abc       |
  | def       |

huangapple
  • 本文由 发表于 2020年8月10日 05:09:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63331351.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定