Karate - How to extract from Json Response and use as data driven data in Outline Scenario - karate

Using the below response, I want to extract the ID fields where created_by.$oid is equal to '5bf6d22a60664323c10676cf' and use the extracted Ids as data driven data for Outline Scenarios. Which is the most effective way to do so?
[
{
"deleted": false,
"processing": false,
"id": "5ffff5b36bdfca2cb8f11135",
"created_by": {
"$oid": "3bf6d24a60664343c10676cf"
}
},
{
"deleted": false,
"processing": false,
"id": "600070616bdfca4f2045824f",
"created_by": {
"$oid": "5bf6d22a60664323c10676cf"
}
},
{
"deleted": false,
"processing": false,
"id": "6001907f38d61400080376f4",
"created_by": {
"$oid": "5bf6d22a60664323c10676cf"
}
}
]

Here you go, and please refer to the docs for how this works: https://github.com/intuit/karate#jsonpath-filters
* def fun = function(x){ return x.created_by['$oid'] == '5bf6d22a60664323c10676cf' }
* def filtered = karate.filter(response, fun)
* def ids = $filtered[*].id
* print ids

Related

Karate For loop to get ids based on pattern and then use a delete feature

I have response from an API call that gives me a list of devices each with an id. Some of these devices are test devices with the id starting with the prefix 'Test' Example Test319244.
I wish to only retrieve those ids with the prefix 'Test', may be in an array and be able to pass them to another feature file which takes the device ID as the parameter to delete it. Basically I want to delete all the testdevices.
Here is the sample response that contains all the device IDs
{
"items": [
{
"deviceId": "004401784033074000",
"deviceType": "AVMAP_TMR",
"disabled": false,
"metadata": {
"createdAt": "2020-07-20T00:00:00.000+00:00",
"modifiedAt": "2020-07-20T00:00:00.000+00:00"
}
},
{
"deviceId": "Test319246",
"deviceType": "AVMAP_TMR",
"disabled": false,
"metadata": {
"createdAt": "2020-07-21T00:00:00.000+00:00",
"modifiedAt": "2020-07-21T00:00:00.000+00:00"
}
},
{
"deviceId": "Test319245",
"deviceType": "AVMAP_TMR",
"disabled": false,
"metadata": {
"createdAt": "2020-07-21T00:00:00.000+00:00",
"modifiedAt": "2020-07-21T00:00:00.000+00:00"
}
},
{
"deviceId": "Test319244",
"deviceType": "AVMAP_TMR",
"disabled": false,
"metadata": {
"createdAt": "2020-07-21T00:00:00.000+00:00",
"modifiedAt": "2020-07-21T00:00:00.000+00:00"
}
},
{
"deviceId": "command-service",
"deviceType": "service",
"disabled": false,
"metadata": {
"createdAt": "2020-07-20T00:00:00.000+00:00",
"modifiedAt": "2020-07-20T00:00:00.000+00:00"
}
},
{
"deviceId": "kafka-connect-all",
"deviceType": "kafka-connect",
"disabled": false,
"metadata": {
"createdAt": "2020-07-20T00:00:00.000+00:00",
"modifiedAt": "2020-07-20T00:00:00.000+00:00"
}
}
],
"metadata": {
"pagination": {
"limit": 50,
"offset": 0,
"previousOffset": 0,
"nextOffset": 0,
"totalCount": 15
},
"sortedBy": [
{
"field": "deviceId",
"order": "ASC"
}
]
}
}
Here in the above example I only want to delete the devices with ids - Test319244,Test319245 and Test319246
How can I get an array of ids based on the pattern(Testxxxxxx) and pass that on to another feature file
I need help to define an array of ids like:
* def ids = extract the ids based on the pattern
# pass the ids to the delete feature which would send the id one at a time and delete the device.
* def delete = call(delete.feature) ids
This is how the delete scenario feature file looks:
Scenario: Delete Device
# device_registry_url defined in karate-config.js
Given url device_registry_url
And path '/device/'+DeviceID
And header Authorization = authheader
And request ''
When method delete
Then status 200
Would this be the right approach or could we do it in a better way? If so, can someone kindly help in how to do it please?
Just use karate.filter() and then you know what to do:
* def fun = function(x){ return x.deviceId.startsWith('Test') }
* def filtered = karate.filter(response.items, fun)
* call read('delete.feature') filtered

