Question

I have the following feature that I would like to test in cucumber. But, I would like to process the input file only once ( @Given in the below feature ). But, it seems to be executing the @Given step each time. Is it possible to execute this @Given in the following feature only once?

@fileValidation

Scenario Outline: File Validation

Given a file is uploaded with name "something.csv"
Then response filename created should not match input filename "something.csv"
And reason for errors should be "<Reason>" with error code "<Error code>" for row with RequestId "<RequestId>"

Examples:

  | RequestId     |  Error code | Reason |     
  | 123           |   101       | Failure 1 |
  | 124           |   102       | Failure 1; Failure 2 |

I also tried Before and After hooks by removing Given step with no luck.

I also tried before hooks, still it is coming to this loop for each row in the examples.

  @Before("@fileValidation")
    public void file_is_uploaded() throws Throwable {
        String fileName = "something.csv";
        processInputFile(fileName);
    }

    @After("@fileValidation")
    public void clear() {
        outputFileName = null;
    }

and in the feature file I have something like this:

@fileValidation
Scenario Outline: File Validation

Background: Read the uploaded file "something.csv"
Then response filename created should not match input filename "something.csv"
And reason for errors should be "<Reason>" with error code "<Error code>" for row with RequestId "<RequestId>"

Examples:

  | RequestId     |  Error code | Reason |     
  | 123           |   101       | Failure 1 |
  | 124           |   102       | Failure 1; Failure 2 |

No correct solution

OTHER TIPS

Running some steps (Background) before each set of scenarios or a Scenario Outline can also be achieved by creating a tagged @Before method and passing a Scenario object as parameter. Within the before method, execute your logic only if the scenario name is different as compared to the last scenario.

Below is a how you can do it:

Feature:Setup Data Given Customer logs in as System Admin

@BeforeMethodName
Scenario Outline: Verify ......... 1 
    When <Variable1> And <Variable2> 
    Then <Variable3>

Examples:
    | Variable1 | Variable2 | Variable3 |
    | A1            | B1          | C1        |
    | A2            | B2          | C2        |
    | A3        | B3          | C3        |
    | A4        | B4          | C4        |


@BeforeMethodName
Scenario Outline: Verify ......... 2 
    When <Variable1> And <Variable2> 
    Then <Variable3>

Examples:
    | Variable1 | Variable2 | Variable3 |
    | X1            | Y1          | Z1        |
    | X2            | Y2          | Z2        |
    | X3        | Y3          | Z3        |
    | X4        | Y4          | Z4        |

And define the @BeforeMethodName as below:

private static String scenarioName;

public className BeforeMethodName(Scenario scene) {

        if(!scene.getName().equals(scenarioName)) {

//            Implement your logic

        scenarioName = scene.getName()

        }

        return this;
    }

This way BeforeMethodName will be called before each scenario but will execute the logic only once per Scenario Outline.

Hooks should work / should have worked. Alternatively, you can set up a boolean flag and check it.

public class FileValidation {
...
...
private boolean fileOpened = false;

@Given("^a file is uploaded with name \"([^\"]*)\"$")
public void a_file_is_uploaded_with_name(String arg1) throws Throwable {
  if !(fileOpened) {
    processInputFile(...);
    fileOpened = true;
  }

 }
  ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top