karate - Missing Data from method when using nested Embedded Expressions [duplicate] - karate

so basically I am just starting with karate testing framework and and probably missing something really simple, but I cant seem to get an embedded expression to be resolved properly. If I have a feature file like so that does the same thing a couple ways:
Feature: Test Service
Background:
* url 'http://testurl:8080'
* def localDateTime = Java.type('java.time.LocalDateTime')
Scenario: Successful request
* def createDateTime = LocalDateTime.now()
* def testRequest =
"""
{
createDateTime: "#(createDateTime)",
expiryDateTime:"#(localDateTime.now().plusMinutes(5))"
}
"""
* print testRequest
* set testRequest.createDateTime = createdTime
* print testRequest
Then when it gets to the print lines, I get output like this where the values are empty js objects {}
16:43:28.580 [main] INFO com.intuit.karate - [print] {"createDateTime":{},"expiryDateTime":{}}16:43:28.586 [main] DEBUG com.jayway.jsonpath.internal.PathCompiler - Using cached path: $true[]
Also, I can see where it appears to be setting the path for the first print statement like so:
16:32:30.612 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['createDateTime']
16:32:30.613 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['createDateTime'] new value 2017-09-14T16:32:30.566
16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['expiryDateTime']
16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['expiryDateTime'] new value 2017-09-14T16:37:30.621
Can anybody please explain to me why I am unable to get the actual dates inserted into the testRequest?
Thank you.

I think your problem will be solved if you convert those date objects to strings.
* def LocalDateTime = Java.type('java.time.LocalDateTime')
* def createDate = LocalDateTime.now() + ''
* def expiryDate = LocalDateTime.now().plusMinutes(5) + ''
* def testRequest = { createDateTime: '#(createDate)', expiryDateTime: '#(expiryDate)' }
* print karate.pretty(testRequest)
The above is working for me and this is the output:
06:11:47.010 [main] INFO com.intuit.karate - [print] {
"createDateTime": "2017-09-15T06:11:46.983",
"expiryDateTime": "2017-09-15T06:16:46.990"
}

Related

# is converting passed function to a string and not a javascript expression in Karate [duplicate]

so basically I am just starting with karate testing framework and and probably missing something really simple, but I cant seem to get an embedded expression to be resolved properly. If I have a feature file like so that does the same thing a couple ways:
Feature: Test Service
Background:
* url 'http://testurl:8080'
* def localDateTime = Java.type('java.time.LocalDateTime')
Scenario: Successful request
* def createDateTime = LocalDateTime.now()
* def testRequest =
"""
{
createDateTime: "#(createDateTime)",
expiryDateTime:"#(localDateTime.now().plusMinutes(5))"
}
"""
* print testRequest
* set testRequest.createDateTime = createdTime
* print testRequest
Then when it gets to the print lines, I get output like this where the values are empty js objects {}
16:43:28.580 [main] INFO com.intuit.karate - [print] {"createDateTime":{},"expiryDateTime":{}}16:43:28.586 [main] DEBUG com.jayway.jsonpath.internal.PathCompiler - Using cached path: $true[]
Also, I can see where it appears to be setting the path for the first print statement like so:
16:32:30.612 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['createDateTime']
16:32:30.613 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['createDateTime'] new value 2017-09-14T16:32:30.566
16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['expiryDateTime']
16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['expiryDateTime'] new value 2017-09-14T16:37:30.621
Can anybody please explain to me why I am unable to get the actual dates inserted into the testRequest?
Thank you.
I think your problem will be solved if you convert those date objects to strings.
* def LocalDateTime = Java.type('java.time.LocalDateTime')
* def createDate = LocalDateTime.now() + ''
* def expiryDate = LocalDateTime.now().plusMinutes(5) + ''
* def testRequest = { createDateTime: '#(createDate)', expiryDateTime: '#(expiryDate)' }
* print karate.pretty(testRequest)
The above is working for me and this is the output:
06:11:47.010 [main] INFO com.intuit.karate - [print] {
"createDateTime": "2017-09-15T06:11:46.983",
"expiryDateTime": "2017-09-15T06:16:46.990"
}

Any example to have a java class instance to call once and use in all scenarios in karate with different feature files

