Get/Update "REST Request Properties" value in SOAPUI is coming NULL - groovy

I am trying to get the Property value and Set it with different value in SoapUI's "REST Request Properties" (NOT the custom properties). It just gives me NULL value
Here is what I did:
1. Get test step object
2. get the property value with name of the property => It is giving me null value.
I know I am getting the correct object as I was able to rename the same Test Step name with following code
def restRequest = testRunner.testCase.getTestStepByName("Test");
def a = restRequest.getPropertyValue("Method")
log.info(a) // this gives null
restRequest.setName("Test1") // This works

In the step object, there is another object called testRequest from which you can get all those required properties.
For example if you want to get all the properties
log.info step.testRequest.metaClass.methods*.name
For example if you want to know the get methods
log.info step.testRequest.metaClass.methods*.name.findAll {it.startsWith('get')}
Similarly you can get the methods to set the value as well.
For instance, you want to modify Pretty Print from true to false:
step.testRequest.setPrettyPrint(false)
log.info step.testRequest.properties['prettyPrint']
Similarly, you can find the required property name, find the right method to modify the value as per your needs.

Related

Groovy - Access variable which has value of another variable

I've been trying to use a common property file in Jenkins which will have details of multiple servers. Based on the selection in Jenkins(By selecting "Build with parameters"), corresponding server details need to be obtained from the property file. For this, I need to access a value of variable created by value of another variable. Is this supported in groovy?
I have defined the properties in a property file and the sample values are like
PROD_SERVERNAME = sampleprodserver;
DEV_SERVERNAME = sampledevserver;
def environment = "PROD"; // this will be given as a parameter
def servername = environment + "_SERVERNAME";
def Propertyfile = readProperties file:propertyfile;
def server = Propertyfile.servername
I expect the value of server should be sampleprodserver but the value i'm getting is null.
Any help would be highly appreciated.
the code
Propertyfile.servername
tries to get property with name servername from Propertyfile variable
and to get the property value by variable value use one of:
Propertyfile.getProperty(servername)
//or
Propertyfile[servername]

conditional execution of particular teststep according to result of previous request

