0

I have two cucumber runner classes. One of them is a JUnit based runner and the other is TestNG based. The Junit one is the first runner I implemented for API tests and runs as expected via maven. I added a web test to the project and used TestNG to help me achieve cross-browser testing. As a result, I created an additional cucumber runner (TestNG based). The web test runs as expected when I run it via the testng.xml file. No problems at this point.

JUnit Cucumber Runner:

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@IncludeTags("api")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "stepdefinitions")
@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "json:target/surefire-reports/cucumber.json")


public class RunCucumberTest {
}

TestNG Cucumber Runner

@CucumberOptions(
        plugin = {"pretty", "html:target/surefire-reports/cucumber",
                "json:target/surefire-reports/cucumberOriginal.json"},
        glue = {"stepdefinitions"},
        tags = "@web-1",
        features = {"src/test/resources/features/web.feature"})
public class RunCucumberNGTest extends AbstractTestNGCucumberTests {

public final static ThreadLocal<String> BROWSER = new ThreadLocal<>();

  @BeforeTest
  @Parameters({"Browser"})
  public void defineBrowser(String browser) {
    //put browser value to thread-safe container
    RunCucumberNGTest.BROWSER.set(browser);
    System.out.println(browser);
  }

}

TestNG config:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite" parallel="tests" thread-count="5">

    <test name="Chrome Test">
        <parameter name="Browser" value="CHROME"/>
        <classes>
            <class name="config.RunCucumberNGTest"/>
        </classes>
    </test>
    <test name="Firefox Test">
        <parameter name="Browser" value="FF"/>
        <classes>
            <class name="config.RunCucumberNGTest"/>
        </classes>
    </test>
</suite>

The JUnit runner is embedded in POM via the maven surefire plugin, so if I do a maven command e.g. mvn test, only the JUnit runner is executed, as expected.

Now, to execute both runners together, I added the TestNG runner in the maven surefire plugin using <suiteXmlFiles> tags. At runtime, only the Junit runner is executed. No failure. The testng.xml wasn't picked up.

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <properties>
                        <configurationParameters>
                            cucumber.junit-platform.naming-strategy=long
                        </configurationParameters>
                    </properties>

                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>

                </configuration>
            </plugin>
            <plugin>
                <groupId>net.masterthought</groupId>
                <artifactId>maven-cucumber-reporting</artifactId>
                <version>5.6.2</version>
                <executions>
                    <execution>
                        <id>execution</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <projectName>AutomationFrameworkSkeleton</projectName>
                            <!-- optional, per documentation set this to "true" to bypass generation of Cucumber Reports entirely, defaults to false if not specified -->
                            <skip>false</skip>
                            <outputDirectory>${project.build.directory}
                            </outputDirectory>
                            <inputDirectory>${project.build.directory}
                            </inputDirectory>
                            <jsonFiles>
                                <param>**/*.json</param>
                            </jsonFiles>

                            <!-- optional, set true to group features by its Ids -->
                            <mergeFeaturesById>false</mergeFeaturesById>
                            <!-- optional, set true to get a final report with latest results of the same test from different test runs -->
                            <mergeFeaturesWithRetest>false
                            </mergeFeaturesWithRetest>
                            <!-- optional, set true to fail build on test failures -->
                            <checkBuildResult>false</checkBuildResult>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Is there a way to run cucumber Junit and TestNG classes together with maven? I guess there is. I may have added the testng.xml file to POM wrongly.

The man
  • 129
  • 16

1 Answers1

1

From your issue , it seems like you are trying to run API and cross browser web tests together. If you do not have to use two runners for some other reason then cross browser config may be achieved this way. I tested it with docker containers but it may be used without docker

In a DriverFactory class do this. This class should be with your stepdefs as this is utilizing @Before

public class DriverFactory {

    public static Collection tagList;

    @Before
        public void getScenarioTags(Scenario scenario) {
            tagList =  scenario.getSourceTagNames();            
         }
         
    
        public RemoteWebDriver getManager() throws MalformedURLException , IOException {          
        if (tagList.contains("@firefox")){
              //implement FirefoxManager class to return firefox instance . class may be outside your stepdefs
               return new FirefoxManager().getDriver();
                }
          else if (tagList.contains("@chrome")){
          //implement ChromeManager class to return chrome instance. class may be outside your stepdefs
              return new ChromeManager().getDriver();
             }
            else{
                return null;
              }
        } 
                    
}

tag the scenarios accordingly in feature files and include them in @cucumberOptions. I include tags this way tags = {"@firefox or @chrome"},

I am running this setup for parallel tests at scenario level running on docker instance. However , I am using courgette-jvm and testNG . If you like to get more info on courgette-jvm with testNG , have a look at this question. Also look at courgette-jvm

user1207289
  • 3,060
  • 6
  • 30
  • 66