0

Is there any way to create a common variable at the feature file level & use it across scenarios instead of hardcoding as below for each step in cucumber feature files using java

eg:
Feature: Test
Scenario: scenario 1
  Given a user
  When get request is sent to "url"
  Then response code is 200

Scenario: scenario 2
  Given a user
  When get request is sent to "url"
  Then response code is 200

Want to do something like this

eg:
Feature: Test
global_variable_url=url
Scenario: scenario 1
  Given a user
  When get request is sent to "<global_variable_url>"
  Then response code is 200

Scenario: scenario 2
  Given a user
  When get request is sent to "<global_variable_url>"
  Then response code is 200

user16666530
  • 1
  • 1
  • 1
  • If you're testing http calls and response codes cucumber might not be the right tool. Or you've written the scenario with too much detail. https://www.gregbeech.com/2014/01/19/effective-api-testing-with-cucumber/ – M.P. Korstanje Dec 27 '21 at 13:45

4 Answers4

0

You can use Background keyword that is used to set up the common state for all scenarios like.

Feature: Test

Background:
  Given base url "http://my.base/url"

Scenario: scenario 1
  Given a user
  When get request is sent to base url
  Then response code is 200

Scenario: scenario 2
  Given a user
  When get request is sent to base url
  Then response code is 200

When you process Given step from your Background you set the shared reference to that url value and use it from other steps.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27
0

Sharing a variable across different scenarios is not a good practice as scenarios should be independent and this would being a risk to impact result of one scenario from another one.

That being said, you can preferrably use a Background step which defines the value you want to share and use it within your When steps:

Feature: Test

Background:
  Given the global url is "..."

Scenario: scenario 1
  Given a user
  When get request is sent to global url
  Then response code is 200

Scenario: scenario 2
  Given a user
  When get request is sent to global url
  Then response code is 200

In the code of the Given the global url is "..." step, store the value in a class attribute, it will be available during the scenario only but not in other scenarios as a new class will be created.

Gaël J
  • 11,274
  • 4
  • 17
  • 32
0

what you are looking for is supported with qaf-cucumber. It provides resource configuration capability and also a way to use in your feature file or code. With qaf provided base.url in properties file, your step can look like

When get request is sent to "${base.url}"

Furthermore, you can utilize web-service support which provides ready to use steps (doesn't required to write any code).

Scenario: scenario 1
  When user requests '${req.examplereq.get}'
  Then response should have status code 200

where req.examplereq.get represents request call in properties file, may look like below:

myservice-request-calls.properties

#request call repository for my-service.
req.examplereq.get={\
'headers':{},\
'endPoint':'/myservice-endpoint',\
'baseUrl':'${env.baseurl}',\
'method':'GET',\
'query-parameters':{\
  'param1':'val1',\
  'param2':'val2'\
 },\
}

user861594
  • 5,733
  • 3
  • 29
  • 45
0

Instead of Cucumber I recommend you evaluate Karate. Disclaimer: I'm the lead developer.

Karate was created to solve the problems you run in-to when you use Cucumber or REST-assured (or especially both combined) - for API testing. To specifically answer this question, you can declare a variable globally or in the Background for an entire Feature. JSON and JS expression support is "built-in". You can even re-use other Feature files, which is not possible in Cucumber.

Feature:

Background:
* def payload = { hello: 'world' }

Scenario:
* url 'https://httpbin.org/post'
* request payload
* method post
* status 200
* match response.json == payload

There are many teams that have fallen into this trap of using Cucumber for API testing because of blog posts by influential yet mis-informed bloggers etc. I humbly request those who read this answer to keep an open-mind and look at the evidence.

BDD is not recommended for API testing. You need a lower-level framework, but something that still can be "readable" (to some extend) by business users.

For more information, please refer to this answer: https://stackoverflow.com/a/47799207/143475

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