Karate replace in graphql - karate

I am not sure why replace is not working.
I have a graphql query:
mutation updateLocation{
updateLocation(input: {
address:"<address>",
id:"<storeID>",
name:"<name>",
workingHours: [
{
closingTime:"<closingTime>",
isClosed:false,
openingTime:"<openingTime>"
}
.......
And in feature file I have this:
Given def query = read ('classpath:graphQL/updateStore.graphql')
* replace query.address = "<address>"
* replace query.regionId = "<regionId>"
* replace query.name = "<name>"
* replace query.closingTime = "<closingTime>"
* replace query.openingTime = "<openingTime>"
* replace query.storeID = storeId
And request { query : '#(query)'}
When method post
Then status 200
Examples:
|address |regionId |name |closingTime |openingTime |
|Adrs1 |286 |st1 |20:00 |10:00 |
The replace works for address, regionid, and name but it does not work for closing time or opening time, these two values stay empty.
Also if I define header in background like this:
Given header Authorization = 'Bearer ' + token
I still have to add this line for each request in the same scenario, or I have been missing something?

Works for me:
* def query = 'closingTime:"<closingTime>"'
* replace query.closingTime = '20:00'
* match query == 'closingTime:"20:00"'
So please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
Note that * replace query.closingTime = closingTime should work. I recommend avoiding confusion by using different names for the Examples columns.

Related

Modified variable name in karate framework [duplicate]

This question already has an answer here:
Karate Http request add param conditionally
(1 answer)
Closed 1 year ago.
I want to modified param variable for my request(GET/POST) dynamically. As I have 2 different environment which takes different parameters for same request.
I tried below code, but not able to replace param variable(name).
I can replace param value successfully.
This function generate the dynamic param name for different enviornment
public static String paramDynamicVariable(String env, String param) {
String paramValue;
if (env.equals("test")) {
paramValue = '$' + param;
} else {
paramValue = param;
}
return paramValue;
}
Now when I am using paramValue in my test--
Scenario: xyz
Given path URLOfRequest
* print paramDynamicVariable(karate.env,'nameParam')
And param random.paramDynamicVariable(karate.env,'nameParam') = 10
It prints correct value, but in the next line it is not replacing for param name.
Please suggest if any solution is there to dynamic param name.
Please do something like this:
* def nameParam = paramDynamicVariable(karate.env, 'nameParam')
* def paramValues = {}
* paramValues[nameParam] = 10
And then:
* params paramValues
Since params accepts any JSON, all you need to do is create the JSON. Since the key is dynamic, it requires you to do a little more work.

Karate - substring - lastindexof trim/ strip/ slice help needed

I have a tricky authentication issue which requires multiple calls to ultimately obtain an access-token.
I am currently stumbling on the conversion of a returned correlation-id to a correct format.
The correlationID from a post response is returned as: Id-c5ea93607b0682a76040b5db 0; Id-c5ea93607b0682a76040b5db 2
I need to convert to c5ea93607b0682a76040b5db
I have tried with * def serviceId1 = correlationID.substring(correlationID.lastIndexOf('-')+ 1).trim()
which is resulting in c5ea93607b0682a76040b5db 2
I need to strip the " 2" value from the backend of the id to make it
from: 'c5ea93607b0682a76040b5db 2'
to: 'c5ea93607b0682a76040b5db'
I have tried many ways but cant refine to get working - ANY HELP IS MUCH APPRECIATED
The code below:
Scenario: Obtaining access_token
* def correlationID = 'Id-c5ea93607b0682a76040b5db 0; Id-c5ea93607b0682a76040b5db 2'
* def serviceId1 = correlationID.substring(correlationID.lastIndexOf('-')+ 1).trim()
* print 'correlationID ' + correlationID
* print 'serviceID ' + serviceId1
is resulting in c5ea93607b0682a76040b5db 2 ( I want to strip the trailing " 2" - how can I do this?
Here you go:
* def temp = 'c5ea93607b0682a76040b5db 2'
* def pos = temp.lastIndexOf(' ')
* def fixed = temp.substring(0, pos)
* match fixed == 'c5ea93607b0682a76040b5db'

Karate - Unable to replace graphql query variables through data driven scenario outline 'examples' [duplicate]

I am not sure why replace is not working.
I have a graphql query:
mutation updateLocation{
updateLocation(input: {
address:"<address>",
id:"<storeID>",
name:"<name>",
workingHours: [
{
closingTime:"<closingTime>",
isClosed:false,
openingTime:"<openingTime>"
}
.......
And in feature file I have this:
Given def query = read ('classpath:graphQL/updateStore.graphql')
* replace query.address = "<address>"
* replace query.regionId = "<regionId>"
* replace query.name = "<name>"
* replace query.closingTime = "<closingTime>"
* replace query.openingTime = "<openingTime>"
* replace query.storeID = storeId
And request { query : '#(query)'}
When method post
Then status 200
Examples:
|address |regionId |name |closingTime |openingTime |
|Adrs1 |286 |st1 |20:00 |10:00 |
The replace works for address, regionid, and name but it does not work for closing time or opening time, these two values stay empty.
Also if I define header in background like this:
Given header Authorization = 'Bearer ' + token
I still have to add this line for each request in the same scenario, or I have been missing something?
Works for me:
* def query = 'closingTime:"<closingTime>"'
* replace query.closingTime = '20:00'
* match query == 'closingTime:"20:00"'
So please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue
Note that * replace query.closingTime = closingTime should work. I recommend avoiding confusion by using different names for the Examples columns.

Embedded expressions not replaced if surrounded by dot and underscores in it

The embedded expressions are not replaced when appended, prepended or surrounded by characters in the following way
* def RADName = 'IntegrationFirstRAD'
* def tenantID = '1452119626'
* def out =
"""
{
"nsName": "fld_<tenantID>_stage00.rad.<RADName>_.resources:<RADName>_resource"
}
"""
* print out
Executing the scenario returns:
"nsName":"fld_1452119626_stage00.rad.<RADName>_.resources:<RADName>_resource
In the above scenario 'RADName' is not replaced with the value
Please use the replace keyword:
* def out = { nsName: 'fld_<tenantID>_stage00.rad.<RADName>_.resources:<RADName>_resource' }
* replace out.RADName = 'IntegrationFirstRAD'
* replace out.tenantID = '1452119626'
* match out == { "nsName": "fld_1452119626_stage00.rad.IntegrationFirstRAD_.resources:IntegrationFirstRAD_resource" }
You seem to be confused between embedded expressions and Scenario Outlines.
I guess it is worth saying this again, you really really really should read the documentation fully once.

how to call a sql script or query in background for a Karate feature?

I have Karate feature with a long list of operations. I need to manually have some test data setup before running these tests which is essentially one sql query. Is there a way we can have this query run in "background" in Karate?
I'm trying to update all the values in status which aren't "ready_to_test" to "ready_to_test".
Say my query is
update my_table
set status = 'ready_to_test'
where status != 'ready_to_test';
EDIT:
I am trying to run the update query as follows
use JDBC to setup test data
* def config = {username: 'postgres', password: 'postgres', url: 'jdbc:postgresql://localhost:5432/postgres', driverClassName: 'org.postgresql.Driver'}
* def DbUtil = Java.type('com.utils.DbUtils')
* def db = new DbUtil(config)
* def correctStatus = 'ready_to_test'
* def testData = db.cleanRows('UPDATE MY_TABLE M SET M.STATUS = ' 'WHERE M.STATUS != ' + correctStatus)
Also tried
* def testData = db.cleanRows('UPDATE MY_TABLE SET STATUS = 'ready_to_test' WHERE STATUS != 'ready_to_test)
Please see if the callSingle API solves your need.
https://github.com/intuit/karate#karate-callsingle
var result = karate.callSingle('classpath:common.feature', { some: 'config' });
Also see hooks: https://github.com/intuit/karate#hooks
I also wanted to update particular row in database and I tried add the function below to DbUtils then use it feature file , It works.
public void insertRows(final String sql) {
System.out.println("Inserting data to database...");
jdbc.batchUpdate(new String[]{sql});
}
For more information you can watch this video