How to create Identical GET request with different parameters in same project
- 8 months ago
This is perfect case for data driven test. Put all positive and negative cases together and also have an additional column in the data for status code which can be used in the assertion.
Again if certain request need two headers, certain request needs single header then that too can be achieved with the the help of data and a groovy combination with out creating n no of resources.
- Resource:
Create one method resource without any headers and this will be used in the test. Even if there are any, does not matter as it can be taken care in the groovy script.
- Data creation:
Here is the template for the data/CSV file for all the tests, just add your cases and expected status code.
| AuthHeaderValue | XAuthHeaderValue | IsAuthHeaderRequired | IsXAuthHeaderRequired | ExpectedStatusCode | |-----------------|------------------|-----------------------|-----------------------|--------------------| | valid | invalid | false | true | 400 | | invalid | invalid | true | true | 400 | | valid | invalid | true | false | 200 |
The meaning of the first row is to add only X-Auth-key to the request as it is defined in the data saying IsXAuthHeaderRequired is true and other is false. Of course this has to be done in the groovy that I mentioned earlier. But gives ability to control things in the groovy as needed.
Similarly, in the second row, both headers needed as per the data defined.
Third row is to use valid Authroization header.
- Test case:
Create data driven test using above created resource method and above created data file.
Use the assertion for status verification from data ie., ExpectedStatusCode
- Groovy:
Now there are two ways of adding the required headers to the request dynamically, the groovy I am talking about.
- With an explicit groovy script step which reads data from data file and add required headers based on the data defined. Of course this step has to be placed before rest request.
- Here the request header(s) will be added to the request and visible in the request editor.
- It has to be taken care for each row of the data (in the data driven tests) i.e., remove existing headers(because, previous run/row headers will be there) first (at least Authorization, and X-Auth-Key) and add headers as per the current row of data.
- Alternatively use Events/Listeners feature to add the headers dynamically beforeSubmit event.
- The advantage with this is that the header(s) will be added at runtime at object level.
- Headers aren't shown/visible in the request editor like in the above approach.
- No need of removal of Authorization, X-Auth-Key headers as they aren't there unlike #1 approach
- May be to avoid other test cases, execute this only for the specific test case/suite.
Hope you knew how to add header using groovy based on above conversations. Only thing is add respective header based on condition defined in data file.
The pseudo code :
Remove the headers, Authorization,X-Auth-Key if present.
If IsXAuthHeaderRequired is true, then add X-Auth-Key header to the request, and take the value from XAuthHeaderValue
Similary, add the header Authorization to the request if IsAuthHeaderRequired is true and value from AuthHeaderValue
Try this approach and let me know me know in case any trouble.