问题
I have an authentication feature working, but would like to make it reusable. I'm not able to get working through the methods I've tried so far.
The standalone method that works is:
# Created at 10/4/18
Feature: #Authentication Management
Background:
* url gatewayUrl
* def myid = 'myid'
* def mysecret = 'mysecret'
Scenario: # Generate authentication token for x user
Given path 'mypath'
And header Content-Type = 'application/x-www-form-urlencoded'
And request 'grant_type=api_key&myid=' + myid + '&mysecret=' + mysecret
When method post
Then status 200
* print response.Token
Here is the working POST request for this one:
1 > POST <authURL>
1 > Accept-Encoding: gzip,deflate
1 > Connection: Keep-Alive
1 > Content-Length: 108
1 > Content-Type: application/x-www-form-urlencoded; charset=UTF-8
1 > Host: <authURL>
1 > User-Agent: Apache-HttpClient/4.5.5 (Java/10.0.2)
grant_type=api_key&myid=myId&mysecret=mySecret
I'd like to replace myid and mysecret with the args from the feature that performs the call. Using '#(myid)' and '(#mysecret)' doesn't seem to work anywhere but when defining param(s). Is there a way to do this replacement, or another equivalent way of building up the request data to send?
Thanks
回答1:
Of course it is worth reading through the docs carefully: https://github.com/intuit/karate#calling-other-feature-files
Try this:
Feature: #Authentication Management
Scenario: # Generate authentication token for x user
Given url gatewayUrl
And path 'mypath'
And header Content-Type = 'application/x-www-form-urlencoded'
And request 'grant_type=api_key&myid=' + myid + '&mysecret=' + mysecret
When method post
Then status 200
And then call as follows:
Feature: main
Background:
* def result = call read('auth.feature') { myid: 'actualid', mysecret: 'actualsecret' }
* def token = result.response.Token
# remaining test uses token
来源:https://stackoverflow.com/questions/52725546/properly-calling-an-authorization-karate-feature-with-arguments