Skip to content

Commit

Permalink
Switch the URL request package from Request to Axios #170
Browse files Browse the repository at this point in the history
  • Loading branch information
TasneemNatshah committed Sep 8, 2024
1 parent 8d79e2c commit 67548ea
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
28 changes: 23 additions & 5 deletions lib/custom-assertions/responseStatus.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
const request = require("request");
const axios = require('axios');

module.exports.assertion = function (negativeCase, expectedStatusCode) {

return browser.url(function (currentURL) {
browser.url(function (currentURL) {
request(currentURL.value, (error, response, body) => {
if (negativeCase) {
return browser.assert.not.equal(response.statusCode, expectedStatusCode).end();
return axios.get(currentURL.value)
.then(function (response) {
browser.assert.equal(response.status, expectedStatusCode);
})
.catch(function (error) {
browser.assert.equal(error.status, expectedStatusCode);
})
.finally(function () {
// always executed
});
}
return browser.assert.equal(response.statusCode, expectedStatusCode).end();
return axios.get(currentURL.value)
.then(function (response) {
browser.assert.equal(response.status, expectedStatusCode);
})
.catch(function (error) {
browser.assert.equal(error.status, expectedStatusCode);
})
.finally(function () {
// always executed
});
});
});
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"geckodriver": "*",
"webpack-cli": "~5",
"webpack-dev-server": "~4",
"request": "~2"
"axios": "~1"
},
"bin": {
"steplist": "./steplist.js"
Expand Down
24 changes: 19 additions & 5 deletions tests/step-definitions/webship.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { Given} = require('@cucumber/cucumber');
const { When, Before} = require('@cucumber/cucumber');
const { Then} = require('@cucumber/cucumber');

const request = require('request');
const axios = require('axios');

/**
* Opens homepage.
Expand Down Expand Up @@ -885,8 +885,15 @@ When(/^(I|we)* wait max of (\d*) minute(s)*$/, function (pronoundCase, number, w
*/
Then(/^the response status code should be (\d+)$/, function (expectedStatusCode) {
return browser.url(function (currentURL) {
request(currentURL.value, (error, response, body) => {
browser.assert.equal(response.statusCode, expectedStatusCode);
axios.get(currentURL.value)
.then(function (response) {
browser.assert.equal(response.status, expectedStatusCode);
})
.catch(function (error) {
browser.assert.equal(error.status, expectedStatusCode);
})
.finally(function () {
// always executed
});
});
});
Expand All @@ -898,8 +905,15 @@ Then(/^the response status code should be (\d+)$/, function (expectedStatusCode)
*/
Then(/^the response status code should not be (\d+)$/, function ( expectedStatusCode) {
return browser.url(function (currentURL) {
request(currentURL.value, (error, response, body) => {
browser.assert.not.equal(response.statusCode, expectedStatusCode);
axios.get(currentURL.value)
.then(function (response) {
browser.assert.not.equal(response.status, expectedStatusCode);
})
.catch(function (error) {
browser.assert.not.equal(error.status, expectedStatusCode);
})
.finally(function () {
// always executed
});
});
});
Expand Down

0 comments on commit 67548ea

Please sign in to comment.