I am trying to run some basic database tests using Karate and Spring JDBC ( as mentioned in DBUtils class in Demo Project).
Here is what I am doing:
Background:
# Read url/username/pwd and provide it to the class
* def config = read('env_file.json')
* def DbUtils = Java.type('DbConnection')
* def db = new DbUtils(config)
Scenario: Validate the modd_ts is present in lob table
* def createdTs = db.readRows('SELECT crtd_ts FROM tableA ')
* print createdTs
Here is what it returns:
com.intuit.karate - [print] [{"crtd_ts":{"nanos":0}},{"crtd_ts":{"nanos":0}}
Not sure why it should return timestamp as "nanos":0 , seems a bug to me . Please confirm and I will open one in github.
This is not a problem with Karate it is the implementation of DbUtils. It is up to you to write something that is right for your environment and database etc. Remember this is just part of the karate-demo as an example.
Related
Want to know if Karate supports Neo4j database?. If yes, would like to have an ex. feature which will be helpful.
Karate supports any Java code so that way indirectly you should be able to do anything you want.
Please look at this JDBC example which will get you started: dogs.feature. You will need to write a little bit of Java code (one time only) so if you don't have that skill, please ask someone to help.
# use jdbc to validate
* def config = { username: 'sa', password: '', url: 'jdbc:h2:mem:testdb', driverClassName: 'org.h2.Driver' }
* def DbUtils = Java.type('com.intuit.karate.demo.util.DbUtils')
* def db = new DbUtils(config)
# since the DbUtils returns a Java Map, it becomes normal JSON here !
# which means that you can use the full power of Karate's 'match' syntax
* def dogs = db.readRows('SELECT * FROM DOGS')
* match dogs contains { ID: '#(id)', NAME: 'Scooby' }
I am trying to invoke a feature for each element of json array
* def values = karate.mapWithKey(values, 'value')
* def result = call read('my-feature') values
My feature is defined as
#Ignore
Feature: My feature
Background:
*some task
Scenario:
# TEST: My scenario
Given path urlPath, value
This works fine if i use #Tags and only run this scenario.
But on trying to run all karate tests, this fails with error
com.intuit.karate.exception.KarateException: my-feature.feature:15 - javascript evaluation failed: value, ReferenceError: "value" is not defined in at line number 1
How do i fix this?
I have marked the ignored feature as #Ignore, but that doesnt help
Got the solution ,
I was using #Ignore annotation but it also need to be mapped at APITest class
Defining
#KarateOptions(tags = {"~#Ignore"})
And marking the feature file as #Ignore, solved my issue
Shouldn't it be:
* def result = call read('my-feature') ids
If still stuck, follow this process please: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
Strange behaviour when I call a feature file for test clean using afterFeature hook. The cleanup feature file is called correctly because I can see the print from Background section of the file, but for some reason the execution hangs for Scenario Outline.
I have tried running feature with Junit5 runner and also in IntelliJ IDE by right clicking on feature file but get the same issue, the execution hangs.
This is my main feature file:
Feature: To test afterFeature hook
Background:
* def num1 = 100
* def num2 = 200
* def num3 = 300
* def dataForAfterFeature =
"""
[
{"id":'#(num1)'},
{"id":'#(num2)'},
{"id":'#(num3)'}
]
"""
* configure afterFeature = function(){ karate.call('after.feature'); }
Scenario: Test 1
* print 'Hello World 1'
Scenario: Test 2
* print 'Hello World 2'
The afterFeature file:
#ignore
Feature: Called after calling feature run is completed
Background:
* def dynamicData = dataForAfterFeature
* print 'dynamicData: ' + dynamicData
Scenario Outline: Print dynamic data
* print 'From after feature for id: ' + <id>
Examples:
| dynamicData |
The execution stalls at Scenario Outline. I can see the printed value for dynamicData variable in console but nothing happens after that.
Seems like the outline loop is not starting or has crashed? Was not able to get details from log as the test has not finished or there is no error reported. What else can I check or what might be the issue?
If not easily reproducible, what test cleanup workaround do you recommend?
For now, I have done the following workaround where I have added a test clean-up scenario at the end of the feature that has tests. Have stopped parallel execution for these tests and to be honest I do not mind these tests not running in parallel as they are fast to run anyways.
Ids to delete:
* def idsToDelete =
"""
[
101,
102,
103
]
"""
Test clean up scenario:
# Test data clean-up scenario
Scenario: Delete test data
# Js method to call delete data feature.
* def deleteTestDataFun =
"""
function(x) {
var temp = [x];
// Call to feature. Pass argument as json object.
karate.call('delete-test-data.feature', { id: temp });
}
"""
* karate.forEach(idsToDelete, deleteTestDataFun)
Calls the delete test data scenario and passes it a list of ids that needs to be deleted.
Delete test data feature:
Feature: To delete test data
Background:
* def idVal = id
Scenario: Delete
Given path 'tests', 'delete', idVal
Then method delete
Yeah I personally recommend a strategy to pre-clean-up always, because you cannot guarantee that an "after" hook gets called, e.g. if the machine is switched off.
Sometimes the simplest option is to do this as plain old Java code in your JUnit test-suite. So maybe a one-line after using Runner is sufficient.
It gets tricky if you need to keep track of dynamic data that your tests have created. What I would do is write a Java singleton, use it in your tests to "collect" the ID-s that need to be deleted, and then use this in your JUnit class. You can use things like #AfterClass.
Please try and replicate using the instructions here: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue - because this can indeed be a bug with Scenario Outline.
Finally, you can evaluate ExecutionHook which has an afterSuite() callback: https://github.com/intuit/karate/issues/970#issuecomment-557443551
EDIT: in 1.0 - it has become RuntimeHook: https://github.com/intuit/karate/wiki/1.0-upgrade-guide#hooks
Want to know if Karate supports Neo4j database?. If yes, would like to have an ex. feature which will be helpful.
Karate supports any Java code so that way indirectly you should be able to do anything you want.
Please look at this JDBC example which will get you started: dogs.feature. You will need to write a little bit of Java code (one time only) so if you don't have that skill, please ask someone to help.
# use jdbc to validate
* def config = { username: 'sa', password: '', url: 'jdbc:h2:mem:testdb', driverClassName: 'org.h2.Driver' }
* def DbUtils = Java.type('com.intuit.karate.demo.util.DbUtils')
* def db = new DbUtils(config)
# since the DbUtils returns a Java Map, it becomes normal JSON here !
# which means that you can use the full power of Karate's 'match' syntax
* def dogs = db.readRows('SELECT * FROM DOGS')
* match dogs contains { ID: '#(id)', NAME: 'Scooby' }
I have two feature files and I'm trying to reuse the result of the first feature file as a background to the second feature file's scenario
Feature file 1
Feature: First feature file
Scenario: create random session id
* def sessionId = Java.type('com.company.RandomSessionId').getRandomSessionId()
Feature file 2
Feature: calling another feature file
Background:
* def mycall = callonce read('first.feature')
* def randomId = mycall.sessionId
Scenario: print sessionId
* print randomId
Scenario: print sessionId-2
* print randomId
When I execute the scenarios in Feature file 2, I get two different results.
It must be because you are using the IDE support / right-click / "run-as" option. This is an open issue, because Karate needs to cache across Scenarios which "native" Cucumber does not support: https://github.com/intuit/karate/issues/136 - apologies and I need to update the documentation.
Please use a JUnit runner for these cases, I recommend having these in place for dev-mode anyway, and the new HTML dev-mode report makes this even more useful: https://twitter.com/KarateDSL/status/935029435140489216