How can i write a customized TestNGCucumberRunner with the latest io.cucumber.cucumber-testng version 4.2.6

我与影子孤独终老i 提交于 2019-12-13 04:36:08

问题


I am trying to write a custom TestNGCucumberRunner (for the latest version cucumber 4.2.6) where I can filter the list of cucumberfeatures based on runtime arguments, in the getFeatures() method.

All the examples online are explained with info.cukes 1.2.5 version, where all the dependent classes and methods were public

I have never written a testrunner before. Can any one help please?


回答1:


First - Update POM.xml with correct set of io.cucumber dependencies as per v 4.2.6

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.2.6</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>4.2.6</version>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>4.2.6</version>
</dependency>

Second - Customize TestNGRunner class as per your framework need

package com.jacksparrow.automation.suite.runner;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import com.jacksparrow.automation.steps_definitions.functional.BaseSteps;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(features = "classpath:features/functional/",
                     glue = {"com.jacksparrow.automation.steps_definitions.functional" },
                   plugin = { "pretty","json:target/cucumber-json/cucumber.json",
                            "junit:target/cucumber-reports/Cucumber.xml", "html:target/cucumber-reports"},
                   tags = { "@BAMS_Submitted_State_Guest_User" },
                   junit ={ "--step-notifications"},
                   strict = false,
                   dryRun = false,
               monochrome = true)

public class RunCukeTest extends Hooks {

} 

Third - Implement Hooks.java

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import cucumber.api.testng.AbstractTestNGCucumberTests;

public class Hooks extends AbstractTestNGCucumberTests {

    @Parameters({ "browser" })
    @BeforeTest
    public void setUpScenario(String browser){
        BaseSteps.getInstance().getBrowserInstantiation(browser);
    }
}

Note - I have not implemented this way. But as per my best knowledge, it may work. Please check and share your experience.

Fourth - Update TestNG.xml under /src/test/resources/ as per your TestNGRunner Class and framework need.

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

    <test thread-count="1" name="Test" parallel="tests">
        <parameter name="browser" value="chrome" />
        <classes>
            <class
                name="com.jacksparrow.automation.suite.runner.RunCukeTest" />
        </classes>
    </test>
</suite>

Fifth - You shall be all set to run automation suite using TestNG in any of the following ways

 -    Run TestNG.xml directly from IDE 
 -    From CMD - mvn test -Dsurefire.suiteXmlFiles=src/test/resources/testng.xml
 -    From POM.xml - Using Surefire Plugin

<profiles>
   <profile>
      <id>selenium-tests</id>
      <build>
         <plugins>
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>3.0.0-M3</version>
               <configuration>
                  <suiteXmlFiles>
                     <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                  </suiteXmlFiles>
               </configuration>
            </plugin>     
         </plugins>
      </build>
   </profile>
</profiles>


来源:https://stackoverflow.com/questions/55831733/how-can-i-write-a-customized-testngcucumberrunner-with-the-latest-io-cucumber-cu

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!