I have a scenario, where i have to validate 30 links and in each link there are 24 values. So i have to click on every link in 30 links and then validate the 24 values in that link. How to achieve this in cucumber framework?? In normal java methods we can use 2 loops, in outer loop will input the list of 30 links then in inner loop will input the list of 24 values. How to achieve nested loop in cucumber framework?? Thanks in advance
Asked
Active
Viewed 500 times
0
-
1How to achieve nested loop in cucumber framework? --> Cucumber is a tool based on BDD framework there is nothing to write different using cucumber. If you are using Java bindings then you can use the same approach what you have mentioned (two loops) in your question. – Nandan A Dec 15 '21 at 06:50
-
But there is a data table concept in cucumber right ? can't we utilize that for this scenario ?? – Konapala Gupta Dec 15 '21 at 08:37
1 Answers
0
Say you have feature file like this
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|
Then first of all you need to add Picocontainer
dependency to your project:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
N.B. - Version has to match your cucumber version
Then you implement your step defs like this:
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));
}
}
}
}
}
Let's now look at the output:
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

Alexey R.
- 8,057
- 2
- 11
- 27