Skip to main content

Performance testing with Karate Gatling

What is Karate Gatling? Github page of this project has the tag line API performance testing made simple . Simply put this should help in performance testing of the api test written using Karate by leveraging performance testing capabilities of karate. On reviewing the documentation available on GitHub following capabilities are listed: Re-use karate tests as performance tests executed by Gatling Gatling and scala required only for defining the load model Everything else can be defined in Karate  Karate assertion failure also appear in the Gatling report Anything that can be written in Java can be performance tested. Option to scale out over multiple hardware nodes or Docker containers In this post we will see how to reuse Karate tests for execution using Gatling We will see step by step on how to setup Karate Gatling performance tests. There are a few prerequisites for setting up your karate Gatling tests. Prerequisites: JDK should be installed and availabl...

Getting Started with Karate - ZIP release

In the last post we saw what is Karate? and Why to use Karate. In this post we will see one of the simplest way of setting up and getting started with Karate framework.

Usually when someone is starting their journey towards learning automation, then one of the most difficult things is doing the various setups before they can even get started.
Karate although a recent entry as compared to some of the other automation tools already out there for a long time, has taken a clever approach that adresses these issues that are faced during busy schedules .

I'm listing down few of the statements that have come up during my discussions with various people of the Testing community. These are the pain points that they encounter on a regular basis and in some extreme cases have pushed them away from even starting their automation journey.

Problem statements

  • Lots of time is wasted in setup of tools or the framework, when your target is to complete only a small POC
  • In most places there are many restrictions on what can and cannot be installed, hence we cannot implement / learn new thing
  • Setting up a demo of a new tool is a very time consuming
In this post we'll see how Karate addresses these concerns. So, lets dive right in

What is ZIP release?

"Karate can be used as a stand-alone executable, ideal for teams that don't want to use a Java IDE"
 
Within the ZIP file is present the Karate executable jar file which contains all of the important features. It also contains  sample for -
  • Api automation
  • Mocks
  • Web / UI automation
  • Desktop automation

Where to Get the ZIP release?

 I prefer downloading from the Bintray  as it is easier to find the latest release at this location. From here you can either download the 
  • Latest stable Karate Zip release 
  • Or just the Karate jar file
  • Or the latest rc version (with rc in the version number) that have experimental features of Karate
  • Or the Karate-robot for desktop automation
 
Bintray - Latest version download from here
Bintray


 
Download the Karate-<latest version available>.zip file

What Next?

 Extract the jar file
Unzip File
Unzip File
And the folder structure at the root level would look like this
Folder structure
Folder structure

 
Tree structure

As seen here samples for API / Mock / Web / Robot (desktop) automation are already included in the ZIP release.
There's just one more thing and we would be ready to explore Karate
 For getting started there are only a couple of things required
  • Zip file
    • This step is already completed
  • Jre
    • Verify that Jre is installed in your PC and is available in the Path variable
 
D: \karate\karate-0.9.6> java -version
openjdk version "11.0.9" 2020-10-20
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9+3-202008212340)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9+3-202008212340, mixed mode)

That's it and now we are ready to explore Karate
  • At this stage having an IDE is entirely optional
    • Although IDE's are very helpful, but it is not a necessity and shouldn't stop you from learning new things.
    • Samples available can be run from the command line

Before we begin a bit of History:

During the initial releases of the zip release version of Karate, for ease of use there were a few changes that were required to be made before we could start using the terminal. But now no changes are required and using it even via command line is very straight forward.
 
 Open the command prompt in the root of the project and type karate -h to get a list of commands


So what next? How do I run the included samples?

It's pretty simple. Type Karate followed by the path of the file that you need to run. 
E.g  For windows OS if I need to run the API sample I'll run the command karate src/demo/api  and hit the Enter key:

You should now see the the test getting executed and at the end of the run you would see something like this
Api test results printed at the end
Results for the API test

At the End we can see the summary for the test and also the url to the new HTML report.
Just copy the path and open it in a web browser of your choice.
 
Summary report
Summary report

By default the report opens in Summary view. To view the detailed report click on the link under the feature column (displaying the path and name of the feature file)

Details for the Feature run
Detailed Report

The Number and characters in blue are clickable, and when you click on them these show more details related to the step. By default in the report the Background details are hidden. If you wish to see it then click on the >> and then the number under it to view the details.

All this is great, but wouldn't I need any more libraries for running the web / UI automation tests?

