how to put in a param even if the value of param is null - karate

so my code is as follows:
* def value = newMP.response.data[randomizer].phoneNumber
* def nullvalue = 'null'
* def filter = (value == '#null' ? nullvalue : value)
* print filter
And param filter[phoneNumber] = filter
The result of this code is
Thing is that my application allows a search for null as well. therefore im looking if its possible to put in a filter that is null based on the conditional logic
Additionally if i go like this
And param filter[phoneNumber] = 'null'
the null value is in the GET call

Yes by default Karate ignores null-valued params and this is what most users expect.
You have not been clear as to what you are expecting but I'm guessing you need a param with an empty value.
Try this:
And param filter = ''
Also read this example for more ideas: https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/demo/search/dynamic-params.feature

What i required was to put in the value of the parameter as either null or "null", and the validate in response that the value is null, not the string form of it. below is the workaround for it.
And def value = newMP.response.data[randomizer].phoneNumber
And eval if (value == null) karate.set('value', 'null')
And param filter[phoneNumber] = value
When method GET
Then status 200
* eval if (value == 'null') karate.set('value', null)
Then match response.data[0].attributes.phoneNumber contains value

Related

How to add a dynamic variable value inside a param field as a String in Karate?

I have an API, wherein the Param field, I need to pass the current date as a string.
And param filter = 'ORDER_DATE:"2021-01-31"'
I am trying to pass the current date for the ORDER_DATE field form a java method:
* def todaysDate = helper.getTodaysDate()
And print todaysDate // Prints 2021-02-04
Now I need to pass this "todaysDate " valuein the param filter field.
Following what I have tried so far:
And param filter = 'ORDER_DATE:#(todaysDate )'
And param filter = 'ORDER_DATE:<todaysDate>'
From example table value.
In Both cases, it printed "todaysDate " instead of its value "2021-02-04"
It is just JavaScript:
And param filter = 'ORDER_DATE:"' + todaysDate + '"'
This can improve in the 1.0 version BTW:
And param filter = `ORDER_DATE:"${todaysDate}"`
Further reading: https://github.com/intuit/karate#rules-for-embedded-expressions

values are passing when datatype is different in karate

When we are providing the input whose datatype is boolean passed in POST API and validating it with GET API which return value in string
eg:
* def a = 'false' // result from GET API
* def b = false //input
* match a == b
Expected Result : It should fail as the datatype is different
Actual Result ": scenario is showing PASS
Why is it passing?
whereas, I also noticed when I an validating the data from database whose column datatype is string and we are matching the data with boolean value
i.e
* match 'false' == false
Expected Result : It should fail
Actual Result : scenario is failed
I believe the issue is related to this https://github.com/intuit/karate/issues/1179
seems like it has been fixed recently. you can build the code following this https://github.com/intuit/karate/wiki/Developer-Guide#build, else it will be next release.
You can use this option to check for if else inside a feature
Scenario: if else
* def val = 'test'
# * def result = <condition> ? <true value> : <false value>
* def result = val == 'test' ? 'TRUE' : 'FALSE'
* print result

Check if a list contains at least one variable non null

I'm making my first app in Kotlin and there is a lot of syntax I don't know, and I was wondering if there is a better way to check if a list contains at least one non null entry.
For now my solution is:
var atLeastOneValue: Boolean
var i = 0
for (x in list) {
if (x != null) atLeastOneValue = true
else i++
}
if (list.size == i) atLeastOneValue = false
return atLeastOneValue
I'm working with MutableList<String>.
You can use contains function for that:
val hasNull = list.contains(null)
contains can also be called in the operator form, it corresponds to the operator in:
val hasNull = null in list
val hasNoNull = null !in list

Condition based on response value in karate

I want to do a Query API which returns me a 200 always, then check if the result has an array of elements and if it does not have then create the entity using POST API, if found, validate the entity details.
This is my code:
Given url url
And path 'offering'
When method GET
* def expected1 = response != null ? { expected: 'in if' } : { expected: 'in else' }
But this checks only for null value in response and does not check for null array in response
This can be achieved by response.length:
Sample code:
eval if (response.length<=1) karate.call('offeringcreate.feature')

warning variable unused in assert

I can test the content after a redirection like this
assert "/url" = redir_path = redirected_to(conn, 302)
conn = get(recycle(conn), redir_path)
assert html_response(conn, 200) == "Content after redirection"
But I cannot add a param variable to the assert url like this:
id = 10
assert "/url/#{id}" = redir_path = redirected_to(conn, 302)
It complains with:
cannot invoke remote function id/0 inside match
So if I just define the path like this before using it:
url_path = "/url/%{id}"
assert url_path = redir_path = redirected_to(conn, 302)
warning: variable "url_path" is unused
?? unusued? is it being used inside the assert...
Not sure how to silence that warning or how to approach this.
The code is similar to Chris's response here:
https://elixirforum.com/t/following-redirection-in-controllertests/10084
warning: variable "url_path" is unused
?? unusued? is it being used inside the assert...
No, it's not. Your assert will match any value because if you have a variable name on the LHS of a match, it matches any value and that variable gets assigned the value of the RHS.
In:
foo = 10
assert foo = 20
IO.inspect foo
the assert passes and foo is printed as 20.
If you just want to assert that that string equals the value returned by redirected_to/2, you can use ==:
assert "/url/#{id}" == redirected_to(conn, 302)