Karate- Automation with Gherkin Syntax

Karate- Automation with Gherkin Syntax

23 September 2020

Karate

Karate is an open source tool which can combine API automation, performance testing and UI automation into a single framework.

Karate framework is flexible to both programmers and non-programmers. Programmers can use Java to create the maven project and can configure the required test cases. Non Programmers can download the zip file and can use the zip file with the Karate extension of Visual Studio Code.

Gherkin Syntax

Gherkin is the format for the cucumber specifications. It describes the exact business behaviour without having the knowledge about the implementation. It is object oriented programming which uses indentation to define the structure.

Structure

Feature: brief description of what is being tested more lines of description if needed. Background: # this section is optional ! # steps here are executed before each Scenario in this file # variables defined here will be 'global' to all scenarios # and will be re-initialized before every scenario Scenario: brief description of this scenario # steps for this scenario Scenario: a different scenario # steps for this other scenario

Lines that start with a # are comments.

Background:

It is used to execute the set of statements before every scenario. It is useful whenever there is some data to reinitialise before every scenario. It is described inside the feature file.

Scenario:

It is a kind of event which specifies the set of actions.Karate runs different scenarios in parallel if there are multiple scenarios in a single feature file.

Feature File:

Feature file is the entry point to the Karate tests. This is the file where the tests will be described with different scenarios.

karate-config.js

It is a javascript file which is used to set different environmental variables. These variables are the global variables which can be accessed in every feature file.

Variables

Karate contains different types of variables. The following is the list of supported variables:

  • def
  • text
  • Table
  • csv
  • string
  • JSON

def: It can override any variable that was using the same name earlier.

* def myNum = 5

Text: Karate supports importing the text files and stores the data into a text variable.

* text query = read(‘input.txt’)

Table: It is a simple way to create json arrays.

* table cats | name | age | | 'Bob' | 2 | | 'Wild' | 4 | | 'Nyan' | 3 | * match cats == [{name: 'Bob', age: 2}, {name: 'Wild', age: 4}, {name: 'Nyan', age: 3}]

csv: It is used to create csv to json arrays.

* text foo = """ name,type Billie,LOL Bob,Wild """ * csv bar = foo * match bar == [{ name: 'Billie', type: 'LOL' }, { name: 'Bob', type: 'Wild' }]

String: Converts JSON or any datatype to string.

* def foo = '10' * string json = { bar: '#(1 * foo)' } * match json == '{"bar":10.0}'

JSON: convert XML, a map-like or list-like object, a string, or even a Java object into JSON.

* def json = {a: 1}

Send Http requests

Karate supports all the types of https methods to send requests and to catch the response.

Given url https://google.com When method get Then status 200

Assertions

Karate supports different assertions to validate the response obtained from the http request. The following are the different assertions.

  • Match ==
  • Match !=
  • Match contains
  • Match !contains
  • Match each
  • Match header
  • Match schema

Example:

Given url https://google.com When method get Then status 200 Then match header Content-Type == 'application/json' And match response.id == 1 And match response.status != 1 And match response contains {status: true} And match response !contains {status: 1} And match each response.foo == { bar: '#number', baz: '#string' } * def schema = {a: ‘#string’, b: ‘#number’} * def data = {a: ‘test’, b: 123} And match data == schema

Chaining Requests

Chaining requests is quite simple when compared to other frameworks. Karate supports passing response data to the other http requests configured in a feature file.

Example:

Scenario: Given url https://google.com When method get Then status 200 Given url https://google.com Then path ‘id’, response.id

Import data files

Karate supports importing the data from the json, text, csv, xml files.

* def importData = read(‘input.json’)

Write data into File

* def exportData = write(data, ‘input.json’)

Call one feature file in another

 * def feature = call read(‘feature1.feature’)

Import Javascript functions

* def currentDate= “”” Function () { var date = new Date(); return date } “”” * print ‘current date is:’, currentDate();

Click on the link to find the detailed description of the document.

Request a quote