Forum Discussion

max1965's avatar
max1965
Contributor
13 years ago

time difference

I write a groovy script to save the date when the test case was executed:

def date = new Date()
def formattedDate = date.format('yyyy-MM-dd HH:mm:ss')
log.info formattedDate
def props = testRunner.testCase.getTestStepByName( "Properties" )
props.setPropertyValue("xxxxxx", formattedDate);

Result: Tue Feb 26 15:41:18 CET 2013:INFO:2013-02-26 15:41:18

When I executed the test case, the test steps must be executed only if are passed 10 days from the previous execution.

How I can check the time difference between the two execution ?

4 Replies


  • baseDate = new Date()

    newDate = new Date()

    if(newDate >= (baseDate + 10)) {
    log.info '10 days passed do stuff here'
    }
  • Thanks; I put the feedback in the following groovy script:

    def distretto = context.expand( '${#Global#distretto}' )
    def Path_UDB = context.expand( '${#Global#Path_UDB}' )
    def testDataSet = []
    def failureList = []
    def fileName = "${Path_UDB}\\VDCRTG\\data_cap_${distretto}.txt"
    new File(fileName).eachLine { line -> testDataSet.add( line.split(",") ) }
    context.setProperty( "failureList", failureList )
    context.setProperty( "testDataSet", testDataSet )
    context.setProperty( "index", 0 )
    def testDataSet1 = context.getProperty("testDataSet")
    def index = context.getProperty("index")
    def testDataLine = testDataSet1[index]
    def props = testRunner.testCase.getTestStepByName( "Properties" )
    props.setPropertyValue("data_cap", testDataLine[0])
    newDate = new Date()
    log.info ("newDate = $newDate")
    baseDate = props.getPropertyValue("data_cap")
    log.info ("baseDate = $baseDate")
    if( newDate >= (baseDate + 10))
    {
    props.setPropertyValue("timer", "25000")
    log.info '10 days passed from previous CAP'
    f = new File("${Path_UDB}\\VDCRTG\\data_cap_${distretto}.txt")
    f.write("$newDate")
    }
    else
    {
    props.setPropertyValue("timer", "1000")
    }

    When I execute it, I have the following error: Wed Feb 27 11:15:31 CET 2013:ERROR:An error occured [java.lang.String cannot be cast to java.util.Date], see error log for details
    Wed Feb 27 11:15:31 CET 2013:ERROR:java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
    java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
    at java.util.Date.compareTo(Unknown Source)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.compareToWithEqualityCheck(DefaultTypeTransformation.java:570)
    at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.compareTo(DefaultTypeTransformation.java:525)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareTo(ScriptBytecodeAdapter.java:688)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.compareGreaterThanEqual(ScriptBytecodeAdapter.java:709)
    at Script14.run(Script14.groovy:19)
    at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:96)
    at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:149)
    at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:274)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
  • The error actually has the solution for you, see this "java.lang.String cannot be cast to java.util.Date"
    Properties in soapUI are stored as strings (correct me if i am wrong), all you have to do is transform your string to date.

    import
    //import this
    import java.text.SimpleDateFormat


    change


    baseDate = props.getPropertyValue("data_cap")


    to


    //get the string "data_cap" and parse it to date()
    //if you are not using hours, minutes and sconds, delete hh:mm:ss
    baseDate = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss").parse(props.getPropertyValue("data_cap"))




    your changed code

    //import this
    import java.text.SimpleDateFormat

    def distretto = context.expand( '${#Global#distretto}' )
    def Path_UDB = context.expand( '${#Global#Path_UDB}' )
    def testDataSet = []
    def failureList = []
    def fileName = "${Path_UDB}\\VDCRTG\\data_cap_${distretto}.txt"

    new File(fileName).eachLine { line -> testDataSet.add( line.split(",") ) }

    context.setProperty( "failureList", failureList )
    context.setProperty( "testDataSet", testDataSet )
    context.setProperty( "index", 0 )

    def testDataSet1 = context.getProperty("testDataSet")
    def index = context.getProperty("index")
    def testDataLine = testDataSet1[index]

    def props = testRunner.testCase.getTestStepByName( "Properties" )
    props.setPropertyValue("data_cap", testDataLine[0])
    newDate = new Date()
    log.info ("newDate = $newDate")

    //get the string "data_cap" and parse it to date()
    //if you are not using hours, minutes and sconds, delete hh:mm:ss
    baseDate = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss").parse(props.getPropertyValue("data_cap"))


    if( newDate >= (baseDate + 10)) {
    props.setPropertyValue("timer", "25000")
    log.info '10 days passed from previous CAP'
    f = new File("${Path_UDB}\\VDCRTG\\data_cap_${distretto}.txt")
    f.write("$newDate")
    } else {
    props.setPropertyValue("timer", "1000")
    }