Search code examples
cucumberbddcucumber-jvmcucumberjskarate

How do i export as global variable


I have a karate feature file

Scenario: Feature

Given url 'https://testlocal/v1/test/authorize'
And request 'type=code&uri=http://www.testlocal/app'
And header Accept = 'application/x-www-form-urlencoded'
And header Content-Type = 'application/x-www-form-urlencoded'
When method POST
Then status 302
* def code = responseHeaders['Location'][0].substring(59,95)
* print code

when i run the above feature file i can see the code being printed seperately, however I would like to use that code in the following of the same feature file

Scenario: Login

Given url 'https://testlocal/v1/test/authorize/login'
And request 'username=test1@gmail.com&password=123!&requestId=**"I am not sure how to call the above code here"**
And header Accept = 'application/x-www-form-urlencoded'
And header Content-Type = 'application/x-www-form-urlencoded'
When method POST
Then status 302

Thanks


Solution

  • Sounds like you just need a Background: section, refer to this as a good example of re-use: headers.feature.

    If you really need re-use across multiple feature files, refer to the documentation on how to re-use feature files via the call keyword.

    I'd like to suggest an improvement to your test, you can do the following instead of manually forming the request, using the form field syntax:

    EDIT: full solution

    Background:
    Given url 'https://testlocal/v1/test/authorize'
    And form field type = 'code'
    And form field uri = 'http://www.testlocal/app'
    When method post
    Then status 302
    And def code = responseHeaders['Location'][0].substring(59,95)
    
    Scenario: login
    Given path 'login'
    And form field username = 'test1@gmail.com'
    And form field password = '123!'
    And form field requestId = code
    When method post
    Then status 302