UNPKG

playwright-fluent

Version:
97 lines (96 loc) 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const path = tslib_1.__importStar(require("path")); const simple_fake_server_1 = require("simple-fake-server"); const SUT = tslib_1.__importStar(require("../../playwright-fluent")); const utils_1 = require("../../../utils"); describe('Playwright Fluent - recordFailedRequests(url)', () => { let p; let fakeServer = undefined; beforeAll(() => { fakeServer = new simple_fake_server_1.FakeServer(1244); fakeServer.start(); //The FakeServer now listens on http://localhost:1244 }); afterAll(() => { if (fakeServer) { fakeServer.stop(); } }); beforeEach(() => { p = new SUT.PlaywrightFluent(); }); afterEach(async () => { await p.close(); }); test('should record failed requests 500', async () => { // Given const url = `file:${path.join(__dirname, 'record-failed-requests-500.test.html')}`; fakeServer && // prettier-ignore fakeServer.http .get() .to('/500') .willFail(500); // When await p .withBrowser('chromium') .withOptions({ headless: true }) .withCursor() .recordFailedRequests() .navigateTo(url); // Then await p.waitForStabilityOf(async () => p.getFailedRequests().length, { stabilityInMilliseconds: 2000, }); const requests = p.getFailedRequests(); expect(Array.isArray(requests)).toBe(true); expect(requests.length).toBe(1); const stringifiedSentRequest = await (0, utils_1.stringifyRequest)(requests[0]); const failedRequest = JSON.parse(stringifiedSentRequest); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect(failedRequest.response.status).toBe(500); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect(failedRequest.response.statusText).toBe('Internal Server Error'); }); test('should accumulate failed requests', async () => { // Given const url500 = `file:${path.join(__dirname, 'record-failed-requests-500.test.html')}`; const url503 = `file:${path.join(__dirname, 'record-failed-requests-503.test.html')}`; const url400 = `file:${path.join(__dirname, 'record-failed-requests-400.test.html')}`; fakeServer && fakeServer.http.get().to('/500').willFail(500); fakeServer && fakeServer.http.get().to('/503').willFail(503); fakeServer && fakeServer.http.get().to('/400').willFail(400); // When await p .withBrowser('chromium') .withOptions({ headless: true }) .withCursor() .recordFailedRequests() .navigateTo(url500) .wait(2000) .navigateTo(url503) .wait(2000) .navigateTo(url400); // Then await p.waitForStabilityOf(async () => p.getFailedRequests().length, { stabilityInMilliseconds: 2000, }); const requests = p.getFailedRequests(); expect(Array.isArray(requests)).toBe(true); expect(requests.length).toBe(3); const stringifiedSentRequest = await (0, utils_1.stringifyRequest)(requests[0]); const failedRequest = JSON.parse(stringifiedSentRequest); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect(failedRequest.response.status).toBe(500); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect(failedRequest.response.statusText).toBe('Internal Server Error'); // When I clear all failed request p.clearFailedRequests(); // Then const remainingRequests = p.getFailedRequests(); expect(Array.isArray(remainingRequests)).toBe(true); expect(remainingRequests.length).toBe(0); }); });