I have been using DBUtils class from karate demo, i knew this class was nothing to deal with karate. I have a concern like an example which was given has DBUtlis class is called in background for each and every scenario and it should be mentioned in all featurefiles Background:.
Anything like we configure once and use that DB instance variable in all scenarios?? If yes examples please.
Update after below comment by peter:
config:
Main Feature File:
Reusing DB instance in another feature file
Please confirm whether this is right approach or not?
Dry Run for a String:
var result = karate.callSingle('classpath:featureFiles/dbBackground.feature', config);
config.PersonName = result.name;
Main Feature:
Feature: DB Background
Background:
* def name = "Sandeep";
Other feature:
Feature: Get Account Details
Background:
* def actualname = PersonName;
#golden
Scenario: user 1 details
* def expectedFormat = read('../requestFiles/format.json')
Given url 'https://reqres.in/api/users'
And params ({id: '1'})
When method Get
Then match response.data.email == "george.bluth#reqres.in"
Then print '###################################name is: ', actualname
Then print '###################################name is: ', PersonName
Console result seeing null:
Updated Dry run 2:
Feature: DB Background
Background:
* def name = "Sandeep";
#golden
Scenario: user sample details
* def expectedFormat = read('../requestFiles/format.json')
Given url 'https://reqres.in/api/users'
And params ({id: '1'})
When method Get
Then match response.data.email == "george.bluth#reqres.in"
output:
19:31:33.416 [ForkJoinPool-1-worker-0] DEBUG com.jayway.jsonpath.internal.path.CompiledPath - Evaluating path: $['data']['email']
19:31:33.416 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is: Sandeep
19:31:33.432 [ForkJoinPool-1-worker-0] INFO com.intuit.karate - [print] ###################################name is: Sandeep
Yes you can initialize it in karate-config.js and then it will be a global variable.
Also look at karate.callSingle(): https://github.com/intuit/karate#hooks

Cannot convert to map when re-use one feature on other directory

I have read: https://stackoverflow.com/search?q=%5Bkarate%5Dcannot+convert+to+map and https://github.com/intuit/karate/issues/544
I am using karate-0.8.0
I have one feature which will be re-used on A directory, the content like:
#ignore
Feature:
Background:
* url baseUrl
* def Sign = Java.type('cruiser.token.Sign')
* configure afterScenario =
"""
function() {
if (karate.info.errorMessage != null) {
karate.log(karate.info.errorMessage);
}
}
"""
Scenario:
* def ck = Sign.execute('#(uid)')
* path '/rest/n/rt/upload'
* cookies ck
* multipart fields '#(fo)'
* multipart file rt = { read: 'classpath:cruiser/http/rt/A/123.mp3', filename: '123.mp3', contentType: 'audio/mp3' }
* method post
* status 200
* match response contains { result: 1 }
And have other one feature file on B directory, content like this:
Feature:
Background:
Scenario:
* def fo =
"""
{
'title': '你好!',
'description': '很好听哦'
}
"""
* def x = call read('classpath:cruiser/http/rt/A/upload-base.feature') { uid: 33, fo: '#(fo)' }
* match x.response contains { result: 1 }
* print x.response.feed.id
its runner name is XRunner.java
when mvn test -Dtest=XRunner, the error info:
Running cruiser.http.rt.B.XRunner
11:25:33.138 [main] INFO com.intuit.karate.junit4.Karate - Karate version: 0.8.0
11:25:33.896 [main] ERROR com.intuit.karate - feature call failed: classpath:cruiser/http/rt/A/upload-base.feature
arg: {uid=33, fo={title=你好!, description=很好听哦}}
cannot convert to map: '#(fo)'
Failed scenarios:
cruiser/http/rt/B/x.feature:3 # Scenario:
Both these lines are wrong:
* def ck = Sign.execute('#(uid)')
* multipart fields '#(fo)'
Read this: https://github.com/intuit/karate#rules-for-embedded-expressions
In Karate, expressions are pure JS by default. So just do this:
* def ck = Sign.execute(uid)
* multipart fields fo

