0

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?

Manasi
  • 717
  • 8
  • 18
  • 30
  • Having features that depend on other features running before it is generally bad practise. Try to capture all the state required in your Given's so that they can run in any order. – jedifans Aug 19 '16 at 06:38
  • Okay. But is there any parameter which controls this flow? Because I am not getting why sometimes it runs sequentially and sometimes parallel. – Manasi Aug 19 '16 at 07:03
  • Any reason why you are extending the AbstractTestNG..... class as well as implementing the runCukes method. Just extend the class and leave out the rest. Have a look at this - http://sahajamit.github.io/Cucumber-JVM-with-TestNG/. Or the famous calc example - https://github.com/cucumber/cucumber-jvm/tree/master/examples/java-calculator-testng/src/test/java/cucumber/examples/java/calculator – Grasshopper Aug 19 '16 at 07:27
  • I removed runCukes method but execution flow is yet not fixed. – Manasi Aug 19 '16 at 10:08

1 Answers1

1
  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
    
Mikhail
  • 665
  • 4
  • 16