Answer is both Yes and No.
 
If your aim is to just get started with Karate or just demo the features of Karate then you do not need anything else.
Karate implements the Chrome dev tools protocol for UI automation. So what it means is that if you have chrome installed in the default location of your system then you don't need any additional things. Just type in the command and run the test. Once you have got hang of things then you can tryout cross-browser tests with W3C WebDriver implementations including Microsoft Edge on Windows and Safari on Mac
Lets see how to do it, once again open the command prompt in the root of the project and type the command karate src/demo/web/google.feature  and hit the Enter key

Summary - UI test run
Summary UI test run


HTML Report - UI Automation Details

Why does the mock directory contain 3 files? and how to run mocks? 

cats-mock.feature - This is your mock server
cats.html - This is the HTML client
cats-test.feature - Contains test which can be run against the mock.
 
First we need to start the server, for this use the command  karate -p 8080 -m src/demo/mock/cats-mock.feature

Start - Mock server
Start - Mock server

Now open the  cats.html file in your favorite browser
HTML client - Default State
HTML client - Default State
By default name Billie is available in the Name field.
Now if I click on Create Cat button, a cat named Billie would be added to the mock and the entry would be displayed on the page
Cat created
Cat data created
 
To view the raw json response click on GET /cats
Raw json
Raw json

 To run the cats-test.feature type the command karate src\demo\mock\cats-test.feature
 Once the test completes its run, it displays summary of the run
Mock-Test Final Results
Mock-Test Final Results

Raw json response
Raw json response


Mock Test HTML result
Mock Test HTML Report
 
 P.S. ZIP release also contains sample tests for Desktop automation, which we will explore in another post.
 
Outro:
So if you see getting started shouldn't take much time and also there are not many dependencies that you need to run the sample tests for a quick demo.
 
In case if you feel the need for IDE, any open source IDE or community edition with support for Java should do fine. 
Use this post for - Quick Start with Maven Archetype which has information on getting started with Karate using Maven+Java with IntelliJ IDE.
Here's a video on how to use VsCode for ZIP Release.

 
Here's a Video on how to write your first test

 

References:

Comments

Popular posts from this blog

Performance testing with Karate Gatling

What is Karate Gatling? Github page of this project has the tag line API performance testing made simple . Simply put this should help in performance testing of the api test written using Karate by leveraging performance testing capabilities of karate. On reviewing the documentation available on GitHub following capabilities are listed: Re-use karate tests as performance tests executed by Gatling Gatling and scala required only for defining the load model Everything else can be defined in Karate  Karate assertion failure also appear in the Gatling report Anything that can be written in Java can be performance tested. Option to scale out over multiple hardware nodes or Docker containers In this post we will see how to reuse Karate tests for execution using Gatling We will see step by step on how to setup Karate Gatling performance tests. There are a few prerequisites for setting up your karate Gatling tests. Prerequisites: JDK should be installed and availabl...

Getting started with karate-config.js

In this post we will see how to add an configuration file (karate-config.js) to our sample karate project. This configuration file is similar to the properties file that you would have used for other automation projects.  I'll be using the Karate-Zip release version for this post too. Still unaware about it, check out my last post . When using the config file we need to understand a few basics about it: When Karate starts-up it expects a file called karate-config.js in the classpath This config file should be a Javascript function . The function is expected to return a json object All Keys and values in the json object will be made available as script variable So what is classpath when using the Karate Standalone Jar or the Zip release version of Karate? To understand this lets open the karate.bat file and try to see what it contains, it has this command java -cp karate.jar;. com.intuit.karate.Main %* So com.intuit.karate.Main is the entry point for the Karate command line app A...

Karate - Quick Start with Maven Archetype

 What is Karate? Karate enables you to script a sequence of calls to any kind of web-service and assert that the responses are as expected.  Karate provides an easy to use DSL which natively supports json and xml. Karate is not just limited to API automation. After the initial releases which supported API automation, now the capabilities include Performance testing, UI  automation, Desktop automation (a release candidate was made available just a few weeks ago with this capability). Why Karate?  Is based on the popular Cucumber / Gherkin standard, and IDE support and syntax-coloring options exist  You can run tests and generate reports like any standard Java project. But instead of Java - you write tests in a language ( DSL ) designed to make dealing with HTTP, JSON or XML Java knowledge is not required and even non-programmers can write tests Embedded JavaScript engine that allows you to build a library of re-usable functions  Gherki...