grails internationalization (i18n) - variables

i work on grails project
def result = "customer"
//(this value is according to returned method parameter,
//it may be customer, company,... & so on)
def messages = "${message(code: 'default.result.${result}', default:'${result}')}"
i need to send a variable inside message code as i mention above
problem: this code appears as
default.result.${result}
that there is no code in message.properties refer to these code
there is default.result.customer ....$ so on
Question: how can i send variable inside message Code?

Try omitting the double quotes (GString) and it should work like the following:
def xxx = "bar"
def m = message(code: "foo.${xxx}", args: ['hello world'])
Results in following message-code
foo.bar

Try:
def messages = message(code: 'default.result.' + result, default: result)
If you want to pass in some values, e.g. a string, you can define your message like this:
default.result.success = Action {0} was successfull.
And resolve your code like this:
def m = message(code: 'default.result.' + result, args: ['delete User'])

Related

calling A.feature from B.feature is returning com.intuit.karate.ScriptObjectMap#xxxxxx

I apologize if this has been answered in a previous thread, I tried searching and was not able to find any that were this specific problem.
I am trying to call A.feature from B.Feature - where A returns a specific value to be used in B
A.feature:
Given url endpoint
And header Content-Type = 'application/json,text/plain'
And request
"""
{
"prod": "xxxx"
}
"""
When method post
Then status 200
Then match response[0].acc.insType == 'xxx'
And def accKey = response[0].account.accKey
then in B.feature, i am calling it with the * call read:
* def key = call read('ReadRoundUpSubscription.feature');
* print 'Account Key is ' + key
But it is returning
[print] Account Key is com.intuit.karate.ScriptObjectMap#xxxxxx
UPDATE:
I changed this line:
And def accKey = response[0].account.accKey
to be
And set accKey = response[0].account.accKey
and now i am getting variable is null or not set 'accountToken'
Update 2:
I figured it out thanks to this post
A.feature ends with
* def accKey = response[0].account.accKey
Then in B.feature:
* def key = karate.call('ReadRoundUpSubscription.feature');
* def keyvalue = key.acckey
I realize my variable names are bad. real bad.
If there is a better way to accomplish this result please let me know!
I think you finally got it: * def keyvalue = key.acckey - so the result will contain an envelope of all variables in the "called" feature.
This is explained in detail in the documentation: https://github.com/karatelabs/karate#calling-other-feature-files

Karate: How can we retrieve the value from called feature file

I have two parameter in feature file A and I pass those values to another feature file called B.
But I am unable to retrieve the values as expected in Feature file B
CODE:
Feature File A:
And def Response = response
And def token = response.metaData.paging.token
And def totalPages = response.metaData.paging.totalPages
* def xyz =
"""
function(times){
for(currentPage=1;currentPage<=times;currentPage++){
karate.log('Run test round: '+(currentPage));
karate.call('ABC.feature', {getToken:token, page:currentPage});
}
java.lang.Thread.sleep(1*1000);
}
"""
* call xyz totalPages
Feature File B:
* def token = '#(getToken)'
* def currentPage = '#(page)'
But the output was
#getToken
#page
What would be the best way? to these values for further utilization.
Try this:
* def token = getToken
* def currentPage = page
Here's another thing, any variable defined in the calling feature will be visible, e.g. token so most of the time you don't need to pass arguments:
* print token
* print totalPages
Please avoid JS for loops as far as possible: https://github.com/intuit/karate#loops - and actually you seem to have missed the data-driven testing approach that Karate recommends: https://github.com/intuit/karate#the-karate-way
If you are still stuck, please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Karate framework variable usage

I have this steps:
...
Then status 200
And match response.requests[0].request.url == "/endpoint"
And json body = response.requests[0].request.body
And match body == { "something": "something"}
To simplify, I tried to put response.requests[0].request in a variable called request:
...
Then status 200
And def request = response.requests[0].request
And match request.url == "/endpoint"
And json body = request.body
And match body == { "something": "something"}
I'm having the following error:
'request' is not a variable, use the form '* request <expression>' instead
I read the documentation and the use of request seems to be fine:
Given def color = 'red '
And def num = 5
Then assert color + num == 'red 5'
What am I doing wrong?
Thanks in advance.
Just make this change:
* def req = response.requests[0].request
# other steps
* request req
We simply disallow def request (using request as a variable name) because a lot of newbie users get confused. The error message has worked 99.9% of the time for users to understand what the problem is, but I guess you fall in the 0.1% :)

How to pass single param to separate feature file

I've tried below to send a param to separate feature file ( by following this example ) but, it is not working as expected.
project.feature
* def id = '55000000005021'
* def result = call read('delete_project.feature')
delete_project.feature
Given path 'project', '#(id)'
When method DELETE
Then status 200
Then match response.status == 'success'
Getting below exception
com.intuit.karate.exception.KarateException: projects.feature:48 -
delete_project.feature:11 - status code was: 404, expected: 200,
response time: 239, url: https://localhost:8080/project/%23(id)
response:
{"status":"failure","data":{"error_code":"INVALID_URL_PATTERN","message":"Please
check if the URL trying to access is a correct one"}} at ✽.* def
result = call read('delete_project.feature') (projects.feature:48)
One more doubt, How to iterate this by passing list of ids. I've multiple id in foo variable, I would like call delete_project.feature for each id availble in that foo variable.
* def foo = get response.data[*].id
I think you over-complicated the path params, change to:
Given path 'project', id
And read this part of the docs: https://github.com/intuit/karate#rules-for-embedded-expressions

How to call a Javascript function with arguments from .js file in karate feature file

Lets say I created javascript functions in functions js file.
function getReviews(reviews){
var length_reviews = reviews.length
return length_reviews
}
function getReviewsLength(reviewLength){
return reviewLength
}
Here in function getReviews argument reviews is an array.
Now how will I call getReviews function in one feature file.
When I tried below code
* def jsFunction = call read('functions.js')
* def review = jsFunction.getReviews(reviewFromFeatureFile)
I am getting an error of
Cannot read property "length" from undefined
I already printed reviewFromFeatureFile and its coming correctly in print statement.
As Peter mentioned above you can keep you js inline on your feature
* def reviews = [{"r1":2},{"r1":3},{"r1":4}]
* def getReviews = function(reviews){ return reviews.length }
* def getReviewsLength = getReviews(reviews)
* print getReviewsLength
In this example, it should print 3.
For more other options for handling javascript or other reusable modules in karate, please refer to this article
Organizing re-usable functions in karate
In one "common" feature file, define multiple methods like this:
* def uuid = function(){ return java.util.UUID.randomUUID() + '' }
* def now = function(){ return java.lang.System.currentTimeMillis() }
You can now call this feature like this:
* call read('common.feature')
And now all the functions in that feature are available for use:
* def id = uuid()
* def time = now()