Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Did you ever wonder how API access the data we want? How many millions of requests and operations are performed in the backend to provide you with the information you requested?
This article will discuss how to access samples in Ready API. The ‘samples’ we will discuss are various operations, resources, and requests exchanged between you and the server. We will also discuss how to access the properties of these samples as well.
So let’s get started!
Access Operations and Resources
We are talking about accessing samples in Ready API. The very first thing we try to access is operations and resources. We can use groovy scripts to work with various entities.
We will use a groovy script to access SOAP operations and REST resources in APIs. It will be helpful when we want to change their properties or content according to our needs.
We have several ways to implement this using script:
⭐We can run the script as a Groovy Script test step.
⭐In functional tests, we can run the script as a setup or teardown script on the project, test suite, or test case level.
👉The following script will show us how to access thelogin rq SOAP operation in the sample SOAP project. We will change the username and password by implementing this code.
// Access the interface
def myInterface = testRunner.testCase.testSuite.project.getInterfaceByName("ServiceSoapBinding")
// Access the operation
def myOperation1 = myInterface.getOperationByName("login")
// Access the request
def myRequest1 = myOperation.getRequestByName("login rq")
//Change request parameters
myRequest1.setUsername("Login1")
myRequest1.setPassword("password")
Accessing Request XML Content
Moving forward in our discussion, we will now learn how to request access to our XML content. As we know, Groovy scripts have access to the XML content of the request. We can also modify it by using groovy scripts. We can change the access request text before, during the test run, and before the access request is sent.
We have several ways to implement this using script:
⭐We can run the script as a Groovy Script test step.
⭐In functional tests, we can run the script as a setup or teardown script on the project, test suite, or test case level.
⭐We can run the script as an event in the project too.
👉The following examples modify the XML content of a test step with the added ninja name. We assume the test step has the following content:
👉This example modifies the request made by the ninja.
// Create groovyUtils and XmlHolder for the request made by the Ninja step
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( 'Add Ninja#Request' )
// Change the node values in the imported XML
holder["/Ninja/request"] = "Groovy"
holder["//status"] = "sold"
// Now update the request and write the updated request back to the test step
holder.updateProperty()
context.requestContent = holder.xml
👉If we use this script as the Setup Script of a test case, it will modify the XML before the execution of test cases:
We can also delete empty XML nodes using the Remove Empty Content property. Although, it only removes empty end nodes and leaves empty parent nodes. So for deleting the others, we will use the following script:
// Creating groovyUtils and XmlHolder for the request of the Sample Request test step
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( 'Add Ninja#Request' )
// Now, finding the nodes that only consist of whitespaces
for( item in holder.getDomNodes( "//*[normalize-space(.) = '' and count(*) = 0]" )){
item.removeXobj()
}
// Now, pdating the request and writing the updated request back to the test step
holder.updateProperty()
context.requestContent = holder.xml
Access Properties
We are now discussing the last part of our article. How to access properties in the Ready API? We know that groovy scripts have access to our project information. It includes settings, properties, and names of all items in the project.
We can modify these from the script to suit our needs during the test run. After our test run, we set them back to the default values.
We have several ways to implement this using script:
⭐We can run the script as a Groovy Script test step.
⭐In functional tests, we can run the script as a setup or teardown script on the project, test suite, or test case level.
Get and Set Properties
We used the getter and setter calls in various OOPs languages. And now, we can access all properties of ReadyAPI test items by implementing the following script:
// Get a property from a test case
def testCaseProperty = testRunner.testCase.getPropertyValue( "MyProp1" )
// Get property from a test suite
def testSuiteProperty = testRunner.testCase.testSuite.getPropertyValue( "MyProp1" )
// Get a project property
def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue( "MyProp1" )
// Get a global property
def globalProperty = com.eviware.soapui.SoapUI.globalProperties.getPropertyValue( "MyProp1" )
// In Script Assertions, you access test cases in a different way:
// def testCaseProperty = messageExchange.modelItem.testStep.testCase.getPropertyValue( "MyProp1" )
// Set values to the same properties.
testRunner.testCase.setPropertyValue( "MyProp1", 'someValue' )
testRunner.testCase.testSuite.setPropertyValue( "MyProp1", 'someValue' )
testRunner.testCase.testSuite.project.setPropertyValue( "MyProp1", 'someValue' )
com.eviware.soapui.SoapUI.globalProperties.setPropertyValue( "MyProp1", 'someValue' )
Renaming Test Items
Renaming the test items such as projects, test suites, and test cases is a specific example of a property change. It is because the name of a test item is one of its properties. We can change it by implementing the following scripts:
// Set names for the test case, test suite, and project
testRunner.testCase.name = 'Sample Test Case Name of Ninja'
testRunner.testCase.testSuite.name = 'Sample Test Suite Name of Ninja'
testRunner.testCase.testSuite.project.name = 'Sample Project Name of Ninja'
🤓Note: Some scripts get items by their names. If we rename the item, then those scripts will fail.
Frequently Asked Questions
Where are ReadyAPI projects stored?
The ReadyAPI workspace is, by default, stored in the C:\Users\<username>\ folder.
What is a composite project in ReadyAPI?
When we use a composite project, ReadyAPI splits our project into several files. It is because each team member will work on this project simultaneously. If our file system has a path character limit (255 characters), we may need to rename our resources to ensure that the full path is shorter than the limit.
Is ReadyAPI open source?
ReadyAPI is part of the ReadyAPI family and is unavailable in SoapUI Open Source. Therefore, any functions related to ReadyAPI or virtual services will not work in SoapUI Open Source.
Conclusion
This article briefly discusses how to access samples in Ready API. These samples were operations, resources and requests. We also discussed how to access properties using GET and SET keywords and how to rename test items.
We hope that this blog has helped you enhance your knowledge about how to access samples in Ready API and if you like to learn more, check out our articles:
If you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!