Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Working With Data Sources
2.1.
Combining Multiple Data Source Values
2.2.
Randomizing Data Source Rows
3.
Detect Load Test Runs
4.
Encoding Attachments
5.
Getting the Current Date
6.
Frequently Asked Questions
6.1.
What are the two types of data sources?
6.2.
Why is a load run test needed?
6.3.
What type of data sources can be used in ReadyAPI?
7.
Conclusions
Last Updated: Mar 27, 2024
Medium

Encoding Attachments and Data Sources in Ready API

Introduction

We all deal with a lot of information in our daily life. Sending and receiving attachments are a small part of it. Even on a trip, we still exchange data and other information with others. We usually exchange this information using several applications. But did you ever wonder about its back-end processes?

This article will discuss how we generally encode attachments and work with data sources using Ready API. We will start our discussion by getting a gest about working with data sources. Afterward, we will detect them using some tests and add attachments to our Ready API.

At last, we’ll discuss how to get the current date using Ready API. So please stick with us to the end to get some exciting information!

encoding attachments and data sources in ready api

Working With Data Sources

We cannot talk about “how to add any attachments” without knowing where these attachments are stored on the computer. 

Data sources mean the location where the data being used originates. We can use it to create data-driven tests. Generally, data sources only provide the information they take from the source to any test step that needs it. They do not modify the data in any way or control it. We use Groovy scripts to do this. 

working with data sources

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 test case level.
 

👉Now let’s look at some operations that can be performed on data sources.

Combining Multiple Data Source Values

The first operation we generally prefer is combining multiple data source values.  

A data source combines the value into a single string and writes them to the test case custom property.

// Get values from a data source.
def yourstateName = context.expand( '${DataSource#StateName}' )
def yourstateCode = context.expand( '${DataSource#StateCode}' )

// Get the project's XmlFragment property.
def fragment = context.expand( '${#Project#XmlFragment}' )

// Combine the text of all properties.
def newValueofProperty = fragment + yourstateName + yourstateCode

// Write the result text to the test case property.
testRunner.testCase.setPropertyValue( 'ResultText', newValueofProperty )

Randomizing Data Source Rows

Now discussing the second operation that we use on the data sources. Most data sources in ReadyAPI return values in sequence. We can use groovy scripts to randomize the order of the returned values each time you run a test. Here is how we organize our test to do this:

⭐The original Data Source test step with the data you want to randomize.

⭐The Groovy Script test step creates a list with the data from the data source. 
 

👉Use the following script:

// Create a list if necessary.
// If our data source returns multiple properties, we can create a list for each property we need.
if( context["allRows"] == null ) context["allRows"] = []

// Append the data source row value to the list.
context["allRows"] << context.expand( '${NinjaDataSource#outputProperty}' )

 

⭐The Data Source Loop test step iterates through the original data source so that you can get all the data it returns.

The Groovy Data Source test step provides random data for our test. 
 

👉We can use the following script:

// To shuffle the list before returning values.
// We will see a separate Groovy Script test step if you do not want to shuffle the list at each iteration.
Collections.shuffle( context["allRows"] )

// Get the value of the current row.
def row = testRunner.testCase.testSteps["NinjaDataSource 2"].currentRow

// Now return the value as long as it is within the list.
if ( row + 1 <= context["allRows"].size() )
{
    result["randomRow"] = context["allRows"][row]
}

Your test steps use the data and a data source loop to iterate it.
 

👉Now, this is what your test case will look like:

output

Detect Load Test Runs

The process of detecting load test runs describes how to use a Groovy script to find out if our test case is running as a load test or not.

detect load run test

We have several ways to implement this using script:

⭐We can run the script as an event handler.

In functional tests, we can run the script as a setup or teardown script on the project, test suite, or test case level.

In performance tests, we can run the script as a setup or teardown script.
 

👉The following script will demonstrate how to determine if a test case is running in the command line or as a load test:

// To check whether the test runs from the command line.
if( com.eviware.soapui.SoapUI.isCommandLine() )
{
    log.info "This code is executed because of the command line in SoapUI"
}

// To check whether the test runs as a load test.
if( context.LoadTestContext != null )
{
    log.info "This code is executed from doing a load test"
}

Encoding Attachments

Now coming to the part you want to know more about. How to encode attachments? ReadyAPI allows us to use Groovy scripts to work with attachments – for example, we can extract data from the text files, change their encoding, and much more.

We will use a Groovy script to encode attachments too.

encoding with attachments

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.

⭐In security tests, we can run the script as a setup or teardown script.

⭐In virtual services, we can run the script as a setup or teardown script in performance tests.

⭐We can run the script as a Start, Stop, OnRequest, or AfterRequest script in virtual services.
 

👉The following code will demonstrate how to encode the specific file with base64 in ReadyAPI:

// Replace the file path with the one you need

// Get the file content
def inputFile = new File("C:\\Work\\MyFile.txt").getText('UTF-8')

// Encode the file content
String encoded = inputFile.bytes.encodeBase64().toString()

// Output results
log.info encoded // with the Groovy Script test step, you can use “return encoded”

Getting the Current Date

We can use Groovy scripts to get the current date too. We can use this script anywhere in ReadyAPI. It is an object which is available at script assertion. 

This script was part of the standalone Soap UI Pro, which is now a part of the Ready API application suite.

getting the current date

👉Example

def today = new Date()
def formatofDate = today.format("dd/MM/yyyy")
log.info(formatofDate)

Frequently Asked Questions

What are the two types of data sources?

The two types of data sources are file data sources and machine data sources. Well, both contain similar information about the source of the data, they only differ in the way this information is stored.

Why is a load run test needed?

Load testing is usually performed to check a system's behaviour under both normal and peak load conditions. It helps to figure out the maximum operating capacity of an application and determine which element is causing degradation.

What type of data sources can be used in ReadyAPI?

We can use various types of data sources. Some of these are, data connection, data Generator, excel, file, JDBC, grid, and groovy.

Conclusions

This article briefly discusses encoding attachments and data sources in ready API. We started by working with data sources and then detected the load test runs. We also encoded attachments and examined an example of getting the current date to our ready API.

We hope that this blog has helped you enhance your knowledge about encoding attachments and data sources in ready API and if you like to learn more, check out our articles,

🌐What are Test Steps in ReadyAPI? 

🌐Change Authentication Profiles in Ready API

🌐Security Tests in ReadyAPI 

🌐Running Tests in ReadyAPI 

🌐Managing test items in Ready API 

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScript, and many more! If you wish to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! 

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 problemsinterview 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!

Happy Learning!

Live masterclass