Want to pass multiple URL in karateDSL Scenario with same Path - api

Multiple URL i am going to pass in background for each url path should run scenario
Background:
* header Authorization = call read('basic-auth.js') { username: 'admin', password: 'admin' }
Background:
* header Authorization = call read('basic-auth.js') { username: 'admin', password: 'admin' }
Scenario: Creates the webservices api page
Given path 'rest/activescript/about'
When method get
Then status 200
* def B = response
* print B
Actual Result : is last url in background will consider in scenario to execute
expected Result : all Url in background should run with diff scenario

The following should work. Though I'm not sure it is recommended to do this. The url is usually defined in karate-config.js for the whole run.
Scenario Outline: Scenario called multiple times
* url '<newUrl>'
* header Authorization = call read('basic-auth.js') { username: 'admin', password: 'admin' }
Given path 'rest/activescript/about'
When method get
Then status 200
* def B = response
* print B
Examples:
|newUrl |
|URL1|
|URL2 |

Related

Karate - Trouble passing correct headers for authorization

I am have some problems passing in the correct headers for my graphql endpoints
The use case in Postman:
call requestToken endpoint to obtain sessionToken value
requestToken response contains Key Value " and Token Value.
For subsequent calls, I set postman headers as:
Key = X_SESSION_TOKEN Value = Token Value
The user case in Karate
1st feature 'requestToken.feature' successfully calls and stores key + tokenValue
2nd feature successfully defines and prints the token value
here is my 2nd request:
Feature: version
Background:
* url 'http://api-dev.markq.com:5000/'
* def myFeature = call read('requestToken.feature')
* def authToken = myFeature.sessionToken
* configure headers = { 'X_SESSION_TOKEN': authToken , 'Content-Type': 'application/json' }
Scenario: get version
Given path 'query'
Given text query =
"""
query {
version
}
"""
And request { query: '#(query)' }
When method POST
Then status 200
And print authToken
And print response
I am not sure I send the headers right. Its coming back 200, but I keep getting a error 'token malformed' in the response message
Any suggestions? New at this, thanks!
Honestly this is hard to answer, a LOT depends on the specific server.
EDIT: most likely it is this change needed, explained here: https://github.com/intuit/karate#embedded-expressions
* configure headers = { 'X_SESSION_TOKEN': '#(authToken)' , 'Content-Type': 'application/json' }
2 things from experience:
should it be X-SESSION-TOKEN
add an Accept: 'application/json' header
And try to hardcode the headers before attempting call etc.
Here is an example that works for me:
* url 'https://graphqlzero.almansi.me/api'
* text query =
"""
{
user(id: 1) {
posts {
data {
id
title
}
}
}
}
"""
* request { query: '#(query)' }
* method post
* status 200

Karate Api Testing - How to pass data from one feature file to another [duplicate]

This question already has an answer here:
Calling common scenario from another scenario in same feature file where request body is store in json file in karate [duplicate]
(1 answer)
Closed 1 year ago.
I need to pass data from one feature file to another.
Feature(1): Create a new user
Background:
* url 'http://127.0.0.1:8900/'
* header Accept = 'application/json'
Scenario: Create a new user
Given path '/user'
And request {"email" : "test#test.com", "name" : "Brian"}
When method post
And def newUser = $..id
Then status 201
Feature(2): Call newUser from feature 1
Background:
* url 'http://127.0.0.1:8900/'
* header Accept = 'application/json'
Scenario: Call User
* def newUser = $..id
* print newUser
Please read the docs: https://github.com/intuit/karate#calling-other-feature-files
* def aVariable = "can be anything"
* def result = call read('one.feature') { some: 'data', useExpression: #(aVariable) }
And in one.feature you can get access to the JSON "argument"
* print some
Which should print the value data

Log requests from called features in reports [duplicate]

We are using the report verbosity option available in Karate
I have a MarketingPreferenceTest.feature calling BBB.feature.
The features are as below:
MarketingPreferenceTest.feature
Background:
* url Url
* table credentials
|Email |Password|
|'aaa#test.com'|'test1234'|
* def result = karate.callSingle('classpath:resources/BBB.feature',credentials)
Scenario Outline: Get MS
Given path 'abc'
When method GET
Then status 200
BBB.feature:
Background:
* configure retry = { count: 5, interval: 1000 }
* configure headers = { 'Content-Type': 'application/json'}
* url authenticationUrl
Scenario: Login
Given path 'login'
And request { email: '#(Email)' , password: '#(Password)' }
And retry until responseStatus == 200 && response.loginResponse.loggedIn == true
When method post
My karate.config has
karate.configure('report', { showLog: true, showAllSteps: false } );
When i run the tests in parallel, i want to see all the Given-When-Then's printed in my cucumber report from BBB.feature. How do i achieve it ?
The cucumber report is shown below which doesn't have step definitions from BBB.feature :
Expected Result: Would like to see the Steps of BBB.feature in my report marked in a rectangle box below
Just make the step involving a callSingle use a Gherkin keyword:
When def result = karate.callSingle('classpath:resources/BBB.feature',credentials)

pass parameters to after-feature karate

I discovered after-feature in karate which is very useful. But I didn't find how to pass parameters to after-feature from main feature. Ex: access token to delete a user account or a user_id.
Here is call of after-feature.feature in my main feature:
* configure afterFeature = function(){ karate.call('classpath: AfterFeature.feature'); }
Here is my AfterFeature.feature
Scenario:
* url 'XXX'
* path 'YYY'
* param foo = bar which should come from main feature
* header Authorization = 'Bearer ' + accessToken which should come from main feature
* method delete
* status 204
karate.call() can take parameters.
karate.call('classpath: AfterFeature.feature', { some: 'value' });

How to send the saved auth-token in another request?

Scenario: Verify that Authentication is done or not
Given url '***********'
Given path 'authenticate'
And form field username = 'admin_cs'
And form field password = '********'
When method post
Then status 200
And header tokenn = response.token
* def accessToken = response.token
* print accessToken
Scenario: Verify Get all Clients
Given url '************'
Given path 'users/usersAssignable'
* header x-auth-token = accessToken
When method get
Then status 200
* def response = response
* print response
Please combine the two Scenario-s into one. Or move the first one here into the Background. Please read this very carefully: https://github.com/intuit/karate#script-structure