[Cucumber] Run Feature File with Tags

Here my customkeyword :

package cucumber

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
import internal.GlobalVariable

import java.text.MessageFormat

import org.apache.commons.lang3.StringUtils
import org.junit.runner.Computer
import org.junit.runner.JUnitCore
import org.junit.runner.Result
import org.junit.runner.notification.Failure

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.cucumber.keyword.CucumberReporter
import com.kms.katalon.core.cucumber.keyword.CucumberRunnerResult
import com.kms.katalon.core.cucumber.keyword.internal.CucumberRunnerResultImpl
import com.kms.katalon.core.keyword.BuiltinKeywords
import com.kms.katalon.core.keyword.internal.KeywordMain
import com.kms.katalon.core.logging.KeywordLogger
import com.kms.katalon.core.model.RunningMode
import cucumber.api.cli.Main
import groovy.transform.CompileStatic
import com.kms.katalon.core.util.internal.PathUtil

//public class CustomRun {
public class CustomRun extends BuiltinKeywords {

private static final KeywordLogger logger = KeywordLogger.getInstance(CustomRun.class)

/**

  • Runs the given Feature file with featureId by invoking

  • {@link cucumber.api.cli.Main#run(String, ClassLoader)}.

  • The generated reports will be extracted in the current report folder with the following path: <report_folder>/cucumber_report/<current_time_stamp>

  • @param relativeFilePath

  • relativeFilePath of Feature file

  • @param flowControl

  • an instance {@link FailureHandling} that controls the running flow

  • @return

  • an instance of {@link CucumberRunnerResult} that includes status of keyword and report folder location.

  • @since 5.7

  • @see CucumberRunnerResult
    */
    @Keyword
    public static CucumberRunnerResult runFeatureFileWithTags(String relativeFilePath, FailureHandling flowControl, String tagExpression) {
    return KeywordMain.runKeyword({
    if (StringUtils.isEmpty(relativeFilePath)) {
    throw new IllegalArgumentException(“featureRelativeFilePath param must not be null or empty”)
    }
    String reportDir = RunConfiguration.getReportFolder() + “/Cucumber_Report/” + System.currentTimeMillis()
    String projectDir = RunConfiguration.getProjectDir()
    RunningMode runningMode = RunConfiguration.getRunningMode()

     logger.logInfo(
     		MessageFormat.format("Starting run keyword runFeatureFileWithTags: '{0}' and extract report to folder: '{1}'...", reportDir))
     String[] argv = [
     	"-g",
     	"",
     	projectDir + "/" + relativeFilePath,
     	"--strict",
     	"--plugin",
     	"pretty",
     	"--plugin",
     	"html:Reports/Cucumber_Report/cucumber.html",
     	"--plugin",
     	"json:Reports/Cucumber_Report/cucumber.json",
     	"--plugin",
     	"junit:Reports/Cucumber_Report/cucumber.xml",
     	"--plugin",
     	CucumberReporter.class.getName(),
     	"--tags",
     	tagExpression
     	//"@Rechercher and not @Adresse and not @Identifier"
     ]
     if (runningMode == RunningMode.CONSOLE) {
     	argv = argv + ["--monochrome"]
     }
     boolean runSuccess = Main.run(argv, CustomRun.class.getClassLoader()) == 0
     CucumberRunnerResultImpl cucumberResult = new CucumberRunnerResultImpl(
     		runSuccess ? 'passed' : 'failed', reportDir)
     if (runSuccess) {
     	logger.logPassed(MessageFormat.format("Feature file: ''{0}'' was passed", relativeFilePath))
     } else {
     	KeywordMain.stepFailed(MessageFormat.format("Feature file ''{0}'' was failed", relativeFilePath), flowControl)
     }
     return cucumberResult
    

    }, flowControl, “Keyword runFeatureFileWithTags was failed”)
    }

/**

  • Runs the given Feature file with featureId by invoking
  • {@link cucumber.api.cli.Main#run(String, ClassLoader)}
  • @param relativeFilePath
  • relativeFilePath of Feature file
  • @return
  • an instance of {@link CucumberRunnerResult} that includes status of keyword and report folder location.
  • @since 5.7
    */
    @Keyword
    public static boolean runFeatureFileWithTags(String relativeFilePath, String tagExpression) {
    return runFeatureFileWithTags(relativeFilePath, RunConfiguration.getDefaultFailureHandling(), tagExpression)
    }

/**

  • Runs the given Feature folder and its nested sub-folder with folderRelativePath

  • by invoking {@link cucumber.api.cli.Main#run(String, ClassLoader)}.

  • The generated reports will be extracted in the current report folder with the following path: <report_folder>/cucumber_report/<current_time_stamp>

  • @param folderRelativePath

  • folder relative path that starts from the current project location

  • @param flowControl

  • an instance {@link FailureHandling} that controls the running flow

  • @return

  • an instance of {@link CucumberRunnerResult} that includes status of keyword and report folder location.

  • @since 5.7
    */
    @Keyword
    public static boolean runFeatureFolderWithTags(String folderRelativePath, FailureHandling flowControl, String tagExpression) {
    return KeywordMain.runKeyword({
    if (StringUtils.isEmpty(folderRelativePath)) {
    throw new IllegalArgumentException(“folderRelativePath param must not be null”)
    }
    String reportDir = RunConfiguration.getReportFolder() + “/cucumber_report/” + System.currentTimeMillis()
    String projectDir = RunConfiguration.getProjectDir()
    RunningMode runningMode = RunConfiguration.getRunningMode()

     logger.logInfo(
     		MessageFormat.format("Starting run keyword>runFeatureFolderWithTags: ''{0}'' and extract report to folder: ''{1}''...",
     		reportDir))
     String[] argv = [
     	"-g",
     	"",
     	projectDir + "/" + folderRelativePath,
     	"--strict",
     	"--plugin",
     	"pretty",
     	"--plugin",
     	"html:Reports/Cucumber_Report/cucumber.html",
     	"--plugin",
     	"json:Reports/Cucumber_Report/cucumber.json",
     	"--plugin",
     	"junit:Reports/Cucumber_Report/cucumber.xml",
     	"--tags",
     	tagExpression
     ]
     if (runningMode == RunningMode.CONSOLE) {
     	argv = argv + ["--monochrome"]
     }
    
     boolean runSuccess = Main.run(argv, CustomRun.class.getClassLoader()) == 0
     CucumberRunnerResultImpl cucumberResult = new CucumberRunnerResultImpl(
     		runSuccess ? 'passed' : 'failed', reportDir)
     if (runSuccess) {
     	logger.logPassed(MessageFormat.format("All feature files in ''{0}'' were passed", folderRelativePath))
     } else {
     	KeywordMain.stepFailed(MessageFormat.format("Run feature folder ''{0}'' failed", folderRelativePath))
     }
     return cucumberResult
    

    }, flowControl, “Keyword runFeatureFolderWithTags was failed”)
    }

/**

  • Runs the given Feature folder and its nested sub-folder with folderRelativePath
  • by invoking {@link cucumber.api.cli.Main#run(String, ClassLoader)}
  • @param folderRelativePath
  • folder relative path that starts from current project location
  • @return
  • an instance of {@link CucumberRunnerResult} that includes status of keyword and report folder location.
  • @since 5.7
    */
    @Keyword
    public static boolean runFeatureFolderWithTags(String folderRelativePath, String tagExpression) {
    return runFeatureFolderWithTags(folderRelativePath, RunConfiguration.getDefaultFailureHandling(), tagExpression)
    }

/**

  • Runs the given cucumberRunnerClass that is annotated with {@link Cucumber} runner by invoke JUnit
  • runner.
  • @param cucumberRunnerClass
  • a class that is annotated with {@link Cucumber} runner.
  • Example of cucumberRunnerClass:
    • Example #1: Run all Feature files in Include/features Folder
    • import org.junit.runner.RunWith;
    • import cucumber.api.CucumberOptions;
    • import cucumber.api.junit.Cucumber;
    • @RunWith(Cucumber.class)
    • @CucumberOptions(features = “Include/features”, glue = “”)
    • public class MyCucumberRunner {}
    • Example #2: Run all Feature files in a specified file/folder
    • import org.junit.runner.RunWith;
    • import cucumber.api.CucumberOptions;
    • import cucumber.api.junit.Cucumber;
    • @RunWith(Cucumber.class)
    • @CucumberOptions(features = “Your_Folder_Or_File_Path”, glue = “”)
    • public class MyCucumberRunner {}
    • Example #3: Run all Feature files in a specified file/folder, generate JUnit Cucumber report with XML pretty
    • format,
    • and copy to a specified folder
    • import org.junit.runner.RunWith;
    • import cucumber.api.CucumberOptions;
    • import cucumber.api.junit.Cucumber;
    • @RunWith(Cucumber.class)
    • @CucumberOptions(features=“Your_Folder_Path”, glue=“”, plugin = [“pretty”,
    •                  "junit:Folder_Name/cucumber.xml"])
      
    • public class MyCucumberRunner {
    • }
    • Example #4: Run all Feature files in a specified file/folder, generate multi Cucumber reports with XML, JSON,
    • HTML pretty format,
    • and copy to a specified folder
    • import org.junit.runner.RunWith;
    • import cucumber.api.CucumberOptions;
    • import cucumber.api.junit.Cucumber;
    • @RunWith(Cucumber.class)
    • @CucumberOptions(features=“Your_Folder_Path”, glue=“”, plugin = [“pretty”,
    •                  "junit:Folder_Name/cucumber.xml",
      
    •                  "html:Folder_Name",
      
    •                  "json:Folder_Name/cucumber.json"])
      
    • public class MyCucumberRunner {
    • }
    • @param flowControl
    • an instance {@link FailureHandling} that controls the running flow
    • @return
    • an instance of {@link CucumberRunnerResult} that includes status of keyword and JUnit Runner result.
    • @since 5.7
      */
      @Keyword
      public static CucumberRunnerResult runWithCucumberRunner(Class cucumberRunnerClass, FailureHandling flowControl) {
      return KeywordMain.runKeyword({
      JUnitCore core = new JUnitCore()
      Computer computer = new Computer()
      Result result = core.run(computer, cucumberRunnerClass)
      boolean runSuccess = result.wasSuccessful()
      CucumberRunnerResultImpl cucumberResult = new CucumberRunnerResultImpl(
      runSuccess ? ‘passed’ : ‘failed’, ‘’, result)
      if (runSuccess) {
      logger.logPassed(MessageFormat.format(“Run with ‘’{0}‘’ was passed”, cucumberRunnerClass.getName()))
      } else {
      List failuresDescriptions =
      for (Failure failure : result.getFailures()) {
      failuresDescriptions.add(failure.getMessage())
      }
      KeywordMain.stepFailed(
      MessageFormat.format(“These following reason:\n {0}”, failuresDescriptions))
      }
      return cucumberResult
      }, flowControl, “Keyword runWithCucumberRunner was failed”)
      }

