I want to to set Property value in SOAPUI with current time + 5 minutes
i tried this:
import groovy.time.*
import org.codehaus.groovy.runtime.TimeCategory
import groovy.time.TimeCategory
import java.text.SimpleDateFormat
currentDate = new Date()
use( TimeCategory ) {
after30Mins = date + 30.minutes
}
testRunner.testCase.testSuite.project.setPropertyValue( "SendHour",after30Mins)
log.info (after30Mins)
I got this error:
groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.WsdlProject.setPropertyValue() is applicable for argument types: (java.lang.String, java.util.Date) values: [SendHour, Sun Jun 07 19:37:52 EDT 2015] Possible solutions: setPropertyValue(java.lang.String, java.lang.String), getPropertyValue(java.lang.String) error at line: 10
Any help please, Thank you
As the error message says, the method setPropertyValue takes 2 parameters, both od them String. So the solution is to cast the after3OMins variable to String. In Groovy, Date implements the format method that makes casting to String really easy. In our case it could look like
testRunner.testCase.testSuite.project.setPropertyValue( "SendHour",
after30Mins.format("yyyy-MM-dd'T'HH:mm:ssZ"))
Related
How can I parse
18 January 2022, 14:50 GMT-5
as a timezone aware datetime.
pytz.timezone('GMT-5')
fails.
It appears I may need to parse the GMT part, and manually apply the 5 hours offset post parsing?
Hmm How about maybe:
import re
import datetime
foo = "18 January 2022, 14:50 GMT-5"
bar = re.sub(r"[+-]\d+$", lambda m: "{:05d}".format(100 * int(m.group())), foo)
print(datetime.datetime.strptime(bar, "%d %B %Y, %H:%M %Z%z" ))
I think that gives you:
2022-01-18 14:50:00-05:00
How to convert a given date in yyyy-MM-dd HH:mm:ss.SSS format to yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format in groovy
For example, the given date is 2019-03-18 16:20:05.6401383. I want it to converted to 2019-03-18T16:20:05.6401383Z
This is the code Used:
def date = format1.parse("2019-03-18 16:20:05.6401383");
String settledAt = format2.format(date)
log.info ">>> "+*date*+" "+*settledAt*
The result, where the date is getting changed somehow: Mon Mar 18 18:06:46 EDT 2019 & 2019-03-18T18:06:46.383Z
Thanks in advance for all the answers.
If you're on Java 8+ and Groovy 2.5+, I would use the new Date/Time API:
import java.time.*
def date = LocalDateTime.parse('2019-03-18 16:20:05.6401383', 'yyyy-MM-dd HH:mm:ss.nnnnnnn')
String settledAt = date.format(/yyyy-MM-dd'T'HH:mm:ss.nnnnnnn'Z'/)
This is presuming the input date has a "Zulu" time zone.
it's a feature of java
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS","2019-03-18 16:20:05.6401383")
returns
Mon Mar 18 18:06:46 EET 2019
the problem that java handles only milliseconds SSS (3 digits after seconds)
but you are providing 7 digits for milliseconds 6401383
as workaround remove extra digits with regexp:
def sdate1 = "2019-03-18 16:20:05.6401383"
sdate1 = sdate1.replaceAll( /\d{3}(\d*)$/, '$1') //keep only 3 digits at the end
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS",sdate1)
def sdate2 = date.format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
I know there are lots of Q&As to extract datetime from string, such as dateutil.parser, to extract datetime from a string
import dateutil.parser as dparser
dparser.parse('something sep 28 2017 something',fuzzy=True).date()
output: datetime.date(2017, 9, 28)
but my question is how to know which part of string results this extraction, e.g. i want a function that also returns me 'sep 28 2017'
datetime, datetime_str = get_date_str('something sep 28 2017 something')
outputs: datetime.date(2017, 9, 28), 'sep 28 2017'
any clue or any direction that i can search around?
Extend to the discussion with #Paul and following the solution from #alecxe, I have proposed the following solution, which works on a number of testing cases, I've made the problem slight challenger:
Step 1: get excluded tokens
import dateutil.parser as dparser
ostr = 'something sep 28 2017 something abcd'
_, excl_str = dparser.parse(ostr,fuzzy_with_tokens=True)
gives outputs of:
excl_str: ('something ', ' ', 'something abcd')
Step 2 : rank tokens by length
excl_str = list(excl_str)
excl_str.sort(reverse=True,key = len)
gives a sorted token list:
excl_str: ['something abcd', 'something ', ' ']
Step 3: delete tokens and ignore space element
for i in excl_str:
if i != ' ':
ostr = ostr.replace(i,'')
return ostr
gives a final output
ostr: 'sep 28 2017 '
Note: step 2 is required, because it will cause problem if any shorter token a subset of longer ones. e.g., in this case, if deletion follows an order of ('something ', ' ', 'something abcd'), the replacement process will remove something from something abcd, and abcd will never get deleted, ends up with 'sep 28 2017 abcd'
Interesting problem! There is no direct way to get the parsed out date string out of the bigger string with dateutil. The problem is that dateutil parser does not even have this string available as an intermediate result as it really builds parts of the future datetime object on the fly and character by character (source).
It, though, also collects a list of skipped tokens which is probably your best bet. As this list is ordered, you can loop over the tokens and replace the first occurrence of the token:
from dateutil import parser
s = 'something sep 28 2017 something'
parsed_datetime, tokens = parser.parse(s, fuzzy_with_tokens=True)
for token in tokens:
s = s.replace(token.lstrip(), "", 1)
print(s) # prints "sep 28 2017"
I am though not 100% sure if this would work in all the possible cases, especially, with the different whitespace characters (notice how I had to workaround things with .lstrip()).
I've been using Groovy for a few years, but not in the last few months, so this could just be a newbie question. I'm trying to parse a log file, but when I try to do this:
myFile.eachLine { line ->
/* 2014 Jul 30 08:55:42:645 GMT -4 BW.TMSJobService-TMSJobService-1
* User [BW-User] - Job-2584 [Process/Common/LogAuditInfo.process/WriteToLog]: */
/* 1234567890123456789012345678901 */
/* 0 1 2 3 */
LogItem logItem = new LogItem()
// get the time stamp
String timestamp = line.substring(0, 31)
SimpleDateFormat sdf = new SimpleDateFormat('yyyy MMM dd HH:mm:ss:S')
logItem.date = sdf.parse(timestamp)
}
I get this exception:
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: java.text.SimpleDateFormat.parse() is applicable for argument types: (java.lang.String, ce.readscript.TmsLogReader$_read_closure1_closure3) values: [2014 Jul 30 08:34:47:079 GMT -4, ce.readscript.TmsLogReader$_read_closure1_closure3#14235ed5]
Possible solutions: parse(java.lang.String), parse(java.lang.String, java.text.ParsePosition), parse(java.lang.String, java.text.ParsePosition), wait(), clone(), clone()
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
at org.codehaus.groovy.runtime.callsite.PojoMetaClassSite.call(PojoMetaClassSite.java:46)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
It's always the last line in the closure. If I add code after the 'parse', then it bombs on this code. Even a "079".toLong() call gets a error.
I see some similar errors in stack overflow, but nothing that solves my problem.
It is trying to invoke SimpleDateFormat::parse(String, Closure) which doesn't exist. There seems to be a typo somewhere. It is working fine under groovy 2.1.8 and 2.3.4. You can try to make it a bit more groovy, to check whether it has some typing error not in your example:
new File("log.log").eachLine { line ->
def item = new LogItem()
def timestamp = line[0..30]
item.date = Date.parse('yyyy MMM dd HH:mm:ss:S', timestamp)
}
I used the time honored technique of deleting the file and starting over. I haven't encountered the issue again.
I have one test step which contains two assertion.
Not SOAP Fault
Contains. The Condition is that response should contain "Message Sent Successfully"
Now I have one groovy script, from where I am executing this test step. Using this groovy script I need to print assertion name, Value and Status. Below is the code I have written:
testStepSrc = testCase.getTestStepByName(testName)
Assertioncounter = testStepSrc.getAssertionList().size()
for (AssertionCount in 0..Assertioncounter-1)
{
log.info("Assertion :" + testStepSrc.getAssertionAt(AssertionCount).getName() + " :: " + testStepSrc.getAssertionAt(AssertionCount).getStatus())
error = testStepSrc.getAssertionAt(AssertionCount).getErrors()
if (error != null)
{
log.error(error[0].getMessage())
}
}
but in output it is displaying like:
Wed Sep 04 17:21:11 IST 2013:INFO:Assertion :Not SOAP Fault :: VALID
Wed Sep 04 17:21:11 IST 2013:INFO:Assertion :Contains :: VALID
As you can see, I am able to print assertion name and status but not the value of 'Contains' assertion. Please help me how to get the value of a particular assertion.
Thanks in advance.
So here is some things for you to read
http://www.soapui.org/forum/viewtopic.php?t=359
http://whathaveyoutried.com
and what i tried
def assertionsList = testRunner.getTestCase().getTestStepByName("Test Step Name").getAssertionList()
for( e in assertionsList){
log.info e.getToken() //gives the value of the content to search for
log.info e.DESCRIPTION
log.info e.ID
log.info e.LABEL
log.info e.toString()
}
This gives the following output
Wed Sep 04 15:12:19 ADT 2013:INFO:Abhishek //the contains assertion was checking for the word "Abhishek" in the response of my test step where the assertion was applied.
Wed Sep 04 15:12:19 ADT 2013:INFO:Searches for the existence of a string token in the property value, supports regular expressions. Applicable to any property.
Wed Sep 04 15:12:19 ADT 2013:INFO:Simple Contains
Wed Sep 04 15:12:19 ADT 2013:INFO:Contains
Wed Sep 04 15:12:19 ADT 2013:INFO:com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.SimpleContainsAssertion#c4115f0
Abhishek's response did contain you answer I believe but just not in the format you were looking for.
I was looking for the same info for custom reporting and after digging through The SoapUI forms I stumbled upon this.
The piece of code that I believe you are looking for is:
log.info e.getToken()
however this is an example of how to retrieve it only when an error occurs but you can get it in a valid scenario using something similar to:
def iAssertionName = assertionNameList[j]
def iAssertionStatus = testStep.getAssertionAt(j).getStatus().toString()
def tstep = testStep.getName()
def gStatus = testStep.getAssertionAt(j).status
def expect = testStep.getAssertionAt(j).getToken()
log.info "Expected Content: " + expect
This is a subset of my code but produces the log message:
Fri Sep 20 11:04:09 CDT 2013:INFO:Expected Content: success
My SoapUI script assertion was checking to see if my response contained the string "success".
Thanks Abhishek for your response!