Getting junk value(com.intuit.karate.ScriptObjectMap#XXXX ) when i call karate feature( using Karate.call) in javascript

I am trying to call a karate feature in javascript and capture its response as below, but while doing, the response from karate.call is showing junk value(com.intuit.karate.ScriptObjectMap#XXXX ) . kindly help to get the actual values from karate.call or suggest me any best idea ?
function RequestMandator(featurepath,data) {
var Mandator = [];
data.forEach(function(data){
var TransferId = data.TransferID;
var FocusKey = data.TransferID + ':';
var TimeStamp = data.LastUpdate;
var result = karate.call(featurepath, { input: [TransferId, FocusKey,TimeStamp ] });
karate.log('Added Mandator :', result);
Mandator.push(result);
})
return Mandator;
}
Output:
11:32:53.307 [main] WARN com.intuit.karate - xml parsing failed, response data type set to string: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 15; Open quote is expected for attribute "border" associated with an element type "table".
11:32:53.310 [main] INFO com.intuit.karate - Added Mandator : com.intuit.karate.ScriptObjectMap#102d92c4
Error:
com.intuit.karate.exception.KarateFileNotFoundException: C:\XXXXXXXX\com.intuit.karate.ScriptObjectMap#7808fb9,com.intuit.karate.ScriptObjectMap#25d958c6,com.intuit.karate.ScriptObjectMap#5eeedb60,com.intuit.karate.ScriptObjectMap#6ad6fa53,com.intuit.karate.ScriptObjectMap#6f099cef,com.intuit.karate.ScriptObjectMap#2d66530f,com.intuit.karate.ScriptObjectMap#25b865b5 (The filename, directory name, or volume label syntax is incorrect)
at com.intuit.karate.FileUtils.getFileStream(FileUtils.java:146)
at com.intuit.karate.FileUtils.readFile(FileUtils.java:110)
at com.intuit.karate.ScriptBridge.read(ScriptBridge.java:67)
Please refer to the documentation on type-conversion: https://github.com/intuit/karate#type-conversion
It is not possible to figure out based on the incomplete information you have provided. Still let me try, I think you have done some mistake in string concatenation before calling this function. And the value of featurepath is completely wrong.
In the example below, see how a string concatenation within a JS function leads to what you call "junk value":
* def fun = function(){ var temp = { hello: 'world' }; return temp + '' }
* def bar = fun()
* print "bar:", bar
Results in output:
13:52:50.912 [main] INFO com.intuit.karate - [print] bar: [object Object]
If you still are stuck, the only suggestion I have is follow the instructions here please: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Embedded expressions with karate DSL don't replace value in json

so basically I am just starting with karate testing framework and and probably missing something really simple, but I cant seem to get an embedded expression to be resolved properly. If I have a feature file like so that does the same thing a couple ways:
Feature: Test Service
Background:
* url 'http://testurl:8080'
* def localDateTime = Java.type('java.time.LocalDateTime')
Scenario: Successful request
* def createDateTime = LocalDateTime.now()
* def testRequest =
"""
{
createDateTime: "#(createDateTime)",
expiryDateTime:"#(localDateTime.now().plusMinutes(5))"
}
"""
* print testRequest
* set testRequest.createDateTime = createdTime
* print testRequest
Then when it gets to the print lines, I get output like this where the values are empty js objects {}
16:43:28.580 [main] INFO com.intuit.karate - [print] {"createDateTime":{},"expiryDateTime":{}}16:43:28.586 [main] DEBUG com.jayway.jsonpath.internal.PathCompiler - Using cached path: $true[]
Also, I can see where it appears to be setting the path for the first print statement like so:
16:32:30.612 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['createDateTime']
16:32:30.613 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['createDateTime'] new value 2017-09-14T16:32:30.566
16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.CompiledPath - Evaluating path: $['expiryDateTime']
16:32:30.629 [main] DEBUG com.jayway.jsonpath.internal.JsonReader - Set path $['expiryDateTime'] new value 2017-09-14T16:37:30.621
Can anybody please explain to me why I am unable to get the actual dates inserted into the testRequest?
Thank you.
I think your problem will be solved if you convert those date objects to strings.
* def LocalDateTime = Java.type('java.time.LocalDateTime')
* def createDate = LocalDateTime.now() + ''
* def expiryDate = LocalDateTime.now().plusMinutes(5) + ''
* def testRequest = { createDateTime: '#(createDate)', expiryDateTime: '#(expiryDate)' }
* print karate.pretty(testRequest)
The above is working for me and this is the output:
06:11:47.010 [main] INFO com.intuit.karate - [print] {
"createDateTime": "2017-09-15T06:11:46.983",
"expiryDateTime": "2017-09-15T06:16:46.990"
}