Karate: Is it possible to compare two service responses & exclude some keys in comparison

I am trying to compare responses from two service call(for Migration project) using Karate.
Is it possible to exclude or ignore some keys while comparing whole response at one go for following cases:
1)There are few elements in response where the values is different(marked in bold).
2)There are few elements which is not present in one response but present in other(marked in bold).
Service 1 response -
{
"userKey": "string",
"enabled": true,
"locked": true,
"profile": {},
"roles": [
{
"roleKey": 3,
"name": "Role",
**"links": []**
}
],
"links": [
{
"rel": "self",
**"href": "https://starhub1.com"**
},
{
"rel": "self",
**"href": "https://singtel1.com"**
}
]
}
Service 2 response -
{
"userKey": "string",
"enabled": true,
"locked": true,
"profile": {},
"roles": [
{
"roleKey": 3,
"name": "Role"
}
],
"links": [
{
"rel": "self",
**"href": "https://starhub22.com"**
},
{
"rel": "self",
**"href": "https://singtel22.com"**
}`enter code here`
]
}
Yes Karate is pretty good at this. Just do a transform on one payload before comparison.
Please see this answer for details: https://stackoverflow.com/a/53120851/143475
For your specifc example maybe all you need to do is change one "side" to #string and then do the comparison.
* def response1 = { foo: 'hello' }
* def response2 = { foo: 'world' }
* response2.foo = '#string'
* match response1 == response2

How to traverse thru a response when it is as below

{
"createDate": "2019-05-15 10:07:44",
"mak": "pokijoklm",
"optStatus": "yujuim",
"partnerAccount": {
"operatorName": "frftcrtii",
"partnerCustomerId": "XXXXX",
"type": "partnerAccount"
},
"transactionId": "hjknhijn1",
"type": "nihnui",
"updateDate": "2019-05-15 11:20:59"
}
For an array we can try as response.partneraccount[*]. but my response is { }
You use [] only when there are arrays. Just observe your JSON structure carefully and you will get it. Try pasting the below into a fresh Scenario and see it work:
* def response =
"""
{
"createDate": "2019-05-15 10:07:44",
"mak": "pokijoklm",
"optStatus": "yujuim",
"partnerAccount": {
"operatorName": "frftcrtii",
"partnerCustomerId": "XXXXX",
"type": "partnerAccount"
},
"transactionId": "hjknhijn1",
"type": "nihnui",
"updateDate": "2019-05-15 11:20:59"
}
"""
* def partnerAccount = response.partnerAccount
* match partnerAccount == { operatorName: 'frftcrtii', partnerCustomerId: 'XXXXX', type: 'partnerAccount' }
* def custId = response.partnerAccount.partnerCustomerId
* match custId == 'XXXXX'

Extract data from json array in Karate

