Table of contents
1.
Introduction
2.
Pre-installed Script Libraries 
2.1.
Already Imported Libraries
2.2.
Installed Libraries Ready for Import
3.
External Java Libraries
3.1.
Loading External Libraries
4.
Groovy Script Libraries
4.1.
Storing Scripts 
4.1.1.
Specify Script Folder
4.1.2.
Organize your Script Packages
4.2.
Example 
4.2.1.
Creating The Groovy Library
4.2.2.
Import the Package
4.2.3.
Calling Static Methods
4.3.
Load the Library Dynamically
5.
Frequently Asked Questions
5.1.
What is a script?
5.2.
What is Groovy?
5.3.
What is a Library?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Script Libraries in ReadyAPI

Author Manish Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Script Libraries

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. 

Pre-installed library

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:

✅ javax.jms.*

✅ java.nio.*

✅ groovy.json.*

✅ groovy.sql.*

✅ groovy.xml.*

✅ org.apache.commons.*

✅ org.apache.http.*

✅ org.apache.activemq.*

 

Must Read Apache Server

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.

External libraries

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
Run Code

Groovy Script Libraries

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.

Groovy

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. 😁

Creating The Groovy Library

Follow these steps to create the library:

  • Create Ninja.groovy file inside <ReadyAPI>/bin/scripts directory
  • Add the code snippet given below:
package readyapi.demo
// Make a Ninja Class 
class Ninja
{
    String code()
    {
        return "Ninja is Learning ReadyAPI"
    }
    def static salute( who, log ) { log.info "Hello Again $who!" }
}
You can also try this code with Online Java Compiler
Run Code

 

Import the Package

To use the above package, we need to import it into ReadyAPI. Follow the instructions to import the package:

  • Restart ReadyAPI and create a Groovy Script test step
  • Write the code snippet given below in the script editor
import Ninja
// call the class and log the information
N = new Ninja()
log.info N.code()
You can also try this code with Online Java Compiler
Run Code

 

  • Press the button to run your script
  • The log should show something like this:

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:

Please refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enrol in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Please upvote our Script Libraries in the ReadyAPI blog if you found it helpful and informative!

Happy learning!

Live masterclass