Simple Automated scripts with Karate Framework: Database connection example
Before to start talking, How I have learned and show an example How to connect on database with Karate Framework, let me introduce It. Karate is an open-source tool which combine API test-automation, mocks, performance-testing and even UI automation into a single, unified framework. It works with Gherkin language, and It is easy for even non-programmers.
- Who: Karate Framework
- Why: If you don’t have a framework and you would like to write scripts simple and have fast feedback about API under test… Karate is an excelent tool. For me, It works like a Postman in Java, and It is excelent for people who don’t have a big programming background.
- Where: Now let me share some sites, which provide me knowledge about Karate:
- https://github.com/intuit/karate: Official site, with information about How to install and many examples;
- https://www.youtube.com/channel/UCCBL8gbYRJkkngjcn8lyO7A : For me, the best Karate Framework Channel. The best examples and many suggestions to extract better results from Karate;
- https://venkatramanareddy.github.io/karate/: Faster guide to solve questions;
Example: This is an example how to send some information for some endpoint and validate data processing with a database query
Feature: Access a database to validate something
Background:
Scenario Outline: Send a payload and validate something in a database
header Authorization = call read(‘header.js’) { username: username’, password: ‘password’ }
* def sourceID = function(max){ return Math.floor(Math.random() * max) }
* def sourceValue = ‘Test-’ + sourceID(1000000)
* def xml = read(‘Payload.xml’)
* set xml/CommunicationInputMessage/sourceId = sourceValue
* set xml/CommunicationInputMessage/value[1] = sourceValue
* set xml/CommunicationInputMessage/variable[2]/value = sourceValue
* set xml/CommunicationInputMessage/value[1] = ‘<Locale>’
* set xml/CommunicationInputMessage/filter[2]/value = ‘<Country>’
#Send a message for some place
Given url appUrl + ‘/send’
And request xml
When method POST
Then status 200
#Connect with database
* def config = appUrlDB
* def DbUtils = Java.type(‘examples.DbUtils’)
* def db = new DbUtils(config)
# Validate Data
* def notification = db.readRow(‘select * from history where id = \’’ + sourceValue + ‘\’’)
#Scenario for two countries
Examples:
| Country | Locale |
| US | en_US |
| BR | pt_BR |