SoapUI Beginner Tutorial 6 - How to Get and Set Properties through GROOVY scripts
Vložit
- čas přidán 4. 09. 2024
- website - automationstep...
Today we will learn:
1. How to get property
2. How to set property
3. How to add property
4. How to remove property
5. How to loop through all properties
References
www.soapui.org...
Notes
// Get and Set Property
//Project
testRunner.testCase.testSuite.project.getPropertyValue("Name")
testRunner.testCase.testSuite.project.setPropertyValue("Name","I am in Project")
//TestSuite
testRunner.testCase.testSuite.getPropertyValue("Name")
testRunner.testCase.testSuite.setPropertyValue("Name","I am in TestSuite")
//TestCase
testRunner.testCase.getPropertyValue("Name")
testRunner.testCase.setPropertyValue("Name","I am in TestCase")
//TestStep
testRunner.testCase.getTestStepByName("CountryCodes").getPropertyValue("Name")
testRunner.testCase.getTestStepByName("CountryCodes").setPropertyValue("Name","I am in Test Step")
//Global
com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "Name")
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue( "Name","I am in Global Prop" )
// Add Property
testRunner.testCase.testSuite.project.addProperty("DOB")
// Remove property
//Project
testRunner.testCase.testSuite.project.removeProperty("Name");
//TestSuite
testRunner.testCase.testSuite.removeProperty("Name")
// Loop through properties
testRunner.testCase.properties.each
{
key,value -
log.info (testRunner.testCase.getPropertyValue(key))
//log.info (key+" - "+value)
}
_______________________________________________________
SoapUI Playlist -
• SoapUI
Like on Facebook - / automationstepbystep
Subscribe on:
CZcams - / automationstepbystep
_______ ONLINE COURSES TO LEARN _______
Visit - automationstepb...
Hi Raghav,
I'm Michael from Hungary. I will start as a junior automation tester in October and your videos are quite useful, so thank you very much. I have the intention to watch all of your videos. Your explanations are so clear and understandable. You are an excellent teacher. Thanks again.
Best of luck Mihály You can check all here - automationstepbystep.com/
I really like the way you create a slide at the end with the overview and some tips, great!!
Glad to know this Jorge
Very good Raghav, thanks for these tutorials
You're welcome Jeremy
Exceleentway of teaching- to the point i would say. thanks
You're very welcome Sharat
Excellent groovyscripting 100% use video Please do more videos iam waiting for your videos really great job
Great to know this Gowri. Keep watching.
Thanks alot for your guidance
Most welcome Spriti
Excellent work.Thak you for amazing work
Thanks for your comment Rakhi
Very informative video.
Glad to know this
Very effective presentation
Thanks Goran
Great info on groovy scripting
Thanks for watching Munaver
Nice explanation thank you!! Do we need to preinstall Groovy script in our machine to follow this session?
No as long as you are working inside SoapUI
@@RaghavPal Thank for your reply.
Hi Raghav ,
Very informative and helpful videos, can we create library class using groovy script ?
can you please create videos for ServiceV groovy scripts how to handle endpoints communication with client applications
Hi Krishan, I will do these topics
Hi,
Excellent video. Just had a question - so getPropertyValue and context.expand is doing the same thing right? Both are used to fetch property values?
Hi Niaz, context is actually a placeholder. In this case you can say they are doing the same thing.
Excellent
Hi Raghav,
Its nice video but some confusion in it .
in your previous video you described to get the property value by Context class and in this video you get the property by testRunner class . can you tell me what is the difference between it and when we used these classes .
And also let me know how i learn groovy upto soap level please refer material.
Thanks .
Hi Bhupender, sorry for the confusion, This will help you -
stackoverflow.com/questions/25184307/what-t-is-the-difference-between-context-and-testrunner-in-soapui
community.smartbear.com/t5/SoapUI-Open-Source/testRunner-Context-Object-in-groovy-script/td-p/34009
There are some sublinks in the above page that will be useful
Very informative sessions. I would like to know if there is a option to extract execution report with the Test case title and result as Pass /Fail from Soap UI. If yes, can the execution report be exported to excel document
Hi Girija, you can check the default reports generated. In case you need something customized can do that with groovy scripting
www.soapui.org/reporting/reference/reporting-tab.html
community.smartbear.com/t5/SoapUI-Open-Source/how-to-generate-report-in-SoapUI-5-3-0-free-version-i-am-looking/td-p/147608
Dude, You're absolutely the best. I was loosing my mind looking for an answer and then I saw Your video. Totally saved my ass, thanks
Happy to know it helped
How to get the value from request and pass it to the response.
Hi Devraj, I believe I have covered this in one of the videos in SoapUI playlist, and its related to property transfer
hi...could u please help me in setnodevalue and getnodevalue functions, as I am unable to pass values in request through groovy. so any demo script u can provide will be helpful. and also ur way of teaching is awesome, can grasp things quickly.
Hi Lata thanks for watching. What is the error its showing. Pls send me logs.
Without giving input request manually can we give as pick it from specific location
Hi Mahesh, yes you can use requests from external files. Can check some examples online
@@RaghavPal I can't able to find can you please help me my number is 9059342222
Hi Raghav, can you please share what is the syntax to 'Add a property' and 'Remove a property' and 'Loop through properties' at a Test Step level?
I will plan a session Ashish
Raghav these 22 vedios are enough for handle Project in real time? or we need learn more about SoapUI to handle Realtime Project?
Hi Krishna, these will be enough to get started with any enterprise project and this will lay the foundation. After this, with this knowledge base, you can explore more as per the project requirements. I created this course after my experience of creating a project for enterprise api automation testing with soapui free ver.
Raghav,
Can you please make a video on attaching any png or jpg file in a SOAP UI TOOL
I will check and plan Abhijeet
Hi Raghav, I am able to get the property using context but getting error whenever I try to get the property with testRunner. The error is: "No such property. TestRunner for class: Script1". Any idea how to resolve this?
Hi Vaibhav,
The error message "No such property. TestRunner for class: Script1" indicates that the TestRunner object is not recognized by the script. This can happen if the script is not executed in the context of a test case or if the TestRunner object is not imported correctly.
To access the TestRunner object in a Groovy script in SoapUI, you need to make sure that the script is executed in the context of a test case. This means that the script should be associated with a test step or a test case in SoapUI.
Here's an example of how you can access the TestRunner object in a Groovy script in SoapUI:
// Import the TestRunner class
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner
// Get a reference to the TestRunner object
WsdlTestCaseRunner testRunner = context.testCase.testSuite.project.testSuites["TestSuite 1"].testCases["TestCase 1"].run(new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(), false)
// Get the value of a property using TestRunner
def propertyValue = testRunner.testCase.testSuite.getPropertyValue("property_name")
In the above example, we have imported the WsdlTestCaseRunner class, which is a subclass of TestRunner. We then get a reference to the TestRunner object using the run method of the WsdlTestCaseRunner class. Finally, we get the value of a property using the getPropertyValue method of the TestRunner object.
Make sure that you replace "TestSuite 1" and "TestCase 1" with the actual names of your test suite and test case. You should also replace "property_name" with the name of the property that you want to get.
Hi Raghav , I didn't get tthe output for global Property
log.info "Virtusa consulting services pvt ltd"
def name6=context.expand('${#Global#Name}')
log.info("Company name is : "+name6);
Even if i give the value to the name global property through preferences .
output:
Tue Nov 12 22:55:46 IST 2019:INFO:Virtusa consulting services pvt ltd
Tue Nov 12 22:55:46 IST 2019:INFO:Company name is :
Hi Vipin, the syntax seems correct and in the output you are getting blank but no errors. Pls check in Global settings if the property Name is added and has a value and project is saved
In final concept of printing property by loop , we are passing key as argument , why is it printing the values???
Okay okay , understood. but when i tried this , i am getting only Null values printed even when there are values for the key
Hi Akshayaram, make sure you have saved the project and the spelling is correct (case-sensitive) with no leading or training spaces
Hi Raghav , thanks a ton for your videos !
i hav a property named prop1 which can take only 3 values - A,B and C.
Is it possible that during runtime , one of 3 random values will be set for prop1 ? if this is possible , Can you please share URL of the video ? thanks in advance !
Hi Vighnesh, you can do, Can do this with Groovy Script
Thanks Raghav , i found it and it works as below:
def mylist = ["A","B","C","D","E"]
Collections.shuffle mylist
def key= mylist.first()
Raghav,
Can you do getproperty and setproperty for dynamic values and pass it in another request/ response like we do it with property transfer
.
If yes,can you guide how?
Hi Abhijeet,
Yes, you can use the getProperty and setProperty methods in SoapUI to retrieve and set property values programmatically. These methods can be used to work with dynamic values and pass them between requests and responses in a similar way to property transfer.
Here is an example of how to use getProperty and setProperty methods in SoapUI:
In your first request, use a script step to extract a value that you want to use in another request. For example, you could extract a value from the response using a regular expression:
def response = context.expand( '${Request 1#Response}' )
def pattern = /(.*)/
def matcher = (response =~ pattern)
def value = matcher.group(1)
Set the extracted value as a property using the setProperty method:
context.setProperty( 'my.property', value )
In your second request, use a script step to retrieve the property value using the getProperty method:
def value = context.getProperty( 'my.property' )
Use the retrieved value in your second request. For example, you could use it as a parameter in the request:
def request = "example.com/service?param=${value}"
Using these methods, you can pass dynamic values between requests and responses in SoapUI just like you would with property transfer.
@@RaghavPal hi Raghav
I tried the below mentioned steps to transfer dynamic values in soap ui just like property transfer using getproperty and setproperty,but unfortunately it didn't work.
Can you please guide or make a video on how can we do it
@@RaghavPal Hi Raghav
Please look into my below comment.
The code that you gave didn't work.
Can you please help me on the same or make a video on the same so that people at large would be benefited
I will check and try to do a video
@@RaghavPal thanks so much Raghav..
If you can try to make the video soon,it will help me pass the show stopper of my project
Thanks in advance
Hello Sir,
For test suite property, I am getting null value in the output. However, I have set the value of Name property as " I am Test Suite."
Also, when we do getpropertyvalue for test suite, how does it differentiate between test suite 1 and test suite 2 ? I have set properties for both the test suites.
Hi Dhananjay, pls check if there are any white spaces in your property name. Check of any spelling mistakes, case of characters.
@@RaghavPal Thank you for replying Sir. I added another property and it worked.
I would be thankful if you could address my second query as well. I have added name property to both the Test Suites. However, when I execute getpropertyvalue, I see the value added for Test Suite 2 in the result. Why is it so?
Pls show your code.
how to attach documents in SOAP UI ?Please share your videos
Do not have it yet Arun.
Hi Raghav, can't we get method names after dot operator in SoapUI as in eclipse ide. For example testRunner.testcase... Do we need to remember or what Raghav?
Hi Hari, I believe this was introduced in ready api paid ver.
Hi Raghav thanks for your reply. I'm really appreciate for your work and time. You are really helping to the people by your teaching.
Hi Sir, How can I set/get/add a Test Suite Property to a particular testsuite (e.x. Testsuite1 or TestSuite2)?
Regards
Hi Mario, there is a video on that. pls check automationstepbystep.com/909-2/online-courses/
Hi Raghav,
Could you please help me with groovy script to clone test suite from project to another
Hi Naga, this can help community.smartbear.com/t5/SoapUI-Open-Source-Questions/How-do-you-use-a-Groovy-script-to-clone-a-TestSuite-from-one/td-p/169552
Thank you for the quick response
Is there a way to send the null value in the string?
Where exactly you need to send null. Ideally if you keep it empty, it will be null
SOAPUi Request XML
...
I want this value to go to the SAOP endpoint/ code as null(object). When I leave this empty it goes to the code as empty string.
C# code
[webmethod]
public bool foo(string a, string b string c)
Expecting this
if(string c == null) do something.
receiving this
if(string c == string.empty) this is what I get now.
Thank you, Raghav. The video series is really awesome and helpful.
Automation Step by Step - Raghav Pal
Did you try using nil="true" in the request tag, like here . - blog.ginzburgconsulting.com/null-values-in-soap-requests/
Hi Raghav! Loop code is not working for me. it is giving a very long error "org.codehaus.groovy.control.MultipleCompilationErrorsException". Pls suggest.
Hi Jeetendra, will need to check more details, Pls check if you find Caused By section in the logs
@@RaghavPal sure, thank you
def testStepProp = testRunner.testCase.getTestStepByName("CountryCodes").getPropertyValue("Name")
log.info("Test Step Property = "+testStepProp)
Got the output as Null Pointer
.
Hi Vipin, again check if you have added the property Name in the stop CountryCodes and has a value, and project is saved. It is case sensitive, Also check for any leading or trailing spaces in the name
@@RaghavPal
Hi Ragahav i think it's a case sensative
def testStepProp = testRunner.testCase.getTestStepByName("CountryCodes").getPropertyValue("Name")
log.info("Test Step Property = "+testStepProp)
Vipin, Will have to see more details and logs to troubleshoot
Hi Ragahav I have a same issue. All properties are specified correctly.
Hi, The countryinfoservice WSDL is throwing error 503. Can you please let me know any other sample SOAP service
Hi Shaan, you can see here - quicksoftwaretesting.com/sample-wsdl-urls-testing-soapui/
How to set property at environment level?
Hi Pragati, there are 5 levels in SoapUI
Project
TestSuite
TestCase
TestStep
Global properties (In settings)
Now based on your needs you can set the properties at any level. If you want to set something globally that can be accessed throughout the project use Project level properties. For anything at suite level use properties at TestSuite level and so on. For env Project level properties will be best suited
@@RaghavPal thank you
Excellent work...when I run the script it shows script1$_run_closure1@3dd2b5d4
Hi Amrita, not familiar with this issue, Pls recheck with the video if there are any differences in the steps or setup
Hello Raghav,
Thanks for sharing the contents on SOAP UI Groovy Scripting.
By referring to your video,
I tried to add/Set properties using Groovy scripting, but properties are getting added multiple times.
Below is my code [SOAP UI Version 5.7.0]
//addProperty
testRunner.testCase.testSuite.addProperty("A2AusernameC0001")
testRunner.testCase.testSuite.addProperty("A2ApasswordC0001")
testRunner.testCase.testSuite.addProperty("Nickname")
Make me correct if I did something wrong in the script.
Hi Komal, Can try
testRunner.testCase.testSuite.setPropertyValue( )
I also tried with setPropertyValue() and result is the same that have for addProperty()
Check all the steps again