Automation QA Testing Course Content

Three Different Ways on Cucumber Runner

 

Three Different Ways on Cucumber Runner



I have been working on Web automation projects. Generally, I prefer to use Cucumber due to providing behavior-driven development.
Every project needs a different approach for execution. We need a runner class for executing feature files, but which test framework is better to use with Cucumber? I have three different options, two with TestNG and one with JUnit. You can prefer as your project needs.
I’m listing from the simplest to the most complex ones.

  1. Cucumber Runner with JUnit
    The first example is being created by Cucumber-JUnit and JUnit dependencies. I had been using it for an API project because I didn’t need TestNG annotation to perform it.
package myTestRunners;
import io.cucumber.junit.Cucumber;
import io.cucumber.testng.CucumberOptions;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/functionalTests",
glue= {"myStepDefinitions" , "myHooks"},
tags = "@chrome",
plugin = { "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:",
"timeline:test-output-thread/",
"rerun:src/test/resources/failedrerun.txt"},
monochrome = true,
publish = true
)
public class TestRunnerWithJunit {
@BeforeClass
void beforeClass() {
}
@AfterClass
void afterClass() {
}
} ============================================================================= 2) Cucumber Runner with TestNG (AbstractTestNGCucumberTests)
This example is being created by Cucumber-TestNG and TestNG dependencies. Using the advantages of TestNG, Test XML files can be created and feature files can be performed. We can execute more than one runner class by creating the XML file as concurrently.
package myTestRunners;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;


@CucumberOptions(
        features = "src/test/resources/functionalTests",
        glue= {"myStepDefinitions" , "myHooks"},
        tags = "@chrome",
        plugin = { "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:",
                "timeline:test-output-thread/",
                "rerun:src/test/resources/failedrerun.txt"},
        monochrome = true,
        publish = true
)

public class TestRunner extends AbstractTestNGCucumberTests {

    @BeforeTest
    void beforeTest() {

    }

    @AfterTest
    void AfterTest() {

    }
}
===========================================================================
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite thread-count="2" name="Example Project" parallel="tests">

    <test name="TestRunner1">
        <classes>
            <class name="myTestRunners.TestRunner" />
        </classes>
    </test>
    <test name="TestRunner2">
        <classes>
            <class name="myTestRunners.TestRunner" />
        </classes>
    </test>

</suite>
=================================================================================
3) Cucumber Runner with TestNG (IRetryAnalyzer)
Sometimes one execution may not be enough for a test. IRetryAnalyzer provides repetitive execution. Now I’m working on Cucumber 6.11.0 and TestNG 7.5 versions. This runner class was created by using these versions. Please feel free to comment if you have any issues while applying your project! :)
package myTestRunners;

import io.cucumber.testng.*;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.annotations.*;
@CucumberOptions(
        features = "src/test/resources/functionalTests",
        glue= {"myStepDefinitions" , "myHooks"},
        tags = "@chrome",
        plugin = { "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:",
                "timeline:test-output-thread/",
                "rerun:src/test/resources/failedrerun.txt"},
        monochrome = true,
        publish = true
)
public class TestRunnerWithRetry implements IRetryAnalyzer {

    private TestNGCucumberRunner testNGCucumberRunner;
    private int count = 0;
    private static int maxTry = 3;

    @Override
    public boolean retry(ITestResult iTestResult) {
        if (!iTestResult.isSuccess()) {  ;
            if (count < maxTry) {
                count++;
                iTestResult.setStatus(ITestResult.FAILURE);
                return true;
            } else {
                iTestResult.setStatus(ITestResult.FAILURE);
            }
        } else {
            iTestResult.setStatus(ITestResult.SUCCESS);
        }
        return false;
    }

    @BeforeClass(alwaysRun = true)
    public void setUpClass() throws Exception {
        System.out.println("Before Scenario ****");
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
    }

    @Test(groups = "cucumber", description = "Runs Cucumber Scenarios",
            dataProvider = "scenarios",retryAnalyzer = TestRunnerWithRetry.class)
    public void scenario(PickleWrapper pickleEvent, FeatureWrapper cucumberFeature) {
        testNGCucumberRunner.runScenario(pickleEvent.getPickle());
    }

    @DataProvider
    public Object[][] scenarios() {
        return testNGCucumberRunner.provideScenarios();
    }

    @AfterClass(alwaysRun = true)
    public void tearDownClass() {
        System.out.println("After Scenario ****");
        testNGCucumberRunner.finish();
    }
}
============================================================================
Resources: Features example:
https://courgette-testing.com/bdd

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.