Search code examples
javaseleniumwebdrivercucumbergherkin

Selenium Webdriver: Automatically Repeating a successful scenario


I use Selenium Webdriver tests based on Java/Gherkin/Cucumber.

I have a test running that I want to run multiple times, without me having to restart the scenario several times.

The Gherkin script is something like this:

Given the user opens a browser
Then the user fills in the form
Then the user repeats the filling of the form *5* times 

This way, if I want 10 forms to be filled in, I can just replace the 5 with 10, press play, and grab a beer.

Is this at all possible or do I just have to re-run the scenario manually 5 times?


Solution

  • First you could simplify a bit your Gherkin scenario, like:

     Given the user opens a browser
    
     Then the user fills in the form 5 times
    

    Taking this scenario you then generate your methods and in the second method you can add a loop, like:

    [Then(@"the user fills in the form (.*) times")]

    public void ThenTheUserFillsInTheForm(int nrOfTimes)   
    {        
        for(int i = 0; i < nrOfTimes; i++)
        {
    
          //user fills in the form
    
        }
    }