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

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 
  • Gherkin like syntax, easier to read write tests. 
  • No step definitions required to be written. 
    • Lesser lines of codes 
  • Built in support for data driven testing 
  • Run test in  parallel 
  • Integrate with CI/CD 
  • UI automation
  • Future versions to include support for Desktop and Mobile automation 
  • Reuse your existing karate scripts to run performance tests 
  • QuickStart using maven archetype or a zip release
Introduction to Karate is also available on my YouTube channel

What is Maven?

Maven is a build automation tool used primarily for Java projects.

What is Maven Archetype?

Archetype is a Maven project templating toolkit. An archetype is defined as an original pattern or model from which all other things of the same kind are made.

IDE?

While any IDE can be used, for this tutorial I'll be using IntelliJ IDEA
This  IDE is provided by JetBrains and we will use the community edition (for Windows OS) for our tutorials on API testing with Karate

How Do I start testing with Karate?

Prerequisites

  • JDK 1.8 is installed and added to PATH system variable
  • Maven is installed and added to PATH system variable
  • Download and install IntelliJ IDEA community edition

STEP 1 - Project Setup using Maven Archetype

  • Launch IDE and then use the Create New Project feature 
  • Click on Create New Project
  • On the Next Screen select Maven 
  • In the Project SDK field I've JDK1.8 selected
  • Then place a tick in the Create from archetype checkbox
  • Click on Add Archetype
  • In the Add archetype Pop-up add the following details:
    • GroupId      : com.intuit.karate
    • ArtifiactId  : karate-archetype
    • Version      : 0.9.5 (or latest available release)
  • After entering the details click on OK button.
  • You will see the Archetype now listed.
  • Select the just added karate-archetype and click on Next
  • Enter the details for your New Project and click on Next button
Add Project details

  • Review the details and click on Finish button
Wait for your newly created project to open and allow the indexing and build processes to complete and once it is completed you should be getting a BUILD SUCCESS message. 

Within the generated project structure, a sample test and a test runner file is also included and now the pom.xml should look like this

Here's a video from my YouTube channel to help you with quickly setting up Karate framework for API testing

STEP 2 - Install plugins

Install suggested Plugins for 
  • Syntax highlighting and 
  • To Right click within the .feature file and Run ASFeature
Initially Gherkin and Cucumber for Java plugins should be installed.
For a Video tutorial on how to install plugins required check out this video


Sample test available in the project provides a sample that gives a brief idea about the DSL.

Unlike other BDD frameworks, where a step definition is required for every feature file, here we see that no such thing is required. To begin with even if you do not have any programming experience it is very easy to get started with Karate.

What is a Feature file?

  • Every .feature file conventionally consists of a single feature. 
  • e.g.  users.feature file in the users directory
  • A line starting with the keyword Feature followed by free indented text starts a feature.
  • IT has Scenario/s
  • Every scenario consists of a list of steps
  • Steps start with keywords (Given, when, Then etc.)
  • It may also contain a background, scenario outline and examples

In the sample test there are a couple of scenarios, one tests a GET request and the other tests a POST request.
  • Running this is pretty simple and there are multiple ways that you could run this:
  • Right click inside the feature file in the feature section and select the option Run 'Feature:users'
  • Right click inside the feature file in the Scenario area and run a particular scenario only
  • Open a terminal and type the command mvn test. 
    • This will pick up all the test classes that follow the *Test.java convention
    • In this case we have the runner file ExamplesTest.java that will be picked
    • Once it is completed you should see an output similar to this


  • Other option is to run a specific file using the command  mvn test -Dtest=UsersRunner
    • In this case also once the run is complete a report should be generated.
    • Path is printed in the terminal.


UserRunnerReport
Report
Additionally there is also a Timeline report generated in target/surefire-reports
Timeline Report
Looking at sample tests and how easily Karate can be setup, it certainly makes a case for its tag line Test Automation Made Simple.


Trouble shooting:

If you face maven errors while trying to generate karate project using IDE then follow the below steps to generate it via command line.

Open command prompt in the directory you want to generate your project in. First verify if Maven is installed correctly and available in path environment variable. This can be done by typing the command
mvn -v
It gives below output
C:\Users\Kalimoh>mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f
Maven home: C:\Program Files\apache-maven-3.6.3\bin\..
Java version: 1.8.0_251, vendor: Oracle Corporation, runtime: C:\Program Files\java\jdk1.8.0_251\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

Then type in the below command
mvn archetype:generate -DarchetypeGroupId=com.intuit.karate -DarchetypeArtifactId=karate-archetype -DarchetypeVersion=0.9.5 -DgroupId=com.karateTraining -DartifactId=KaratePractice 
You will be prompted to Define value for property 'version' 1.0-SNAPSHOT: :
Define value for property 'version' 1.0-SNAPSHOT: :1.0-SNAPSHOT
Confirm properties configuration:
groupId: com.karateTraining
artifactId: KaratePractice
version: 1.0-SNAPSHOT
package: com.karateTraining
Y: :Y
Enter a value and Confirm the Properties presented by entering Y
Your project should be created in a short while

References:
Karate-github page
Karate Features
Maven Archetypes Introduction 


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