1

I had written karate tests for one environment only (staging). Since the tests are successful on capturing bugs (thanks a lot Karate and Intuit team!), there is now request to run the tests on production.

Our tests are graphql-based where most of the requests are query. I wonder if it is possible for us to switch variables based on karate.env we passed on terminal?

Most of our requests look like this:

And def variables = {objectID:"1234566", cursor:"1", cursorType:PAGE, size:'10', objectType:USER}
And request { query: '#(query)', variables: '#(variables)' }
When method POST
Then status 200

I had tried reading the conditional-logic page on github page but haven't yet found a success.

What I tried so far is:

* if (karate.env == 'staging') * def variables = {objectID:"1234566", cursor:"1", cursorType:PAGE, size:'10', objectType:USER}

But to no success.

Any help will be greatly appreciated. Thanks a lot!

Raymond
  • 604
  • 1
  • 9
  • 27

2 Answers2

3

We keep our graphql queries & variables in separate json files, but, we're attempting to solve the same issue. Based on what Peter wrote I came up with this, though it will likely get cleaned up before deployment.

Given def query = read('graphqlQuery.graphql')
    And def prodVariable = read('prod-variables.json')
    And def stageVariable = read('stage-variables.json')
    And def variables = karate.env == 'prod' ? prodV : stageV
    And path 'api/' + 'graphql'
    And request { query: '#(query)', variables: '#(variables)' }
    When method post
    Then status 200
Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
James Ryan
  • 31
  • 5
1

This should be easy:

* def variables = karate.env == 'staging' ? { objectID: "1234566", cursor: "1", cursorType: 'PAGE', size: '10', objectType: 'USER' } : { }

Here is another hint:

* def data = { staging: { foo: 'bar }, production: { foo: 'baz' } }
* def variables = data[karate.env]

EDIT: also see this explanation: https://stackoverflow.com/a/59162760/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248