RKS
RKS

Reputation: 21

Replace blank with date

String c = ${__timeShift(yyyy-MM-dd,,P10D,)}
log.info("Today date " + "${c}")
vars.put("DATE_PLUS_10", "${c}")
System.out.println("${c}");   ---- This is printing 2007
def response = prev.getResponseDataAsString()
log.info('Response Initial: ' + response)
def request = response.replaceAll('""', ${DATE_PLUS_10})
log.info('Response Massaged: ' + request)
vars.put('request', request)
//vars.putObject('request', 'request');
System.out.println(${request});

I need to add 10 days to current date and replace the null value with DATE_PLUS_10

2020-02-29 20:20:15,548 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, RESPONSE_STORE javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.$() is applicable for argument types: (Script2$_run_closure1) values: [Script2$_run_closure1@76fcaee5] Possible solutions: is(java.lang.Object), any(), get(java.lang.String), any(groovy.lang.Closure), use([Ljava.lang.Object;), wait() at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-all-2.4.13.jar:2.4.13] at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.13.jar:2.4.13] at javax.script.CompiledScript.eval(CompiledScript.java:92) ~[?:1.8.0_181]

Can someone pls tell me what is the issue in the line :

def request = response.replaceAll('""', ${DATE_PLUS_10})
String c = ${__timeShift(yyyy-MM-dd,,P10D,)} -

DATE_PLUS_10=2007 - this is what debug sampler displays..

Upvotes: 1

Views: 481

Answers (4)

Dmitri T
Dmitri T

Reputation: 168197

Deon't inline JMeter Functions or Variables into scripts, you can use "Parameters" section instead like:

enter image description here

or alternatively if you want "pure" Groovy code:

def newDate = new Date().plus(10).format('yyyy-MM-dd')

enter image description here

More information:

Upvotes: 0

RKS
RKS

Reputation: 21

I tried the below this beanshell preprocessor.

import java.text.SimpleDateFormat; 
    import java.util.Date; 

    Date date = new Date(); 
    date.setDate(date.getDate()+10); 
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
    // or: SimpleDateFormat df = new SimpleDateFormat("MM-dd-yyyy"); 
    String formattedDate = df.format(date); 
    vars.put("FUTUREDATE",formattedDate); 

And used the JSR post processor

def mydate =vars.get("FUTUREDATE");
log.info("my date" + mydate)
def response = prev.getResponseDataAsString()
log.info('Response Initial: ' + response)
def request = response.replaceAll('""', mydate)
log.info('Response Massaged: ' + request)
vars.put('request', request)
//vars.putObject('request', 'request');
System.out.println(${request});

Issue resolved now.

Upvotes: 0

Santosh sanwal
Santosh sanwal

Reputation: 154

you can use a global or user variable and after that you can write

def DATE_PLUS_10=vars.get("DATE_PLUS_10")
log.info(DATE_PLUS_10)
def response = "2020-05-01"//prev.getResponseDataAsString()
log.info('Response Initial: ' + response)
def request = response.replaceAll('""', DATE_PLUS_10)
log.info('Response Massaged: ' + request)

As mentioned in previous answer you can not use ${} inside JSR223 processor.

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58882

Don't use ${} syntax in JSR223 script,

You can add a PreProcessor User Parameters

Allows the user to specify values for User Variables specific to individual threads

Add a variable as DATE_PLUS_10 with value:

  ${__timeShift(yyyy-MM-dd,,P10D,)}

Then use it in script

 String c = vars.get("DATE_PLUS_10");

Upvotes: 1

Related Questions