UNPKG

playwright-fluent

Version:
139 lines (138 loc) 7.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const path_1 = require("path"); const fs_1 = require("fs"); const playwright_fluent_1 = require("../../playwright-fluent"); describe('Playwright Fluent - withStorageState', () => { let p; beforeEach(() => { p = new playwright_fluent_1.PlaywrightFluent(); }); afterEach(async () => { await p.close(); }); test('should re-hydrate cookies and localStorage from a file - chromium', async () => { var _a; // Given const browser = 'chromium'; const url = 'https://reactstrap.github.io'; const storageStateFile = (0, path_1.join)(__dirname, 'storage-state.json'); const storyBookComponentsTree = p .selector('div#storybook-explorer-tree') .find('button') .withText('COMPONENTS'); // When await p .withBrowser(browser) .withCursor() .withOptions({ headless: false }) .withStorageState(storageStateFile) .navigateTo(url) .hover(storyBookComponentsTree); // Then the foo=bar cookie should be re-hydrated const currentStorageState = await p.currentStorageState(); // await p.saveStorageStateTo(storageStateFile); expect(currentStorageState).toBeDefined(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect(currentStorageState.cookies).toBeDefined(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect((_a = currentStorageState.cookies) === null || _a === void 0 ? void 0 : _a.length).toBeGreaterThanOrEqual(1); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const hasGeneratedExtraCookie = currentStorageState.cookies.some((cookie) => cookie.name === 'foo' && cookie.value === 'bar'); expect(hasGeneratedExtraCookie).toBe(true); // And the localStorage foobar should be re-hydrated // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect(currentStorageState.origins).toBeDefined(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const localStorageValues = currentStorageState .origins.filter((origin) => origin.origin === 'https://reactstrap.github.io') .flatMap((origin) => origin.localStorage); const hasGeneratedExtraStorage = localStorageValues.some((localStorage) => localStorage.name === 'foo-localStorage' && localStorage.value === 'bar-localStorage'); expect(localStorageValues.length).toBeGreaterThan(1); expect(hasGeneratedExtraStorage).toBe(true); }); test('should re-hydrate cookies and localStorage from an object - chromium', async () => { var _a; // Given const browser = 'chromium'; const url = 'https://reactstrap.github.io'; const storageStateFile = (0, path_1.join)(__dirname, 'storage-state.json'); const stringifiedStorageState = (0, fs_1.readFileSync)(storageStateFile).toString(); const storageState = JSON.parse(stringifiedStorageState); const storyBookComponentsTree = p .selector('div#storybook-explorer-tree') .find('button') .withText('COMPONENTS'); // When await p .withBrowser(browser) .withCursor() .withOptions({ headless: false }) .withStorageState(storageState) .navigateTo(url) .hover(storyBookComponentsTree); // Then the foo=bar cookie should be re-hydrated const currentStorageState = await p.currentStorageState(); expect(currentStorageState).toBeDefined(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect(currentStorageState.cookies).toBeDefined(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect((_a = currentStorageState.cookies) === null || _a === void 0 ? void 0 : _a.length).toBeGreaterThanOrEqual(1); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const hasGeneratedExtraCookie = currentStorageState.cookies.some((cookie) => cookie.name === 'foo' && cookie.value === 'bar'); expect(hasGeneratedExtraCookie).toBe(true); // And the localStorage foobar should be re-hydrated // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect(currentStorageState.origins).toBeDefined(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const localStorageValues = currentStorageState .origins.filter((origin) => origin.origin === 'https://reactstrap.github.io') .flatMap((origin) => origin.localStorage); const hasGeneratedExtraStorage = localStorageValues.some((localStorage) => localStorage.name === 'foo-localStorage' && localStorage.value === 'bar-localStorage'); expect(localStorageValues.length).toBeGreaterThan(1); expect(hasGeneratedExtraStorage).toBe(true); }); test('should re-hydrate and store again cookies and localStorage from an object - chromium', async () => { var _a; // Given const browser = 'chromium'; const url = 'https://reactstrap.github.io'; const storageStateFile = (0, path_1.join)(__dirname, 'storage-state.json'); const stringifiedStorageState = (0, fs_1.readFileSync)(storageStateFile).toString(); const storageState = JSON.parse(stringifiedStorageState); const storyBookComponentsTree = p .selector('div#storybook-explorer-tree') .find('button') .withText('COMPONENTS'); // When await p .withBrowser(browser) .withCursor() .withOptions({ headless: false }) .withStorageState(storageState) .navigateTo(url) .hover(storyBookComponentsTree); const finalStorageStateFile = (0, path_1.join)(__dirname, 'final-storage-state.json'); await p.saveStorageStateTo(finalStorageStateFile).wait(3000); const finalStorageState = JSON.parse((0, fs_1.readFileSync)(finalStorageStateFile).toString()); // Then the foo=bar cookie should be saved again expect(finalStorageState).toBeDefined(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect(finalStorageState.cookies).toBeDefined(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect((_a = finalStorageState.cookies) === null || _a === void 0 ? void 0 : _a.length).toBeGreaterThanOrEqual(1); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const hasGeneratedExtraCookie = finalStorageState.cookies.some((cookie) => cookie.name === 'foo' && cookie.value === 'bar'); expect(hasGeneratedExtraCookie).toBe(true); // And the localStorage foobar should be re-hydrated // eslint-disable-next-line @typescript-eslint/no-non-null-assertion expect(finalStorageState.origins).toBeDefined(); // eslint-disable-next-line @typescript-eslint/no-non-null-assertion const localStorageValues = finalStorageState .origins.filter((origin) => origin.origin === 'https://reactstrap.github.io') .flatMap((origin) => origin.localStorage); const hasGeneratedExtraStorage = localStorageValues.some((localStorage) => localStorage.name === 'foo-localStorage' && localStorage.value === 'bar-localStorage'); expect(localStorageValues.length).toBeGreaterThan(1); expect(hasGeneratedExtraStorage).toBe(true); }); });