Knowledge Base Article

Groovy Script to identify a file timestamp

Question

How to identify a file timestamp?

 

Answer

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

DataSource check script is shown below

import com.eviware.soapui.support.GroovyUtils
import java.text.DateFormat
import java.text.SimpleDateFormat

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

log.info "------------------------------------------------"
log.info "Running  $TestStepName..."

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


// Get datasource file //

def DataSourceFile =  context.expand('${projectDir}') + "\\DataSource.txt"
File DataFile = new File(DataSourceFile) 
log.info "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)
                 log.info "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 ##//
	         log.info  "Disabling testStep DataCollection..."
    	         testRunner.testCase.getTestStepByName( "DataCollection" ).setDisabled(true)
}else{
	 //## enable teststep to run data collection ##//
	 log.info  "Enabling testStep DataCollection..."
	 testRunner.testCase.getTestStepByName( "DataCollection" ).setDisabled(false)
      
}

log.info "Finished $TestStepName..."
log.info "------------------------------------------------"

thanks!

 

Published 4 years ago
Version 1.0
No CommentsBe the first to comment