defining a variable to set length of an array is failing but assert and print works - karate

def count = * print response.teams[0].teamMembers.length throws below error
com.jayway.jsonpath.PathNotFoundException: Expected to find an object
with property ['length'] in path $['teams'][0]['teamMembers'] but
found 'net.minidev.json.JSONArray'.
This is not a json object
according to the JsonProvider:
'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.
print response.teams[0].teamMembers.length and
assert response.teams[0].teamMembers.length == 9
are working just fine.
Any help here is much appreciated.

Yes, Karate assumes the right-hand-side as Json-Path (which is fine for 90% of the cases). Use parentheses to force JavaScript evaluation when needed.
Try this:
def count = (response.teams[0].teamMembers.length)
For a detailed explanation, please refer to this section in the documentation: Karate Expressions

Related

Use karate.match with contains

I have a scenario where i need to perform a optional match and continue with the execution..
And the value to match is part of an array.
The recommendation was to use karate.match() in one of the stackflow answers
Normal flow -
* match response [*].id contains myid // but execution should not halt here.
How to do this using Karate.match() ?
Consider this not supported by Karate. What you are asking for does not make sense. You either assert for something or you don't. Else you should just print the response in this case, so you can see it in the report.
If you want to conditionally do something, refer this: https://github.com/intuit/karate#conditional-logic
One hint, this will work:
* def ids = $response[*].id
* if (ids.contains(1)) karate.log('yes')
This is covered in more detail in this answer: https://stackoverflow.com/a/54108755/143475

Proper Syntax When Using dot Operator in String Interpolation in Dart

In Dart/Flutter, suppose you have an instance a of Class Y.
Class Y has a property, property1.
You want to print that property using string interpolation like so:
print('the thing I want to see in the console is: $a.property1');
But you can't even finish typing that in without getting an error.
The only way I can get it to work is by doing this:
var temp = a.property1;
print ('the thing I want to see in the console is: $temp');
I haven't found the answer online... and me thinks there must be a way to just do it directly without having to create a variable first.
You need to enclose the property in curly braces:
print('the thing I want to see in the console is: ${a.property}');
That will then print the value of a.property.
It seems you can also do this, but it doesn't seem to be documented anywhere:
print('..... $a.$property1');

Checking if condition on Long type for velocity template

Long type from java is not working with velocity if condition
I am using velocity engine for email with Java where one of the variables type is Long.
While trying if condition on that variable it never succeeds.
Tried following ways but none was helpful,
#if($customTypeList.LongTypeId == 1)
#if($customTypeList.LongTypeId == '1')
#if($customTypeList.LongTypeId == "1")
It should go inside the if condition as variables value is 1.
I have validated that with sysout and even by printing in template.
Actually got the answer after number of trials...
Posting to help others.
#if($customTypeList.longTypeId.intValue() == 1)

Evaluating Variables in Load Script

Is there any reason that this syntax shouldn't work in Qlikview load script??
Let v_myNumber = year(today());
Let v_myString = '2017-08';
If left($(v_myString),4) = text($(v_myNumber)) Then
'do something
Else
'do something else
End If;
I've tried both ways where I convert variable string to number and evaluate against the number variable directly and this way. They won't evaluate to equivalence when they should..
Left function is expecting a string as is getting something else as a parameter. As you are currently doing, the function will be called as Left(2017-08, 4) which is unhandle by QlikView.
If you use Left('$(v_myString)',4), it will evaluate as Left('2017-08', 4) as work as expected. Just adding quotes around the variable it should work.
Although QlikView calls them variables, they should really be seen as "stuff to replaced (at sometimes evaluated) at runtime", which is slightly different from a standard "variable" behaviour.
Dollar sign expansion is a big subject, but in short:
if you are setting a variable - no need for $().
if you are using a variable - you can use $(). depends on its context.
if you are using a variable that needs to be evaluated - you have to use $().
for example in a load script: let var1 = 'if(a=1,1,2)' - here later on the script you will probably want to use this variable as $(var1) so it will be evaluated on the fly...
I hope its a little more clear now. variable can be used in many ways at even can take parameters!
for example:
var2 = $1*$2
and then you can use like this: $(var2(2,3)) which will yield 6
For further exploration of this, I would suggest reading this

Pandas and Fuzzy Match

Currently I have two data frames. I am trying to get a fuzzy match of client names using fuzzywuzzy's process.extractOne function. When I have run the following script on sample data I get good results and no error, but when I run the following on my current data frames I get both an Attribute and Type error. I am not able to provide the data for security reasons, but if anyone can figure out why I am getting errors based on the script provided I would be much obliged.
names2 = list(dftr3['Common Name'])
names3 = dict(zip(names2,names2))
def get_fuzz_match(row):
match = process.extractOne(row['CLIENT_NAME'],choices = n3.keys(),score_cutoff = 80)
if match:
return n3[match[0]]
return np.nan
dfmi4['Match Name'] = dfmi4.apply(get_fuzz_match, axis=1)
I know not having some examples makes this more difficult to troubleshoot, so I will answer any question and edit the post to help this process along. The specific errors are:
1.AttributeError: 'dict_keys' object has no attribute 'items'
2.TypeError: expected string or buffer
The AttributeError is straightforward and to be expected, I think. Fuzzywuzzy's process.extract function, which does most of the actual work in process.extractOne, uses a try:... except: clause to determine whether to process the choices parameter as dict-like or list-like. I think you are seeing the exception because the TypeError is raised during the except: clause.
The TypeError is trickier to pin down, but I suspect it occurs somewhere in the StringProcessor class, used in the processor module, again called by extract, which uses several string methods and doesn't catch exceptions. So it seems likely that your apply call is passing something that is not a string. Is it possible that you have any empty cells?