In the below JSON response, I need to extract the 'cid' for the record that has the 'nationalityDecription' as 'USA'. By using this query as a reference, I used the below loc in the karate feature file, but 1st line itself fails with syntax error(tried different combinations). For now, I'm using the custom javascript as a workaround which is working fine. I need help to check if i'm missing anything in syntax. Thanks
Response:
{
"header": {
"Id": "12345678",
"timeStamp": "2018-09-17T10:09:812.000"
},
"dataRecords": [
{
"cid": "31H678",
"cidMeta": "00",
"nationalityDecription": "CHINA"
},
{
"cid": "31S421",
"cidMeta": "01",
"nationalityDecription": "USA"
}
]
}
Feature file:
* def record= $response.dataRecords[?(#.nationalityDecription=='USA')]
* def cid = record.cid
* def response = { "header": { "Id": "12345678", "timeStamp": "2018-09-17T10:09:812.000" }, "dataRecords": [ { "cid": "31H678", "cidMeta": "00", "nationalityDecription": "CHINA" }, { "cid": "31S421", "cidMeta": "01", "nationalityDecription": "USA" } ] }
* def cid = get[0] response.dataRecords[?(#.nationalityDecription=='USA')].cid
* match cid == '31S421'

Sort icon not changing in Datatable server side processing

When I use server side processing in datatable the sorting works but the sort icon does not change and stays in same direction. Below is the code snippet of my datatable configuration.
$('#dtSearchResult').DataTable({
"filter": false,
"pagingType": "simple_numbers",
"orderClasses": false,
"order": [[0, "asc"]],
"info": true,
"scrollY": "450px",
"scrollCollapse": true,
"bLengthChange": false,
"searching": true,
"bStateSave": false,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": VMCreateExtraction.AppSecurity.websiteNode() + "/api/Collection/SearchCustIndividual",
"fnServerData": function (sSource, aoData, fnCallback) {
aoData.push({ "name": "ccUid", "value": ccUid });
//Below i am getting the echo that i will be sending to Server side
var echo = null;
for (var i = 0; i < aoData.length; i++) {
switch (aoData[i].name) {
case 'sEcho':
echo = aoData[i].value;
break;
default:
break;
}
}
$.ajax({
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"type": "GET",
"url": sSource,
"data": aoData,
success: function (msg, a, b) {
$.unblockUI();
var mappedCusNames = $.map(msg.Table, function (Item) {
return new searchGridListObj(Item);
});
var data = {
"draw": echo,
"recordsTotal": msg.Table2[0].TOTAL_NUMBER_OF_RECORDS,
"recordsFiltered": msg.Table1[0].FILTERED_RECORDS,
"data": mappedCusNames
};
fnCallback(data);
$("#dtSearchResult").show();
ko.cleanNode($('#dtSearchResult')[0]);
ko.applyBindings(VMCreateExtraction, $('#dtSearchResult')[0]);
}
})
},
"aoColumns": [{
"mDataProp": "C_UID"
}, {
"mDataProp": "C_LAST_NAME"
}, {
"mDataProp": "C_FIRST_NAME"
}, {
"mDataProp": "C_USER_ID"
}, {
"mDataProp": "C_EMAIL"
}, {
"mDataProp": "C_COMPANY"
}],
"aoColumnDefs": [{ "defaultContent": "", "targets": "_all" },
//I create a link in 1 st column
]
});
There is some configuration that I am missing here. I read on datatable forums and the only issue highlighted by people was that draw should be same as what we send on server side.
For anyone looking for an answer to this. Sad but i had to write my own function as below:
function sortIconHandler(thArray, sortCol, sortDir) {
for (i = 0; i < thArray.length; i++) {
if (thArray[i].classList.contains('sorting_asc')) {
thArray[i].classList.remove('sorting_asc');
thArray[i].classList.add("sorting");
}
else if (thArray[i].classList.contains('sorting_desc')) {
thArray[i].classList.remove('sorting_desc');
thArray[i].classList.add("sorting");
}
if (i == sortCol) {
if (sortDir == 'asc') {
thArray[i].classList.remove('sorting');
thArray[i].classList.add("sorting_asc");
}
else {
thArray[i].classList.remove('sorting');
thArray[i].classList.add("sorting_desc");
}
}
}
}
tharrray-> The array of all row headers(You can just write a jquery selector for this).
sortCol->Column on which sort is clicked (Datatable param iSortCol_0)
sortDir -> Sorting direction (Datatable param sSortDir_0)
I know this is an old thread, but make sure you don't have an .off() somewhere associated with the tables capture group in jQuery. I had a click event that (for some reason) I attached an off function to.. Took me 3 days to find it.