SOUP UI Groovy || For loop is looping 50 times where the condition is using this number anywhere - groovy

Here I am just taking value(integer) from Properties file and using the same in for loop.
Note : If I use direct number instead of "getTestCasePropertyValue" value it work as expected. Not getting how loop is looping it 50 times.
Groovy script:
def getTestCasePropertyValue = testRunner.testCase.getPropertyValue( "NumOfPayments" )
log.info(getTestCasePropertyValue )
for(i=0; i<=getTestCasePropertyValue; i++)
{
log.info("Test Print"+i)
}
Output:
Fri Mar 06 12:58:47 IST 2020:INFO:2
Fri Mar 06 12:58:47 IST 2020:INFO:Test Print0
Fri Mar 06 12:58:47 IST 2020:INFO:Test Print1
Fri Mar 06 12:58:47 IST 2020:INFO:Test Print2
Fri Mar 06 12:58:47 IST 2020:INFO:Test Print3
...
Fri Mar 06 12:58:47 IST 2020:INFO:Test Print50

Your value from the properties is a String. You will detect problems like this easier, if you use .inspect() to log things.
Also the character '2' is 50 as integer, which then the for loop conditions casts this too.
def getTestCasePropertyValue = "2"
println(getTestCasePropertyValue.inspect())
// → '2'
println(getTestCasePropertyValue as char as int)
// → 50
So best explicitly cast to a number using e.g. .toLong() on the string:
println(getTestCasePropertyValue.toLong().inspect())
// → 2

Related

Using Jscript i want to change 2022-10-20T14:54:29.255+0800 to 13/10/2022 2:54 PM

i tried using this method :
newDateTime = new Date('2022-10-20T14:54:29.255+0800');
but it return me : "Thu Oct 20 2022 14:54:29 GMT+0800 (Singapore Standard Time)"

string comparison does not match but 'contains' does

