How to get assertion value using groovy script - groovy

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!

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

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

Printing the list of files from FTP?

I'm trying to read the list of files from FTP server and printing all files as output.
please check the below code once.
start()
def start(){
def store;
def ftpClient = new FTPClient()
ftpClient.connect(server)
println(ftpClient.replyString)
ftpClient.login(user,pass)
ftpClient.enterLocalPassiveMode()
def fileslist = ftpClient.listFiles("/");
return fileslist;
ftpClient.disconnect()
}
output:
04-03-18 05:20PM 4 Autoplus.txt
04-03-18 08:13PM 19 Autoplus6.txt
03-30-18 06:36PM <DIR> sample
Required output format
|Autoplus.txt|{Tue Apr 03 17:20:00 IST 2018}|
|Autoplus6.txt|{Tue Apr 03 18:13:00 IST 2018}|
How can I print the files in the required out format.
If anybody having idea please let me know
Thanks,
Raj

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