UNPKG

pw-api-plugin

Version:

Playwright plugin for comprehensive API testing and result presentation using the Playwright UI, Trace Viewer, and HTML Report. It significantly aids debugging processes and supports both Playwright's native API and Axios requests.

302 lines (301 loc) 17.2 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a, _axiosApi_displayApiCallInfo; Object.defineProperty(exports, "__esModule", { value: true }); const test_1 = require("@playwright/test"); const axios_1 = __importDefault(require("axios")); const displayUi_1 = require("./displayUi"); /** * Class providing methods to make HTTP requests using Axios API and log the request and response data on the Playwright UI. */ class axiosApi { } _a = axiosApi; // PUBLIC METHODS // -------------- /** * Class function that makes an AXIOS API call and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the API call. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string | any} arg1 - The URL of the API call or an object containing the Axios request configuration object. * @param {any} [arg2] - The optional Axios request configuration object if `arg1` is a string. * @returns {Promise<AxiosResponse>} - A promise that resolves to the Axios response. */ axiosApi.axios = async ({ page }, arg1, arg2) => { let url; let config; if (typeof arg1 === 'string') { url = arg1; config = arg2; } else { ({ url } = arg1, config = __rest(arg1, ["url"])); } return await test_1.test.step(`Axios Api Call - REQUEST - ${url}`, async () => { const start = Date.now(); const response = await (0, axios_1.default)(arg1, arg2); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, config, fromCall: "AXIOS" }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios REQUEST and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the request. * @param {Page} [params.page] - Optional the Playwright page object. * @param {any} config - The Axios request configuration object. * @returns {Promise<AxiosResponse>} - A promise that resolves to the Axios response. */ axiosApi.request = async ({ page }, config) => { return await test_1.test.step(`Axios Api Call - REQUEST - ${config.url}`, async () => { const start = Date.now(); const response = await axios_1.default.request(config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { config, fromCall: "REQUEST" }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios GET request to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the GET request. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the GET request to. * @param {any} [config] - Optional Axios request configuration object. * @returns {Promise<AxiosResponse>} - The response from the Axios GET request. */ axiosApi.get = async ({ page }, url, config) => { return await test_1.test.step(`Axios Api Call - GET - ${url}`, async () => { const start = Date.now(); const response = await axios_1.default.get(url, config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, config, method: 'GET' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios DELETE request to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the DELETE request. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the DELETE request to. * @param {any} [config] - Optional Axios request configuration object. * @returns {Promise<AxiosResponse>} - A promise that resolves to the Axios response. */ axiosApi.delete = async ({ page }, url, config) => { return await test_1.test.step(`Axios Api Call - DELETE - ${url}`, async () => { const start = Date.now(); const response = await axios_1.default.delete(url, config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, config, method: 'DELETE' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios HEAD request to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the request. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the HEAD request to. * @param {any} [config] - Optional Axios request configuration object. * @returns {Promise<AxiosResponse>} - A promise that resolves to the Axios response. */ axiosApi.head = async ({ page }, url, config) => { return await test_1.test.step(`Axios Api Call - HEAD - ${url}`, async () => { const start = Date.now(); const response = await axios_1.default.head(url, config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, config, method: 'HEAD' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios OPTIONS request to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} param0 - An object containing the page instance. * @param {Page} param0.page - The Playwright page instance. * @param {string} url - The URL to which the OPTIONS request is made. * @param {any} [config] - Optional Axios request configuration object. * @returns {Promise<AxiosResponse>} - A promise that resolves to the Axios response. */ axiosApi.options = async ({ page }, url, config) => { return await test_1.test.step(`Axios Api Call - OPTIONS - ${url}`, async () => { const start = Date.now(); const response = await axios_1.default.options(url, config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, config, method: 'OPTIONS' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios POST request to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the POST request. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to which the POST request is sent. * @param {any} [data] - Optional the data to be sent in the body of the POST request. * @param {any} [config] - The Axios request configuration options. * @returns {Promise<AxiosResponse>} - A promise that resolves to the Axios response. */ axiosApi.post = async ({ page }, url, data, config) => { return await test_1.test.step(`Axios Api Call - POST - ${url}`, async () => { const start = Date.now(); const response = await axios_1.default.post(url, data, config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, data, config, method: 'POST' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios PUT request to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the PUT request. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the PUT request to. * @param {any} [data] - Optional the data to be sent in the PUT request body. * @param {any} [config] - The Axios request configuration. * @returns {Promise<AxiosResponse>} - A promise that resolves to the Axios response. */ axiosApi.put = async ({ page }, url, data, config) => { return await test_1.test.step(`Axios Api Call - PUT - ${url}`, async () => { const start = Date.now(); const response = await axios_1.default.put(url, data, config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, data, config, method: 'PUT' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios PATCH request to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the PATCH request. * @param {Page} params.page - The Playwright Page object. * @param {string} url - The URL to send the PATCH request to. * @param {any} [data] - Optional the data to be sent in the PATCH request body. * @param {any} [config] - Optional Axios request configuration. * @returns {Promise<AxiosResponse>} - A promise that resolves to the Axios response. */ axiosApi.patch = async ({ page }, url, data, config) => { return await test_1.test.step(`Axios Api Call - PATCH - ${url}`, async () => { const start = Date.now(); const response = await axios_1.default.patch(url, data, config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, data, config, method: 'PATCH' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios POSTFORM request with 'FormData' to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the API call. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to which the request is sent. * @param {any} [data] - Optional the 'FormData' instance to be sent as the form body. * @param {any} [config] - Optional Axios request configuration. * @returns {Promise<AxiosResponse>} - The Axios response object. */ axiosApi.postForm = async ({ page }, url, data, config) => { return await test_1.test.step(`Axios Api Call - POSTFORM - ${url}`, async () => { const start = Date.now(); const response = await axios_1.default.postForm(url, data, config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, data, config, method: 'POSTFORM' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios PUTFORM request with 'FormData' to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the request. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the PUT request to. * @param {any} [data] - Optional the 'FormData' instance to be sent with the request. * @param {any} [config] - Optional Axios configuration for the request. * @returns {Promise<AxiosResponse>} - A promise that resolves to the Axios response. */ axiosApi.putForm = async ({ page }, url, data, config) => { return await test_1.test.step(`Axios Api Call - PUTFORM - ${url}`, async () => { const start = Date.now(); const response = await axios_1.default.putForm(url, data, config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, data, config, method: 'PUTFORM' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes an Axios PATCHFORM request with 'FormData' to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the function. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the PATCH request to. * @param {any} [data] - Optional the 'FormData' instance to be sent with the PATCH request. * @param {any} [config] - Optional Axios request configuration. * @returns {Promise<AxiosResponse>} - The Axios response object. */ axiosApi.patchForm = async ({ page }, url, data, config) => { return await test_1.test.step(`Axios Api Call - PATCHFORM - ${url}`, async () => { const start = Date.now(); const response = await axios_1.default.patchForm(url, data, config); await __classPrivateFieldGet(_a, _a, "f", _axiosApi_displayApiCallInfo).call(_a, { url, data, config, method: 'PATCHFORM' }, { response, duration: Date.now() - start }, page); return response; }); }; // PRIVATE METHODS // --------------- /** * Obtains and displays API call information on the Playwright UI and HTML Report. * * @param {string} request.url - The URL of the API call. * @param {any} request.data - The data sent with the API call. * @param {any} request.config - The Axios configuration object for the API call. * @param {string} request.method - The HTTP method used for the API call. * @param {string} request.fromCall - The source of the API call. * @param {AxiosResponseInterface} responseDetails - The response details including response object and duration. * @param {AxiosResponse} responseDetails.response - The Axios response object. * @param {number} responseDetails.duration - The duration of the API call. * @param {Page} [page] - Optional the page object where the API call information will be displayed. * @param {AxiosRequestInterface} request - The request details including URL, data, config, method, and the source of the call. * @returns {Promise<{ requestData: RequestDataInterface, responseData: ResponseDataInterface }>} A promise that resolves to an object containing the request data and response data. */ _axiosApi_displayApiCallInfo = { value: async ({ url, data, config, method, fromCall }, { response, duration }, page) => { var _b; // Form the request data object for representation on the UI let requestData = { url: url || (config === null || config === void 0 ? void 0 : config.url) || '', method: (method === null || method === void 0 ? void 0 : method.toUpperCase()) || ((_b = config === null || config === void 0 ? void 0 : config.method) === null || _b === void 0 ? void 0 : _b.toUpperCase()) || 'GET', data: data || (config === null || config === void 0 ? void 0 : config.data), // In case od an axios.request() data will be provided in config fromCall, }; if (config) { const { transformRequest, transformResponse, paramsSerializer, adapter, onUploadProgress, onDownloadProgress, cancelToken, validateStatus } = config, configWithoutFuncs = __rest(config, ["transformRequest", "transformResponse", "paramsSerializer", "adapter", "onUploadProgress", "onDownloadProgress", "cancelToken", "validateStatus"]); let _c = structuredClone(configWithoutFuncs), { url, method, headers, data, params, auth, proxy } = _c, otherOptions = __rest(_c, ["url", "method", "headers", "data", "params", "auth", "proxy"]); if (Object.keys(otherOptions).length === 0) otherOptions = undefined; requestData = Object.assign(Object.assign({}, requestData), { headers, params, auth, proxy, otherOptions, funcs: { transformRequest, transformResponse, paramsSerializer, adapter, onUploadProgress, onDownloadProgress, cancelToken, validateStatus } }); } // Form the request data object for representation on the UI const status = response.status; const responseData = { status, statusClass: status.toString().charAt(0) + 'xx', // Used for give color style to status code. (Eg: '2xx', '4xx', '5xx') statusText: response.statusText, headers: response.headers, body: response.data, duration }; await (0, displayUi_1.addApiCardToUI)(requestData, responseData, page); return { requestData, responseData }; } }; exports.default = axiosApi;