Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninja!!🥷You must have heard about autonomous driving vehicles 🚗, ever wondered how they work??!! They work by following specific predefined rules and behaviours based on real-time situations. In software development and testing, we developers tend to implement automatic logic to reduce human effort. Scripting helps automate testing and saves resources.
Scripts are pieces of code that define sequential commands to execute based on certain conditions. Several libraries are available in ReadyAPI to ease the writing of these scripts. In this blog, we will learn about these libraries and how to load them from external sources.
Pre-installed Script Libraries
ReadyAPI has different Java and Groovy libraries pre-installed in a ready-to-use state. Some of these are imported by default, and some need manual importing.
Already Imported Libraries
Some Java and Groovy libraries are imported by default and ready to be used anytime. The developer does not have to import these libraries manually. ReadyAPI, by default, comes with these libraries, and users do not have to import them specifically into their projects. These are available in all editors in ReadyAPI.
✅ java.io.*
✅ java.lang.*
✅ java.net.*
✅ java.util.*
✅ java.math.BigDecimal
✅ java.math.BigInteger
✅ groovy.util.*
✅ groovy.lang.*
Installed Libraries Ready for Import
These are the pre-installed libraries in ReadyAPI but are not available by default. We first need to import them to use them in our script. Unlike pre-imported libraries, these are available but not yet imported by ReadyAPI. The developer must explicitly import them before using them. We must use the import <library> command to import the desired library. Following are examples of such libraries:
Let's see a sample Groovy script where we import these libraries.
//Hey Ninja, this is a sample Groovy script involving JDBC driver and database
//Import the required library using the import keyword
import groovy.sql.Sql
// Set up database connection properties
def url = 'jdbc:mysql://mydatabase.com/DatabaseVariable'
def user = 'DatabaseUser'
def password = 'DatabasePassword'
def driver = 'com.mysql.jdbc.Driver'
com.eviware.soapui.support.GroovyUtils.registerJdbcDriver( driver )
def sql = Sql.newInstance(url, user, password, driver)
sql.close()
External Java Libraries
Sometimes the built-in libraries are not sufficient for writing efficient scripts. In such cases, we need to install/load external java libraries. We can either write and compile our library or use one from a third-party source. Let's learn how we can load a pre-compiled library as a jar file into ReadyAPI.
Loading External Libraries
Follow these steps to load an external library into ReadyAPI:
💡 Get hold of your jar file and put it into the <ReadyAPI>/bin/ext directory. This step adds your pre-compiled library into the binary→external folder. This directory serves as the home location for external libraries. If you have nested dependencies, place those other jar files here.
💡 If you use a virtual service, place these jar files in the <VirtServer>/bin/ext folder.
💡 If you are using a load test, place these jar files in the <LoadAgent>/bin/ext folder.
💡Finally, import these files into your script. Take a look at the code snippet below for reference.
import Ninja_Class // Import, the class
Ninja_Class.code() // Use the add method from the class
You can also try this code with Online Java Compiler
If you are using a script code, again and again, it is better to wrap it inside a class and make a library of such classes that you can import whenever you need them. ReadyAPI has the feature to store classes using Groovy Script Libraries. Let's learn how to store our scripts and load them dynamically.
Storing Scripts
To store our scripts, we have two easy steps:
Specify Script Folder
The default directory for scripts is <ReadyAPI>/bin/scripts. If you want to change it:
Preferences→ ReadyAPI
Select your desired directory
This directory now serves as the storage location for your scripts. If any new script is found, it compiles it for us. The scanning happens every five seconds to check for new or updated scripts.
Organize your Script Packages
To organize nested scripts, make a directory with the same name as the package. For example, if you have a script named Ninja.work, it should be placed in the /ninja/work folder.
Example
We will now learn how we can create groovy objects and use them in our ReadyAPI projects. We will also import and load them into our project. In this example, we will use Ninja.groovy library. 😁
Fri Sep 30 11:28:06 EST 2022:INFO:Ninja is Learning ReadyAPI
Calling Static Methods
Static methods are globally available and are defined for a class rather than the instances of the class. The following code will work just fine:
readyapi.demo.Ninja.salute(“Ninja”,log)
Output: Fri Sep 30 11:30:08 EST 2022:INFO:Hello again Ninja!
Load the Library Dynamically
For dynamic loading of the library, use the following script:
//Set the path
System.properties[ 'soapui.scripting.library' ] = "C:\\Work\\GroovyLibs"
Thread.sleep(2000) //makes a two-second pause
Frequently Asked Questions
What is a script?
It is a computer language containing several commands in a file capable of running without the need for compilation.
What is Groovy?
It is a java syntax following object-oriented language intended to be run on the java platform. It has features of both static and dynamic typed languages. Groovy is mainly used for script writing.
What is a Library?
A library is a codebase of pre-written codes which are readily available for use. These are intended to make the life of a developer by providing robust code snippets for the most commonly used methods and functions.
Conclusion
In this blog, you learned about script libraries in ReadyAPI. All the important topics related to Script Libraries in ReadyAPI were covered in detail.
Also, do refer to other API testing-related articles: