rerun a test case in ready api using tear down script - groovy

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

Related

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

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

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

TimeCategory add minutes using vars

How do i add minutes to currentDate.
I might add more than 1440 minutes..
def AddMinutes = 1445
currentDate = new Date();
println currentDate
use( TimeCategory ) {
NewCurrentDate = currentDate + AddMinutes.minutes // fails
NewCurrentDate = currentDate + 1445.minutes // works
}
println currentDate
Tue Feb 23 15:09:13 CET 2016
Wed Feb 24 15:14:13 CET 2016
Works for me... Can't see your problem apart from you're not printing out the newCurrentDate (PS: Lower case letters for variable names, otherwise Groovy can get confused, and think you're on about classes -- but that's not the issue here)
import groovy.time.*
def addMinutes = 1445
currentDate = new Date()
use( TimeCategory ) {
newCurrentDate = currentDate + addMinutes.minutes
}
println currentDate //Tue Feb 23 14:45:52 GMT 2016
println newCurrentDate //Wed Feb 24 14:50:52 GMT 2016
I found out that I needed to make sure that the addMinutes was an int. So I added this before in my script:
addMinutes = addMinutes.toInteger()
and now it works.

Cannot add members to MongoDB Replica Set

I'm trying to configure a MongoDB Replica Set but every time I try to add another member it fails.
I have 3 members I'm trying to configure. Their mongod.conf files all look like this:
# mongo.conf
#where to log
logpath=/log/mongod.log
logappend=true
# fork and run in background
fork = true
smallfiles=true
rest=true
port = 27017
replSet=KidzpaceReplSet
dbpath=/data
With the acception of the ports. They are 27017(Primary), 27018(Secondary) and 27019(Arbiter) respectively.
I have verified that the members can see each other:
[ec2-user#domU-12-31-39-06-C4-74 ~]$ mongo --host 174.129.232.170 --port 27018
MongoDB shell version: 2.4.3
connecting to: 174.129.232.170:27018/test
>
[ec2-user#domU-12-31-39-0A-30-E8 ~]$ mongo --host 174.129.230.20 --port 27017
MongoDB shell version: 2.4.3
connecting to: 174.129.230.20:27017/test
>
When adding the second member to the set it returns OK:
KidzpaceReplSet:PRIMARY> rs.add("174.129.232.170:27018")
{ "ok" : 1 }
However whatever the next command I run is, In this case it's adding my Arbiter, the set fails with this error:
KidzpaceReplSet:PRIMARY> rs.add("174.129.232.177:27019", true)
Tue May 28 20:24:07.139 DBClientCursor::init call() failed
Tue May 28 20:24:07.140 trying reconnect to 127.0.0.1:27017
Tue May 28 20:24:07.141 reconnect 127.0.0.1:27017 ok
reconnected to server after rs command (which is normal)
This is the the log file:
Tue May 28 20:44:06.173 [rsStart] replSet I am domU-12-31-39-06-C4-74:27017
Tue May 28 20:44:06.173 [rsStart] replSet STARTUP2
Tue May 28 20:44:07.175 [rsSync] replSet SECONDARY
Tue May 28 20:44:07.175 [rsMgr] replSet info electSelf 0
Tue May 28 20:44:08.174 [rsMgr] replSet PRIMARY
Tue May 28 20:44:29.813 [conn1] replSet replSetReconfig config object parses ok, 2 members specified
Tue May 28 20:44:29.817 [conn1] replSet replSetReconfig [2]
Tue May 28 20:44:29.817 [conn1] replSet info saving a newer config version to local.system.replset
Tue May 28 20:44:29.834 [conn1] replSet saveConfigLocally done
Tue May 28 20:44:29.834 [conn1] replSet info : additive change to configuration
Tue May 28 20:44:29.834 [conn1] replSet replSetReconfig new config saved locally
Tue May 28 20:44:39.835 [rsHealthPoll] DBClientCursor::init call() failed
Tue May 28 20:44:39.835 [rsHealthPoll] replset info 174.129.232.170:27018 heartbeat failed, retrying
Tue May 28 20:44:40.834 [rsHealthPoll] DBClientCursor::init call() failed
Tue May 28 20:44:40.834 [rsHealthPoll] replSet info 174.129.232.170:27018 is down (or slow to respond):
Tue May 28 20:44:40.835 [rsHealthPoll] replSet member 174.129.232.170:27018 is now in state DOWN
Tue May 28 20:44:40.835 [rsMgr] replSet total number of votes is even - add arbiter or give one member an extra vote
Tue May 28 20:44:40.835 [rsMgr] can't see a majority of the set, relinquishing primary
Tue May 28 20:44:40.835 [rsMgr] replSet relinquishing primary state
Tue May 28 20:44:40.835 [rsMgr] replSet SECONDARY
Tue May 28 20:44:40.835 [rsMgr] replSet closing client sockets after relinquishing primary
Tue May 28 20:44:42.044 [conn1] end connection 127.0.0.1:58727 (0 connections now open)
Tue May 28 20:44:46.150 [rsHealthPoll] replSet member 174.129.232.170:27018 is up
Tue May 28 20:44:46.151 [rsMgr] replSet not electing self, not all members up and we have been up less than 5 minutes
Tue May 28 20:44:52.156 [rsMgr] replSet not electing self, not all members up and we have been up less than 5 minutes
UPDATE
I'm wondering if maybe the problem is when I run rs.initiate(). It gives me this output:
{
"set" : "KidzpaceReplSet",
"date" : ISODate("2013-05-28T20:59:05Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "domU-12-31-39-06-C4-74:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 23,
"optime" : {
"t" : 1369774732,
"i" : 1
},
"optimeDate" : ISODate("2013-05-28T20:58:52Z"),
"self" : true
}
],
"ok" : 1
}
Notice the name of the member? "name" : "domU-12-31-39-06-C4-74:27017" Where does this name come from? It's not my IP Address. I'm not sure but maybe this could be the source of the problem.
So it turns out rs.initiate() might give the member that launches it some kind of internal alias for it's IP address. In my case it was: domU-12-31-39-06-C4-74.
The initial connection to the secondary is fine because the primary instigates it. However since the secondary now has this alias to use when it tries to talk back to the primary, it fails.
The solution was a to copy the existing configuration:
cfg = rs.conf()
manually change the name(host) of the primary node:
cfg.members[0].host = 666.666.666.666:27017
And reconfigure the replica set:
rs.reconfig(cfg)

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