karate.match is throwing a TypeError when validating against a string - karate

I am doing some UI automation using Karate. In this particular scenario, the goal is to navigate to a page that has a table with pagination. If there is a "next page" button, to click it, and to verify the pagination changes as expected (pagination will always return either an 8 or a 9).
I have the following code:
* def getPagination =
"""
function(){
var nameVal = []
var nextPageBtn = false
do {
nextPageBtn = driver.exists('skipToNext a');
if (nextPageBtn {
driver.click('skipToNext a');
var getPagination = (driver.text(li[class='active'] a)).trim()
karate.log("The current page value is:", getPagination)
karate.match (getPagination == '8' || '9')
var getNameVal = driver.scriptAll('#names', '_.textContent')
nameVal.push(getNameVal)
}
} while (nextPageBtn)
return(nameVal);
}
I am getting the following error on the match:
org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (match) on com.intuit.karate.core.ScenarioBridge#5d32d8f failed due to: no applicable overload found (overloads: [Method[public java.lang.Object com.intuit.karate.core.ScenarioBridge.match(java.lang.String)], Method[public java.lang.Object com.intuit.karate.core.ScenarioBridge.match(java.lang.Object,java.lang.Object)]], arguments: [true (Boolean)])
- <js>.:anonymous(Unnamed:17)
Any ideas?

Related

Karate : dynamic test data using scenario outline is not working in some cases

I was tryiny to solve dynamic test data problem using dynamic scenario outline as mentioned in the documentation https://github.com/karatelabs/karate#dynamic-scenario-outline
It worked perfectly fine when I passed something like this in Example section
Examples:
|[{'entity':country},{'entity':state},{'entity':district},{'entity':corporation}]]
But I tried to generate this json object programatically , I am getting aa strange error
WARN com.intuit.karate - ignoring dynamic expression, did not evaluate to list: users - [type: MAP, value: com.intuit.karate.ScriptObjectMap#2b8bb184]
Code to generate json object
* def user =
"""
function(response){
entity_type_ids =[]
var entityTypes = response.entityTypes
for(var i =0;i<entityTypes.length;i++ ){
object = {}
object['entity'] = entityTypes[i].id
entity_type_ids.push(object)
}
return JSON.stringify(entity_type_ids)
}
"""

Karate SAXParseException when logPrettyResponse is true and response content type is 'text/turtle'

It looks like a bug but asking the question just in case I have missed something.
Karate version is 1.1.0
The post request is as below. Note that content type is text/turtle and I use logPrettyResponse because further requests in test are testing RDF/XML and other serialisations.
* configure logPrettyResponse = true
Given path '/graph'
* text payload =
"""
<http://example.com/a7460f22-561a-4dde-922b-665bd9cf3bd9> <http://schema.org/description> "Test"#en.
"""
And request payload
And header Accept = 'text/turtle'
And header Content-Type = 'text/turtle'
When method POST
Then status 200
And I get below error
ERROR com.intuit.karate - org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 8; Element type "http:" must be followed by either attribute specifications, ">" or "/>"., http call failed
Reason this seems to be happening is that fromString method in JsValue.java is trying to parse turtle as XML because condition case '<' is true when response is turtle.
public static Object fromString(String raw, boolean jsonStrict, ResourceType resourceType) {
String trimmed = raw.trim();
if (trimmed.isEmpty()) {
return raw;
}
if (resourceType != null && resourceType.isBinary()) {
return raw;
}
switch (trimmed.charAt(0)) {
case '{':
case '[':
return jsonStrict ? JsonUtils.fromJsonStrict(raw) : JsonUtils.fromJson(raw);
case '<':
if (resourceType == null || resourceType.isXml()) {
return XmlUtils.toXmlDoc(raw);
} else {
return raw;
}
default:
return raw;
}
}
To solve the problem I have set logPrettyResponse to false. I would love to know if anyone has any other thoughts or if someone can confirm that it is a bug?

karate.match with contains is not working

I'm trying to do this example (https://github.com/intuit/karate/blob/master/karate-junit4/src/test/java/com/intuit/karate/junit4/demos/js-arrays.feature line 556):
* def foo = { a: 1, b: 'foo' }
* def res = karate.match("foo contains { a: '#number' }")
* match res == { pass: true, message: null }
But I'm getting this error:
javascript evaluation failed: karate.match("foo contains { a: '#number' }"), TypeError: Can not invoke method [jdk.internal.dynalink.beans.SimpleDynamicMethod Map com.intuit.karate.core.ScriptBridge.match(Object,Object)] with the passed arguments; they do not match any of its method signatures. in <eval> at line number 1
Do you know what can I do to fix this?
Old version of Karate. Please upgrade.

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

method as_array() doesn't work

I'm using idiorm as an ORM library and the method as_array() doesn't work this is my code I get 2 errors
1- $market = $market->as_array();
PHP Fatal error: Call to a member function as_array() on a non-object
2- ->as_array();
PHP Fatal error: Call to a member function as_array() on a non-object
My code:
function ($market = 'affiliate', $category = array('all')) use ($app) {
$market = \ORM::for_table('category')
->where('alias', $market)
->find_one();
$market = $market->as_array();
$category = #end($category);
if ($category != 'all') {
$category = \ORM::for_table('category')
->where('alias', $category)
->where_gt('category_id', 0)
->find_one()
->as_array();
$items = \ORM::for_table('item')
->select('item.*')
->select('category.name', 'category_name')
->join('category', 'category.id = item.category_id')
->where('item.category_id', $category['id']);
According to the Idiorm docs:
Any method chain that ends in find_one() will return either a single instance of the ORM class representing the database row you requested, or false if no matching record was found.
You should check to see if your query has returned any rows before calling as_array().
For example:
$market = \ORM::for_table('category')
->where('alias', $market)
->find_one();
if($market != false)
{
$market = $market->as_array();
}
If this still gives an error then you may not have initialised Idiorm properly.