UNPKG

puppeteer-extra-plugin-session

Version:

A puppeteer plugin to dump and inject session data.

89 lines 3.52 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const puppeteer_extra_1 = __importDefault(require("puppeteer-extra")); const constants_1 = require("../constants/constants"); const plugin_1 = require("../plugin/plugin"); let browser; let page; puppeteer_extra_1.default.use(new plugin_1.SessionPlugin()); beforeAll(async () => { browser = await puppeteer_extra_1.default.launch({ headless: true, executablePath: constants_1.TestBrowserExecutablePath, args: ['--no-sandbox'], }); page = await browser.newPage(); await page.goto('https://httpbin.org/ip'); }); afterAll(async () => { await (browser === null || browser === void 0 ? void 0 : browser.close()); }); afterEach(async () => { // delete the "foo" cookie after each tests const session = await page.target().createCDPSession(); await session.send('Network.deleteCookies', { name: 'foo', domain: 'httpbin.org', }); await session.detach(); }); it('can get cookies', async () => { var _a; await page.evaluate(() => { function setCookie(name, value, days) { let expires; if (days) { const date = new Date(); date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); expires = '; expires=' + date.toString(); } else expires = ''; document.cookie = name + '=' + value + expires + ';'; } setCookie('foo', 'bar'); }); const session = await page.session.dump(); // the cookie exists and was obtained expect(JSON.parse(session.cookie).some((cookie) => cookie.name === 'foo')).toBe(true); // the cookie contains the right value expect((_a = JSON.parse(session.cookie).find((cookie) => cookie.name === 'foo')) === null || _a === void 0 ? void 0 : _a.value).toBe('bar'); }); it('can edit and overwrite cookies', async () => { var _a; // set dummy cookie (foo:bar) await page.evaluate(() => { function setCookie(name, value, days) { let expires; if (days) { const date = new Date(); date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); expires = '; expires=' + date.toString(); } else expires = ''; document.cookie = name + '=' + value + expires + ';'; } setCookie('foo', 'bar'); }); const initialSession = await page.session.dump(); // edit cookies initialSession.cookie = JSON.stringify(JSON.parse(initialSession.cookie).map((cookie) => { // note: beware, here, it's baz, while before, it was ba*r*. cookie.value = 'baz'; return cookie; })); await page.session.restore(initialSession); const restoredSession = await page.session.dump(); // the cookie exists again expect(JSON.parse(restoredSession.cookie).some((cookie) => cookie.name === 'foo')).toBe(true); // the cookie contains the right value expect((_a = JSON.parse(restoredSession.cookie).find((cookie) => cookie.name === 'foo')) === null || _a === void 0 ? void 0 : _a.value).toBe('baz'); }); it.todo('can add a cookie'); it.todo('gets cookies from other domains'); it.todo('overwrites cookies from other domain'); //# sourceMappingURL=cookies.spec.js.map