I have a strange problem using groovy, I found a workaround but I'm quite not satisfied so maybe someone will be able to help me:
I use ReadyAPI 2.8. In my test cases I have groovy steps.
In one of those, I recover a String from a previous test step and I want to do a particular processing if it matches the string "TJA470". The previous test step gives a string that is the output of a ssh command.
here is the groovy step code :
def hbox_ref = context.expand( '${get current HBox reference#hbox_ref}' )
// this returns me the data as a String
log.info hbox_ref
log.info "\"$hbox_ref\"" // to check if there is no spurious blank
log.info hbox_ref.class
log.info (hbox_ref == "TJA470") => returns false
log.info (hbox_ref.equals("TJA470")) => returns false
log.info (hbox_ref.contains("TJA470")) => returns true
here is the console result :
Fri Sep 20 16:13:17 CEST 2019: INFO: TJA470
Fri Sep 20 16:13:17 CEST 2019: INFO: "TJA470
"
Fri Sep 20 16:13:17 CEST 2019: INFO: class java.lang.String
Fri Sep 20 16:13:17 CEST 2019: INFO: false
Fri Sep 20 16:13:17 CEST 2019: INFO: false
Fri Sep 20 16:13:17 CEST 2019: INFO: true
The straighforward test is == or equals though there are differences, I use those in all the other comparisons of the same type and it works.
As you can see here the most logic cases return false and I really can't work out why.
If I do the same script in a tool like 'groovy playground' it works as expected ! :(
I'm not an expert in groovy at all and there must be something that I missed, but I find it very tricky !
If anyone can help ...
thanks
Thanks to SO I found out the problem :
with copy/pasting the console return in the question, it shows that there is a special character at the end of the text. This is not visible in SOAPUI log output ...
I added the following processing in my script :
def hbox_ref = context.expand( '${get current HBox reference#hbox_ref}' )
hbox_ref = hbox_ref.replaceAll("[^a-zA-Z0-9]+","")
or
hbox_ref = hbox_ref.replaceAll("[^\\w]+","")
this gives
log.info (hbox_ref == "TJA470") => returns true (at last !)
more elegant solution (thanks to SiKing) :
(hbox_ref.trim() == "TJA470")
instead of using replaceAll

Remove timestamp and url from string python

I have a string from which I have to remove the timestamp and punctuation. And I have to remove all the digits also but responseCode value
has to be kept as is for example 400 in this case. And wherever 400 comes, it should not be removed. And I want to remove all the url's
and file name ending with tar.gz.
mystr="sun aug 19 13:02:09 2018 I_am.98189: hello please connect to the local host:8080
sun aug 19 13:02:10 2018 hey.94289: hello not able to find the file
sun aug 19 13:02:10 2018 I_am.94289: Base url for file_transfer is: abc/vd/filename.tar.gz
mon aug 19 13:02:10 2018 how_94289: $var1={
'responseCode' = '400',
'responseDate' = 'Sun, 19 Aug 2018 13:02:08 ET',
'responseContent' = 'ABC' }
mon aug 20 13:02:10 2018 hello!94289: Error performing action, failed with error code [400]
"
Expected result:
"I_am hello please connect to the local host
hello not able to find the file
Base url for file_transfer
var1
responseCode = 400
responseDate
responseContent = ABC
Error performing action, failed with error code 400
"
My Solution to remove punctuation:
punctuations = '''!=()-[]{};:'"\,<>.?##$%^&*_~'''
no_punct = ""
for char in mystr:
if char not in punctuations:
no_punct = no_punct + char
# display the unpunctuated string
print(no_punct)
Maybe:
patterns = [r"\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} \d{4}\s*", #sun aug 19 13:02:10 2018
r"\w{3}, \d{2} \w{3} \d{4} \d{2}:\d{2}:\d{2} \w{2}\s*", #Sun, 19 Aug 2018 13:02:08 ET
r":\s*([\da-zA_Z]+\/)+([a-zA-Z0-9\.]+)", #URL
r"([a-zA-Z_!]+)[\.!_]\d+:\s*", #word[._!]number:>=0space
r":\d+",
"[/':,${}\[\]]" #punctuations
]
s = mystr
for p in patterns:
s = re.sub(p,'', s)
s = s.strip()
print(s)
Output:
hello please connect to the local host
hello not able to find the file
Base url for file_transfer is
var1=
responseCode = 400
responseDate =
responseContent = ABC
Error performing action failed with error code 400

rerun a test case in ready api using tear down script

I have a test case "Login" which intermittently fails due to login issues.
I would like to implement a tear down script to get the status of the script and rerun if it failed.
Here is what I implemented and it doesn't work as expected.
testRunner.testCase.setPropertyValue("LoginStatus",
testRunner.getStatus().toString())
def loginStatus = context.expand( '${#TestCase#LoginStatus}' )
int retryAttempts = context.expand( '${#Project#RetryAttempts}' ).toInteger()
def myContext = (com.eviware.soapui.support.types.StringToObjectMap)context
while ( loginStatus == "FAIL" && retryAttempts <= 1) {
retryAttempts = retryAttempts+1
log.info "increment retry attempts-" + retryAttempts
testRunner.testCase.testSuite.project.setPropertyValue( "RetryAttempts",
retryAttempts.toString() )
testCase.run(myContext, false)
log.info "after run statement-"+retryAttempts
}
log.info "before final statement"
testRunner.testCase.testSuite.project.setPropertyValue( "RetryAttempts", "0"
)
The script runs 3 times even though it is configured to rerun once. The logs
Fri May 18 13:55:15 EDT 2018:INFO:increment retry attempts-1
Fri May 18 13:55:16 EDT 2018:INFO:increment retry attempts-2
Fri May 18 13:55:16 EDT 2018:INFO:before final statement
Fri May 18 13:55:16 EDT 2018:INFO:after run statement-2
Fri May 18 13:55:16 EDT 2018:INFO:before final statement
Fri May 18 13:55:16 EDT 2018:INFO:after run statement-1
Fri May 18 13:55:16 EDT 2018:INFO:increment retry attempts-2
Fri May 18 13:55:17 EDT 2018:INFO:before final statement
Fri May 18 13:55:17 EDT 2018:INFO:after run statement-2
Fri May 18 13:55:17 EDT 2018:INFO:before final statement

Need help - SoapUi testRunner.getStatus() is returning the status as "RUNNING" indefinitely

In SoapUI after executing a soap request test step (which is under a test suite -> test case)
through testRunner.runTestStepByName("Soap Request Name")
and waiting for 10 seconds after that soap request execution testRunner.getStatus() is returning RUNNING status . below is the groovy script (which is under same test suite -> test case)
import groovy.sql.Sql;
import com.eviware.soapui.model.testsuite.TestRunner.Status
testRunner.runTestStepByName("GetCitiesByCountry - Request 1")
sleep(10000)
log.info( "...${testRunner.getStatus()}...")
while ( testRunner.getStatus() == Status.RUNNING ) {
log.info(testRunner.getStatus())
}
the output is below
Wed Apr 17 21:06:22 IST 2013:INFO:RUNNING
Wed Apr 17 21:06:22 IST 2013:INFO:RUNNING
Wed Apr 17 21:06:22 IST 2013:INFO:RUNNING
Wed Apr 17 21:06:22 IST 2013:INFO:RUNNING
Wed Apr 17 21:06:22 IST 2013:INFO:RUNNING
Wed Apr 17 21:06:22 IST 2013:INFO:RUNNING
.
.
continuing for infinite time...
Ideally it should return FINISHED since the above test step is executed ,
Advanced thanks for any help to this
It sounds logical, as long as you are in the loop, the test is 'running'. You can get the status with this:
import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus
myTestStepResult = testRunner.runTestStepByName("GetCitiesByCountry - Request 1")
myStatus = myTestStepResult.getStatus()
if (myStatus == TestStepStatus.OK)
log.info "The step status is: " + myStatus.toString()
else
log.error "The step status is: " + myStatus.toString()
Also, as the call to runTestStepByName is synchronous, there is no 'running' status, only 'CANCELED', 'FAILED', 'OK' or 'UNKNOWN'.
See the doc here

Resources