    /**

    • Runs the given cucumberRunnerClass that is annotated with {@link Cucumber} runner by invoke JUnit
    • runner.
    • @param cucumberRunnerClass
    • a class that is annotated with {@link Cucumber} runner.
    • Example of cucumberRunnerClass:
      • Example #1: Run all Feature files in Include/features Folder
      • import org.junit.runner.RunWith;
      • import cucumber.api.CucumberOptions;
      • import cucumber.api.junit.Cucumber;
      • @RunWith(Cucumber.class)
      • @CucumberOptions(features = “Include/features”, glue = “”)
      • public class MyCucumberRunner {}
      • Example #2: Run all Feature files in a specified file/folder
      • import org.junit.runner.RunWith;
      • import cucumber.api.CucumberOptions;
      • import cucumber.api.junit.Cucumber;
      • @RunWith(Cucumber.class)
      • @CucumberOptions(features = “Your_Folder_Or_File_Path”, glue = “”)
      • public class MyCucumberRunner {}
      • Example #3: Run all Feature files in a specified file/folder, generate JUnit Cucumber report with XML pretty
      • format,
      • and copy to a specified folder
      • import org.junit.runner.RunWith;
      • import cucumber.api.CucumberOptions;
      • import cucumber.api.junit.Cucumber;
      • @RunWith(Cucumber.class)
      • @CucumberOptions(features=“Your_Folder_Path”, glue=“”, plugin = [“pretty”,
      •                  "junit:Folder_Name/cucumber.xml"])
        
      • public class MyCucumberRunner {
      • }
      • Example #4: Run all Feature files in a specified file/folder, generate multi Cucumber reports with XML, JSON,
      • HTML pretty format,
      • and copy to a specified folder
      • import org.junit.runner.RunWith;
      • import cucumber.api.CucumberOptions;
      • import cucumber.api.junit.Cucumber;
      • @RunWith(Cucumber.class)
      • @CucumberOptions(features=“Your_Folder_Path”, glue=“”, plugin = [“pretty”,
      •                  "junit:Folder_Name/cucumber.xml",
        
      •                  "html:Folder_Name",
        
      •                  "json:Folder_Name/cucumber.json"])
        
      • public class MyCucumberRunner {
      • }
      • an instance of {@link CucumberRunnerResult} that includes status of keyword and JUnit Runner result.
      • @since 5.7
        */
        @Keyword
        public static CucumberRunnerResult runWithCucumberRunner(Class cucumberRunnerClass) {
        return runWithCucumberRunner(cucumberRunnerClass, RunConfiguration.getDefaultFailureHandling())
        }

      }