cucumber框架如何实现嵌套循环?

问题描述 投票:0回答:1

我有一个场景,我必须验证 30 个链接,每个链接中有 24 个值。因此,我必须单击 30 个链接中的每个链接,然后验证该链接中的 24 个值。如何在Cucumber框架中实现这一点?

在普通的Java方法中,我们可以使用2个循环,在外循环中将输入30个链接的列表,然后内循环将输入24个值的列表。

Cucumber框架中如何实现嵌套循环? 预先感谢。

30 个链接 24 个值

selenium-webdriver cucumber bdd cucumber-java automation-testing
1个回答
0
投票

假设你有这样的功能文件

Feature: URLs and values

  Scenario: Validating URLs
    Given the following URL list:
      |http://my.url/1|
      |http://my.url/2|
      |http://my.url/3|
      |http://my.url/4|
    Then values are correspondingly equal to:
      |val 11|val 12|val 13|
      |val 21|val 22||
      |val 31|||
      |val 41|val 42|val 43|

那么首先你需要向你的项目添加

Picocontainer
依赖项:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>7.0.0</version>
    <scope>test</scope>
</dependency>

注意。 - 版本必须与您的黄瓜版本相匹配

然后你像这样实现你的步骤定义:

package click.webelement.cucumber;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import java.util.ArrayList;
import java.util.List;

public class DataTablesStepDef {

    ArrayList<String> urls;

    public DataTablesStepDef(ArrayList<String> urls){
        this.urls = urls;
    }

    @Given("the following URL list:")
    public void urlList(List<String> urls){
        this.urls.addAll(urls);
    }

    @Then("values are correspondingly equal to:")
    public void validateValues(List<List<String>> values){
        for(int i = 0; i < urls.size(); i++){
            for(int j = 0; j < values.get(i).size(); j++){
                String value = values.get(i).get(j);
                if(value != null){
                    System.out.println("Validating value["
                            +  value
                            + "] for url: " + urls.get(i));
                }
            }
        }
    }

}

现在让我们看看输出:

Validating value[val 11] for url: http://my.url/1
Validating value[val 12] for url: http://my.url/1
Validating value[val 13] for url: http://my.url/1
Validating value[val 21] for url: http://my.url/2
Validating value[val 22] for url: http://my.url/2
Validating value[val 31] for url: http://my.url/3
Validating value[val 41] for url: http://my.url/4
Validating value[val 42] for url: http://my.url/4
Validating value[val 43] for url: http://my.url/4
© www.soinside.com 2019 - 2024. All rights reserved.