Scriptworks Logo

GraphQL - Full Response Body Validation

12 May 2021

This article covers creating a GraphQL Query and validating a Full response Body using Scriptworks.

Introduction

In previous articles, we have validated single values and multiple values received from a GraphQL API on Scriptworks. 

This time we will learn how to validate extensive data from the response body in Response to GraphQL API on Scriptworks. Let’s Explore it right away.

Note - Here, We are using https://counties.trevorblades.com to practice GraphQL queries and response validation.

Exercise:-

1) Navigate to https://countries.trevorblades.com/

Navigate to GraphQL Practice WebSite

2) Write a GraphQL query on the left-hand side portion to generate responses.

Put GraphQL Query on left hand side

3) Observe the response received on the right-hand side.

See the Right hand side for Response

Practical Demonstration

Following are the Steps to Perform the above Exercise and perform validation using Visual Programming on Scriptworks :-

Pre-requisite: Users should be able to login to Scriptworks via their Credentials, and his Scriptworks Client should be up and running. 

To know more about Scriptworks Client and Selenium Standalone Server, please follow this Article

1) Open Scriptworks on your system with your credentials and open the project where you want to perform the Automated validations on the GraphQL API response.

Open Project MyTest

2) Now click on the Block symbol present on the left side panel and then click on getting started 

BlockSymbol → Getting Started → API Test

As we know, we are going to initiate an API test. So Drag and Drop “API Test” on Canvas After clicking on the Getting Started link.

put your cursor in search box

Putting API Test Block into the Canvas

3) Now, we will use the comment block to let others know what we will perform. For that, we are going to click on the search text box and write comments and hit return.

Search → Comment → Drag and Drop to Canvas

put your cursor in search box

Put Comment Block into the Canvas

GraphQL query comment in comment block

4) Now, we will drag and drop some basic blocks required for a GraphQL API to request the resource. 

For a GraphQL API Request, we need four basic things from Scriptworks.

i) GraphQL Call Block

ii) URI Block

iii) GraphQL Query Block

iv) Response Block 

We have searched the comment block in step 3. Similarly, we have to do the required blocks and drag and drop them to canvas.

  1. Search → GraphQL Call → Drag and Drop to Canvas
  2. Search → URI → Drag and Drop to Canvas
  3. Search → GraphQL Query → Drag and Drop to Canvas
  4. Search → get Response → Drag and Drop to Canvas

Demonstrating Drag and Drop of GraphQL to Canvas

Search GraphQL Block from left hand side panel

Put GraphQL Call Block into the Canvas

Demonstrating Drag and Drop of URI Block into Canvas

URI Block Search

Put URI Block inside the Canvas

In URI Block Enter “https://countries.trevorblades.com/

Put URL inside URI Block

Demonstrating Drag and Drop of GraphQL Query Block into Canvas

Search Graph QL Query on the left hand side panel

Now Enter the GraphQL query inside the GraphQL query block, for this exercise we are using the following query:

{ languages {code name native } }

Enter GraphQL Query Inside the Query Block

Demonstrating Drag and Drop of get response Block into Canvas

Search for Get Response from Left hand side panel

Putting get response block into the canvas

5) Let’s give it a dry run and see what we are receiving in the response after the run.

In the above screenshot, we can see what request we have sent to GraphQL API through GraphQL query with the kind of request headers that have been passed.

Response Details with Response Headers

In the above screenshot, we can observe values from the GraphQL API in the Response Body. 

As we can see, the Response Body is too long. Then what to do for validating such large responses in the response body. Let’s figure it out right away.

6) Drag comment block to let others know what we are going to do now. And for that, follow Step 3 Above.

Search → Comment → Drag and Drop to Canvas

Comments for Validating response inside canvas

7) Drag saves data to file block into the canvas and enters the location plus file name inside the block, then drag response block and attach to save data to file block and give a test run.

Search → save data to file → Drag and Drop to Canvas

Let’s give the name of the file where we want to save the GraphQL API response. For Demonstration purposes, I am giving it the name “actualresponse,” keeping its extension type as JSON as the response will come in JSON, so the file’s name would be “location-on-local-machine/actualresponse.json,” as shown in the screenshot below: 

Search Save Data to file from left hand side panel

The above shown blocks in the given screenshot will create a file in the Documents folder on the dry run. 

Dry run to save file

saved file in documents folder named as actual response

Now to validate the large chunk of response, or we can say in technical terms, i.e., to Assert the response received in Scriptworks. 

Re-name the file to expectedresponse.json, which is present in the documents folder. 

renamed the file to expected response in documents folder

Now Pull an assert block into the canvas and search for load data from file block and assert it with the response body during the test run.

Search → load data from file → Drag and Drop to Canvas

search load data file from left hand side panel

using assert with renamed file

8) Now, the final thing remaining is to Run the Test. Make sure you have followed the prerequisite mentioned above before the Test Run. Make sure you can see Green Symbol before clicking on the play icon. 

Play button on top and scriptworks panel showing green icon

Test Pass

That’s how we can validate the GraphQL API Response body in full using Scriptworks.

Scriptworks logo
© Copyright 2024 | Scriptworks is part of Odin Technology Ltd, a company registered in England no. 03735083