Attempting to hit an endpoint with a POST request using Protractor has been quite challenging for me. I have tried various versions of the code, utilizing different HTTP clients like 'request' and exploring other options as well.
In addition to that, I am also using cucumber and chai-as-promise in my testing process. However, when running the cucumber test, the POST request does not seem to execute properly. The test simply moves on to the next step without displaying any errors in the console. Subsequently, checking with a REST client confirms that the POST request was not successful.
I am wondering if moving the post request into a separate function within a class and passing the appropriate variables would make a difference?
Below is the code snippet:
this.When(/^I test this$/, function (next) {
var request = require('request');
var options = {
headers: {
'id': 'AQ8WHWC',
'sessionid': 'XnINW5KDQg=',
'Accept': 'application/json',
'Accept-Language': 'en-us',
'random': 'BS3P5Q'
},
form: { "pay_load": [] }
};
request.post('http://myurl.com/endpoint/test/', options, callback);
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
console.log(info);
}
}
browser.sleep(1).then(next)
});
Console output from running the cucumber test:
Scenario: this is a cool test
# endpoint/test/testing.feature:7
Given I run this endpoint test
# endpoint/test/testing.feature:8
When I test this
# endpoint/test/testing.feature:9
Then I see this
# endpoint/test/testing.feature:10
1 scenario (1 passed)
3 steps (3 passed)
[launcher] chrome passed
Done, without errors.