Controlling Cucumber Tests By Driver Script

I will Show you how to  filter and run the cucumber tests by using the master driver script.We will use the following components for this as below:

1).YML based Configuration File
  Priority : p1


      ResultsPath : "D:\\TestProject\\Results"
      FeaturesFolderPath : "D:\\TestProject"
      ResultFormat : "html"

This file will help us in providing the following info:
a).Tag Filter "p1" by which we can execute only high priority test scenarios.
b).Results Path Folder
c).Features folder path
d).Desired Result Format

2).Master Driver Script(Driver.rb)
#Importing the required libraries
require 'yaml'
require 'FileUtils'

#Reading the YML config file and loading the variables and creating the timestamp to suffix results
time ="%Y_%m_%d_%H_%M%S")
configData = YAML.load_file "config.yml"
featureFilePath = configData["ApplicationData"]["FeaturesFolderPath"]
opFileFormat = configData["ApplicationData"]["ResultFormat"]
resBaseFolder = configData["ApplicationData"]["ResultsPath"] + "\\" + "ExecutionStatus_" + time

#Reading the config file and parsing the tags to filter the Features/Scenarios for execution                  def getTagsFromFilters(configData)
    strPriority = configData["env_1"]["Priority"]
    if strPriority.to_s.strip.length > 0
        if strPriority.include? ","
            arrVals = strPriority.split(",")           
            arrVals.each do |val|
                strPriority = strPriority.gsub val,"@" + val
            strRetVal =  " --tags " + strPriority + strRetVal.to_s
            strRetVal =  " --tags " + strPriority.gsub(strPriority,"@" + strPriority) + strRetVal.to_s
    return strRetVal

#Creating the cucumber command by concatenating the all config variables
      strExecutionCmd = "cucumber " 
      strExecutionCmd = strExecutionCmd + " --out " +  resBaseFolder +  "\\Test_Results"
      strExecutionCmd = strExecutionCmd + " --format " + opFileFormat
      strExecutionCmd = strExecutionCmd + "  SCREENSHOT_PATH=" + resBaseFolder + "\\"
      strTags = getTagsFromFilters(configData)
      strExecutionCmd = strExecutionCmd + strTags
      print strExecutionCmd + "\n"

This driver script can be directly executed from command prompt:

C:\ProjectFolder>ruby Driver.rb

or better can scheduled through a batch file and run automatically as required.


Popular posts from this blog

Testing Webservice with SoapUI Having Windows Authentication

How to Re-run Failed Scenarios in Cucumber

Data Driven Testing Using Selenium -WebDriver in Python