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.

213 lines (212 loc) 11.4 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 _a, _pwApi_displayApiCallInfo; Object.defineProperty(exports, "__esModule", { value: true }); const test_1 = require("@playwright/test"); const displayUi_1 = require("./displayUi"); /** * Class providing methods to make HTTP requests using Playwright native API and log the request and response data on the Playwright UI. */ class pwApi { } _a = pwApi; // PUBLIC METHODS // -------------- /** * Class function that makes a Playwright FETCH REQUEST to the specified URL and logs the request and response data on the Playwright UI. * * @param {Object} params - The parameters for the fetch function. * @param {APIRequestContext} params.request - The API Playwright request context. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string | Request} urlOrRequest - The URL or request object to fetch. * @param {any} [options] - Optional fetch options. * @returns {Promise<APIResponse>} - A promise that resolves to the API response. */ pwApi.fetch = async ({ request, page }, urlOrRequest, options) => { return await test_1.test.step(`PW Api Call - FETCH - ${urlOrRequest}`, async () => { const start = Date.now(); const response = await request.fetch(urlOrRequest, options); await __classPrivateFieldGet(_a, _a, "f", _pwApi_displayApiCallInfo).call(_a, { urlOrRequest, options, fromCall: "FETCH" }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes a Playwright GET 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 {APIRequestContext} params.request - The API Playwright request context. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the GET request to. * @param {any} [options] - Optional settings for the request. * @returns {Promise<APIResponse>} - A promise that resolves to the API response. */ pwApi.get = async ({ request, page }, url, options) => { return await test_1.test.step(`PW Api Call - GET - ${url}`, async () => { const start = Date.now(); const response = await request.get(url, options); await __classPrivateFieldGet(_a, _a, "f", _pwApi_displayApiCallInfo).call(_a, { urlOrRequest: url, options, method: 'GET' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes a DELETE 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 {APIRequestContext} params.request - The API Playwright request context. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the GET request to. * @param {any} [options] - Optional settings for the request. * @returns {Promise<APIResponse>} - A promise that resolves to the API response. */ pwApi.delete = async ({ request, page }, url, options) => { return await test_1.test.step(`PW Api Call - DELETE - ${url}`, async () => { const start = Date.now(); const response = await request.delete(url, options); await __classPrivateFieldGet(_a, _a, "f", _pwApi_displayApiCallInfo).call(_a, { urlOrRequest: url, options, method: 'DELETE' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes a 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 {APIRequestContext} params.request - The API Playwright request context. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the GET request to. * @param {any} [options] - Optional settings for the request. * @returns {Promise<APIResponse>} - A promise that resolves to the API response. */ pwApi.head = async ({ request, page }, url, options) => { return await test_1.test.step(`PW Api Call - HEAD - ${url}`, async () => { const start = Date.now(); const response = await request.head(url, options); await __classPrivateFieldGet(_a, _a, "f", _pwApi_displayApiCallInfo).call(_a, { urlOrRequest: url, options, method: 'HEAD' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes a POST 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 {APIRequestContext} params.request - The API Playwright request context. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the GET request to. * @param {any} [options] - Optional settings for the request. * @returns {Promise<APIResponse>} - A promise that resolves to the API response. */ pwApi.post = async ({ request, page }, url, options) => { return await test_1.test.step(`PW Api Call - POST - ${url}`, async () => { const start = Date.now(); const response = await request.post(url, options); await __classPrivateFieldGet(_a, _a, "f", _pwApi_displayApiCallInfo).call(_a, { urlOrRequest: url, options, method: 'POST' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes a PUT 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 {APIRequestContext} params.request - The API Playwright request context. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the GET request to. * @param {any} [options] - Optional settings for the request. * @returns {Promise<APIResponse>} - A promise that resolves to the API response. */ pwApi.put = async ({ request, page }, url, options) => { return await test_1.test.step(`PW Api Call - PUT - ${url}`, async () => { const start = Date.now(); const response = await request.put(url, options); await __classPrivateFieldGet(_a, _a, "f", _pwApi_displayApiCallInfo).call(_a, { urlOrRequest: url, options, method: 'PUT' }, { response, duration: Date.now() - start }, page); return response; }); }; /** * Class function that makes a PATCH 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 {APIRequestContext} params.request - The API Playwright request context. * @param {Page} [params.page] - Optional the Playwright page object. * @param {string} url - The URL to send the GET request to. * @param {any} [options] - Optional settings for the request. * @returns {Promise<APIResponse>} - A promise that resolves to the API response. */ pwApi.patch = async ({ request, page }, url, options) => { return await test_1.test.step(`PW Api Call - PATCH - ${url}`, async () => { const start = Date.now(); const response = await request.patch(url, options); await __classPrivateFieldGet(_a, _a, "f", _pwApi_displayApiCallInfo).call(_a, { urlOrRequest: url, options, method: 'PATCH' }, { response, duration: Date.now() - start }, page); return response; }); }; // PRIVATE METHODS // --------------- /** * Obtains and displays API call information on the Playwright UI and HTML Report. * * @param {PlaywrightRequestInterface} request - The request details. * @param {string | Request} request.urlOrRequest - The URL or request object. * @param {any} request.options - The request options. * @param {string} request.method - The HTTP method used for the request. * @param {string} request.fromCall - The origin of the call. * * @param {PlaywrightResponseInterface} response - The response details. * @param {APIResponse} response.response - The response object. * @param {number} response.duration - The duration of the request in milliseconds. * * @param {Page} [page] - Optional the page object where the API call information will be displayed. * * @returns {Promise<{ requestData: RequestDataInterface, responseData: ResponseDataInterface }>} * An object containing the processed request and response data. */ _pwApi_displayApiCallInfo = { value: async ({ urlOrRequest, options, method, fromCall }, { response, duration }, page) => { var _b; if (process.env.LOG_API_UI !== 'false' || process.env.LOG_API_REPORT === 'true') { // Form the request data object for representation on the UI let requestData = { url: typeof urlOrRequest === 'string' ? urlOrRequest : urlOrRequest.url(), method: (method === null || method === void 0 ? void 0 : method.toUpperCase()) || ((_b = options === null || options === void 0 ? void 0 : options.method) === null || _b === void 0 ? void 0 : _b.toUpperCase()) || 'GET', fromCall, }; if (options) { let _c = structuredClone(options), { method, headers, data, params } = _c, otherOptions = __rest(_c, ["method", "headers", "data", "params"]); if (Object.keys(otherOptions).length === 0) otherOptions = undefined; requestData = Object.assign(Object.assign({}, requestData), { headers, data, params, otherOptions }); } // Form the request data object for representation on the UI const status = response.status(); let body; if ((await response.text()) !== "") { body = await response.json(); } 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, duration }; await (0, displayUi_1.addApiCardToUI)(requestData, responseData, page); return { requestData, responseData }; } else { return null; } } }; exports.default = pwApi;