I use SOAP UI Free.
I want to verify if previous response returns number (id) and conditionally run particular teststep.
My pseudocode below. How can I achieve this action using groovy?
How can I get response and verify if contains number and returns 200?
How can I extract this number and use it as parameter in next request
How can I compare if response is true?
response sample (200) :
523455
response sample (404) :
{
"category": "BUSINESS",
"code": "NOT_FOUND",
"description":"Account not found",
"httpStatus": 404
}
1.step GET accountId
2.step GROOVY
if(accountId is number and returns 200){
extract this number from json
run testRunner.testStep("removeAccount) for extracted number
if(response.equals("true"){
testRunner.runTestStep("createNewAccount")
}
}
1 - Use a property transfer step :
Target the id using JSONPath (should be something like "$.id")
Set "Set null on missing source" to true
Store it in a custom property of your testCase (e.g. "curId")
Additionally, if you want the Http status code, you should store it with a groovy script in another custom variable using something like this :
curHeaders = testRunner.testCase.testSteps["Get token"].testRequest.response.getResponseHeaders()
testRunner.testCase.setPropertyValue("http status", curHeaders["#status#"][0])
2 - Using a groovy script step :
Retrieve the variables above :
curId = testRunner.testCase.getPropertyValue("curId")
curHttpStatus = testRunner.testCase.getPropertyValue("http status")
Test those variables and run test step "removeAccount" if true:
if(curId && (curHttpStatus == 'HTTP/1.1 200 OK'))
{
removeActionTestStep = testRunner.testSteps["removeAccount"]
removeActionTestStep.run(null, false)
}
Note : removeAccount test step should make reference to curId custom variable (e.g. using "${#TestCase#curId}) so it runs with this id
You can add the createAccount part after the removal using jsonSlurper
removeActionResultJSON = context.expand('${removeAccount#Response}')
removeActionResultJSONSlurper = new groovy.json.JsonSlurper().parseText(removeActionResultJSON )
Then target where your response will make it equal true, then use another if - run statement like below.
Hope this helps,
Thomas

SoapUI how to get property transfer TestStep in Script Assertion of the next step

I'm using SoapUi 5.3.0 to test a case like this:
I have 3 apis: Book list, Add a book to favorite list, and Favorite list.
I add 3 apis to a TestCase as TestSteps to test the case: User views book list, choose the first book in the list then adds to favorite list, after that user goes to favorite list to verify that the book is displayed in the first place in Favorite list.
I add a propertyTransfer between step 1 and step 2, to get book_id from respond of step 1, then use to a param of the next step's request.
At step 3, I add an assertion by Script Assertion like below:
import groovy.json.JsonSlurper
//get propertyTransfer value
def tcProperty = messageExchange.modelItem.testStep.testCase.getTestStepByName("propertyTransfer").getPropertyValue("book_id")
// get response message of Favorite book api
def responseMessage = messageExchange.response.responseContent
// get book_id of the first book in favorite list
def jsonSlurper = new JsonSlurper().parseText(responseMessage)
bookId = jsonSlurper.data[0].book_id
// verify
assert bookId == tcProperty
But the script returns failed, and an error displays like attach photo
It seems tcProperty is null, means I could not get propertyTransfer value.
So where am I wrong?
You can either use Property Transfer test step or without it i.e., using Script Assertion for the request step itself.
If Script Assertion is used, then Property Transfer step is not needed at all.
The idea is extract the required value, and set it at test case level custom property in the Script Assertion.
Now, you want to use the above extracted value in the next step, which can be done using property expansion.
Here is the script assertion:
//Check if the response is not empty
assert context.response, 'Response is empty or null'
def jsonSlurper = new groovy.json.JsonSlurper().parseText(context.response)
bookId = jsonSlurper.data[0].book_id
assert bookId, 'Book id is empty or null'
//Set the bookId as test case level property
context.testCase.setPropertyValue('BOOK_ID', bookId)
In the next step, use property expansion i.e., ${#TestCase#BOOK_ID} where ever you needed book id

Global property is not getting added when saving it from groovy script using "launch test runner"

Step 1 : I have a soap rest project, In that i am getting a userID from response.
Step 2 : When i run the test case separately to get the userID, the groovy script for setting the user ID in global property is working fine. Refer : com.eviware.soapui.model.propertyexpansion.PropertyExpansionUtils.globalProperties.setPropertyValue('userID', 'ID from response')
Step 3 : But when i run my whole project using "launch test runner" to get the userID from response and setting the userID to a global property as defined in the above example is not working.
Does any one have idea on this?
Thanks in advance for the answer.
Here is the groovy script which can set the global property value.
Groovy Script
def newValue = 'testsetvalue'
//set the value to global property called PROPERTY_NAME
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue('PROPERTY_NAME', newValue)
// get the property value which was set above.
def getNewValue = com.eviware.soapui.SoapUI.globalProperties.getPropertyValue('PROPERTY_NAME')
//assert it
assert getNewValue == newValue

How to identify the class of an Object which is in a cell of webtable

I have a webtable which MIGHT have a weblink object in it's Row 2, Column 1 cell (also Index of this object is 0). If it indeed is a link I would like to click it else ignore it. Is there a way to identify the class of this object given that we know the row and column number.
Below was my initial code. However it doesn't work always when the webtable cell doesn't have a link to click
Set Table = Browser("Oracle PeopleSoft").Page("Request Payment Predictor").WebTable("Run Control ID").ChildItem(2, 1, "Link", 0)
Table.Click
I would like to know if there a way to find the class of the Object (in cell of a web table) so I can click on the Object only if it's a link Or in other words can we use GetRoProperty("Class Name") on a WebTable Cell Object?
The ChildItem function returns a test object of the requested type if it exists, otherwise it returns Nothing.
So your code should look like this:
Set aLink = Browser("Oracle PeopleSoft")_
.Page("Request Payment Predictor")_
.WebTable("Run Control ID").ChildItem(2, 1, "Link", 0)
If Not aLink is Nothing Then
aLink.Click
End If
The object returned by ChildItem is a test object (if it's not Nothing) so you can use the regular test object methods on it.
Please note that the object returned is not a table cell object, it's the object of the type you requested, this type may be WebElement which is considered the base class of all web objects. This means that you can use ChildItem with "WebElement" and then see what actual type it is by getting its micClass (which is what the Class Name is called internally).
Print webElem.GetROProperty("micclass")
Pro tip: The indexes are 1 based, you can use the undocumented Highlight function in order to make sure you're working on the right object (obj.Highlight).

Resources