Skip to content
/ KarateTemplate Public template

An "abridged" version of Karate Documentation.

Notifications You must be signed in to change notification settings

jawadkalia/KarateTemplate

Repository files navigation

Pre-requisite Software:

Please check the FAQ section for troubleshooting use ctrl+click on links

  • Required

    • Karate requires Java 8 (at least version 1.8.0_112 or greater) and then either Maven or Eclipse to be installed.
    • Git Installation
    • Docker installation
      • A nice video intro to docker concepts on windows
      • If you have a windows machine. please do the steps in this blog and then download docker installer and run it. Also consider switching to a mac.
  • Suggested

Introduction

Karate is a behaviour driven automated testing framework designed to test all kinds of web services using the Cucumber Gherkin Syntax. Karate requires Java 8 (at least version 1.8.0_112 or greater) and then either Maven, Gradle or Eclipse to be installed.

Docker Is used to run the API tests either locally or in a CI/CD environment

Jenkins Is the CI/CD tool to run karate tests on a scheduled daily runs. With trend reporting. We will be using the Jenkins Pipeline Script to build our jobs. This a devops owned tool and therefore please contact your devops liasion for getting access and creating jobs.

Setting up Repository

  • Fork this repository
  • Rename the 'myproject' directory according to project name
  • Update pom.xml file using the comments in the file as guidelines.
  • Update karate-config.js using comments as guidelines. for documentation please click here.
  • Update TestParallel.java file, using the comments in the file as guidelines.
  • Update docker-composeQA.yml files accordingly, using the comments in the file as guidelines.
  • Update .gitignore files accordingly, using the comments in the file as guidelines

Add your own test!

Please make sure to follow the Folder Structure Guidlines.

  • Navigate to examples directory
  • Create a new directory posts
    • Recommendation: Make a directory per endpoint
  • Navigate to newly made directory
  • Create a PostsRunner.java file
    • The DirectoryRunner.java file should have the following contents

      package examples.posts;
      import cucumber.api.CucumberOptions;
      import com.intuit.karate.junit4.Karate;
      import org.junit.runner.RunWith;
      
      @RunWith(Karate.class)
      @CucumberOptions(tags = {"@debug","~@ignore"})
      public class PostsRunner {} // please update the class name accordingly
      
      • Please note:
        • CucumberOptions allows you to run or not run specific tags. so in the above code it will run all files in the directory which has the @debug attached to it either on the Feature or Scenario
    • Create a Feature file posts.feature, with the following contents

      @debug // Since we want to run this feature file using the DirectoryRunner Test class
      Feature: Testing calls to posts endpoint
      
      Background: This is basically the area where you set up Test Data and declare variables that are available to every Scenario. The Background Section runs before every Scenario
      
      * url baseUrl // setting up the baseUrl for tests. please check karate-config.js file
      
      Scenario: Testing Get call to posts endpoint
      
          Given path 'posts'
          When Method GET
          Then status 200 // this is an assertion step as well
      
          // karate provides match keyword for Assertions
          Then match response[0].id == 1
      
      
    • Run the test

      • In your terminal type mvn clean test -D test=PostsRunner. The test will run using the karate's own built in runner class will give out a debugging html report in the console output
      • It is recommended to remove the @debug tag after completing scripting of a test

Congratulations! you have taken the first step towards Karate Automation Engineer 😤

Run Tests using Maven

Open console in the myproject directory

To run all tests in a specific environment mvn clean test -DargLine="-Dkarate.env=qa" just change the karate.env value to the needed environment.

To run a specific test in a specific environment mvn clean test -DargLine="-Dkarate.env=qa" -D test=dashboardrunner

Default environment is qa therefore mvn clean test will run all tests in parallel in the QA env

and

mvn clean test -D test=dashboardrunner will run the specified suite in the QA env

Running all tests will generate a Pretty Html Report under the target directory. Running single tests will generate a report. the path to which will be in the console output.

Run Tests using docker-compose:

Docker provides us a way to run our tests anywhere, only requirment? Latest Version of Docker is installed!

A reference to the docker-compose.yml file syntax can be found here

From your console docker-compose -f docker-compose.yml up

Docker is 🆒

Learn Karate! ✍️

Must read Sections from the original karate documentation

Karate uses Json-Path out of the box to retrieve, set, remove and replace data from a JSON. This Document is good to learn jsonpath. And http://jsonpath.com/ to test your Json paths

FAQ

  • For docker-compose, getting error Cannot create container for service karate

In docker settings in shared drives section, make sure you have shared drive enabled. docker/for-win#77 (comment)

  • Hyper-V errors when running Docker on windows follow this

  • Docker for Windows error: Hardware assisted virtualization and data execution protection must be enabled in the BIOS solution

Karate has a dedicated TAG on Stack Overflow and you can reach out to me and my colleagues!

Authors Note: Karate is developed by Peter Thomas. I recommend very strongly to go through the karate documentation and look the Karate Demo Examples.

Enjoy!

[Jawad Kalia] 😎

About

An "abridged" version of Karate Documentation.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published