How to post-process the HAR file as JSON using Jayway JsonPath
Additionally, I wanted to automate verifying the content of HAR file. I expect that the sample.har
file contains a record of a HTTP request to a url that ends with jquery.min.js
.
{
"log": {
...
"entries": [
...
{
"request": {
"method": "GET",
"url": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.min.js",
...
}
}
...
]
}
}
How can I do it?
There could be many ways to meet this requirement.
I tried to solve this problem using Jayway JsonPath library.
You can find my solution in the code of Test Cases/process_har_with_jsonpath.
The following screenshot shows how the demo TS1
runs with verifying HAR file.
The Test Case process_har_with_jsonpath
verifies if the sample.har
file contains a URL that ends with jquery.min.js
. Of course you can modify the condition as you want. Please study the code and the Jayway JsonPath documentation.
How to import required jar files.
Katalon Studio does not bundle the jar files required to use the Jayway JsonPath. You need to import external jar files from Maven Central repository into the <projectDir>/Drivers
folder. The following document explains how to do it:
To do this, you need to install Java and Gradle into your machine. The following post has an instruction how to:
Once you installed Java and Gradle, you want to write <projectDir>/build.gradle
as follows:
plugins {
id 'java'
id "com.katalon.gradle-plugin" version "0.1.1"
}
repositories {
mavenCentral()
}
dependencies {
implementation group: 'com.jayway.jsonpath', name: 'json-path', version: '2.7.0'
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.6'
implementation group: 'org.slf4j', name: 'slf4j-simple', version: '2.0.6'
}
And you want to execute in the command line:
First, you want to change the current directory to the home directory of your Katalon project. For example:
$ cd ~/BrowserMobProxyInKatalonStudio
then you want to do:
$ gradle katalonCopyDependencies
then you will see some jar files are imported into the Drivers dir:
$ ls -ls Drivers
total 1240
drwxr-xr-x 8 kazuakiurayama staff 256 2 11 11:55 .
drwxr-xr-x 31 kazuakiurayama staff 992 2 11 11:21 ..
-rw-r--r-- 1 kazuakiurayama staff 29489 2 11 11:21 katalon_generated_accessors-smart-2.4.7.jar
-rw-r--r-- 1 kazuakiurayama staff 121790 2 11 11:21 katalon_generated_asm-9.1.jar
-rw-r--r-- 1 kazuakiurayama staff 271159 2 11 11:21 katalon_generated_json-path-2.7.0.jar
-rw-r--r-- 1 kazuakiurayama staff 119227 2 11 11:21 katalon_generated_json-smart-2.4.7.jar
-rw-r--r-- 1 kazuakiurayama staff 62531 2 11 11:21 katalon_generated_slf4j-api-2.0.6.jar
-rw-r--r-- 1 kazuakiurayama staff 15239 2 11 11:21 katalon_generated_slf4j-simple-2.0.6.jar
Now you are ready to run the Test Suites/TS1
which indirectly execute the Test Case ‘process_har_with_jsonpath’.