Ask a Question

Groovy Script to identify a file timestamp

Frequent Contributor

Groovy Script to identify a file timestamp

Just sharing this groovy script I am using to disable a "DataCollection" step based on a filetime stamp.


Use case: 

    • Identify when datasource file is modified
    • If datasource file is already generated then skip/disable datacollection step which in the end writes data to datasource file
    • why to disable - if you want to run the same API tests (but different versions say due to refactoring) against same dataset
    • Below is the sample test case structure
    • TestSteps.PNG

DataSource check script is shown below

import java.text.DateFormat
import java.text.SimpleDateFormat

//## Get test step name //
def currentStepInd = context.currentStepIndex
def TestStepName = testRunner.testCase.getTestStepAt(currentStepInd).name "------------------------------------------------" "Running  $TestStepName..."

//## Get current Date  ##//
def CurrentDate = new Date() "Current Date is $CurrentDate..."

// Get datasource file //

def DataSourceFile =  context.expand('${projectDir}') + "\\DataSource.txt"
File DataFile = new File(DataSourceFile) "DataSource File is: $DataFile"
def fileDate    

if (DataFile.exists()) {
            // Get the last modification information.
            Long lastModified = DataFile.lastModified()

            // Create a new date object and pass last modified
            fileDate = new Date(lastModified)
                 //fileDate = sdf.format(fileDate)
        "File modified time is: $fileDate"

//## To find Date Diff ##//
def diff 
use(groovy.time.TimeCategory) {
                               diff = (CurrentDate - fileDate).days

//## Skip DataCollection if DataSource is older than today ##//
if(diff == 0) {
	         //## disable teststep to skip data collection ##//
  "Disabling testStep DataCollection..."
    	         testRunner.testCase.getTestStepByName( "DataCollection" ).setDisabled(true)
	 //## enable teststep to run data collection ##//  "Enabling testStep DataCollection..."
	 testRunner.testCase.getTestStepByName( "DataCollection" ).setDisabled(false)
} "Finished $TestStepName..." "------------------------------------------------"



SmartBear Alumni (Retired)

Thank you for sharing the script with us!

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Showing results for 
Search instead for 
Did you mean: