Search code examples
selenium-webdrivercucumber-javatestng-eclipse

Scenarios from cucumber .feature file are not running in proper sequence


I have Cucumber feature file with 2 scenarios, e.g. please find below sample file.

@RunFeature
Feature: Sanity Testing of scenarios

@LoginFeature
Scenario: Test xyz feature
    Given The user is on login page
    When User enters credentials
    And clicks on login button
    Then homepage should be displayed

@InfoFeature
Scenario: Test abc feature
    Given The user is on home page
    When User enters employee name in textbox
    And clicks on get details button
    Then Employee details are displayed

I am trying to run this feature file using TestNG,

package TestNGClass;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import cucumber.api.testng.TestNGCucumberRunner;

@Test(groups="cucumber")

@CucumberOptions(
        features ="src/main/resources",
        glue="stepDefinitions",
        tags="@RunFeature")

public class TestNGRunner extends AbstractTestNGCucumberTests{


    @Test(groups="cucumber",description="Runs Cucumber Features")
    public void run_Cukes()throws IOException{
        //new TestNGCucumberRunner(getClass()).runCukes();
    }
}

But I have observed that sometimes it runs both the scenarios in parallel and sometimes in sequential mode. I am trying to run the scenarios in sequential mode. Could anyone tell me what needs to be added in my testng runner class?


Solution

    1. No need to mark your cukes runner as @Test, extending AbstractTestNGCucumberTests is enough
    2. No need to define tests in this class, use step classes and feature files
    3. If you need certain preconditions to happen consider either using @Before annotations in your step classes OR use keyword Background in your feature file. Sentences described in Background section will be executed for every single scenario in that feature file. Like this:

      Background: Test xyz feature
      Given The user is on login page
      When User enters credentials
      And clicks on login button
      Then homepage should be displayed
      
      Scenario: Test abc feature
      Given The user is on home page
      When User enters employee name in textbox
      And clicks on get details button
      Then Employee details are displayed