0

I am a bit confused and need some clarify on this

  Background:
    * def successBody = 'util/successRequestBody.json'

  @test1 @ignore
  Scenario: Verify user 
    Given url
    * def id = id
    * def requestBody = read (successBody)
    And request requestBody
    When method post
    Then status 201

  @test2
  Scenario: First create new user and then delete same user 
    * def id = '123'
    # First call POST user to create a new user
    * def postuser = call read('user.feature@test1') {id: id}
    Given url
    When method delete
    Then status 204

I am providing value in request body for creating new user like this in successRequestBody.json

{
  "id": "#(id)",
  "name": "abc"
}

The above doesn't work. But when I provide like this it works. Please guide how the parameters should be passed in calling a feature from another. I am passing variable name id from test2 while calling test1 but in test1 it is reading id1 not id? Can someone please explain?

  Background:
    * def successBody = 'util/successRequestBody.json'

  @test1 @ignore
  Scenario: Verify user 
    Given url
    # I am setting variable name id from test2 but here it is reading id1 not id?
    * def id = id1
    * def requestBody = read (successBody)
    And request requestBody
    When method post
    Then status 201

  @test2
  Scenario: First create new user and then delete same user 
    * def id1 = '123'
    # First call POST user to create a new user
    * def postuser = call read('user.feature@test1') {id: id1}
    Given url
    When method delete
    Then status 204
Maddy
  • 674
  • 1
  • 7
  • 26

1 Answers1

1

The call syntax is wrong, you have to use embedded expressions:

* def postuser = call read('user.feature@test1') { id: '#(id1)' }

Here's a tip. It is not mandatory to pass parameters. Variables in the "caller" will be visible to the "called" feature. This below has the same effect as above.

* def id = id1
* def postuser = call read('user.feature@test1')

Please read the documentation and examples carefully. If still stuck, follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    can the `call read('user.feature@test1')` be executed conditionally? if so, whats the syntax ? `if (condition === true) call read('user.feature@test1')` doesn't seem to work. thanks in advance. – 500865 Jun 29 '22 at 09:57
  • @500865 use `karate.call('')` if within a JS block: https://stackoverflow.com/a/50350442/143475 – Peter Thomas Jun 29 '22 at 10:28
  • it isn't within a JS block. It is a regular statement. – 500865 Jul 03 '22 at 10:14
  • @500865 follow this process if you still need help: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue – Peter Thomas Jul 03 '22 at 11:03