From 67548ea1c93c82c6f81714fb0e6a672ddd0b9d31 Mon Sep 17 00:00:00 2001 From: Tasneem Natshah Date: Sun, 8 Sep 2024 06:36:07 +0300 Subject: [PATCH] Switch the URL request package from Request to Axios #170 --- lib/custom-assertions/responseStatus.js | 28 ++++++++++++++++++++----- package.json | 2 +- tests/step-definitions/webship.js | 24 ++++++++++++++++----- 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/lib/custom-assertions/responseStatus.js b/lib/custom-assertions/responseStatus.js index b85f805..e5d7a2a 100755 --- a/lib/custom-assertions/responseStatus.js +++ b/lib/custom-assertions/responseStatus.js @@ -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 + }); }); }); -} +} \ No newline at end of file diff --git a/package.json b/package.json index 2f57bda..6eea958 100755 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "geckodriver": "*", "webpack-cli": "~5", "webpack-dev-server": "~4", - "request": "~2" + "axios": "~1" }, "bin": { "steplist": "./steplist.js" diff --git a/tests/step-definitions/webship.js b/tests/step-definitions/webship.js index c5760ce..a7ead63 100755 --- a/tests/step-definitions/webship.js +++ b/tests/step-definitions/webship.js @@ -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. @@ -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 }); }); }); @@ -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 }); }); });