diff --git a/public/cookies/fetch.html b/public/cookies/fetch.html new file mode 100644 index 0000000..f8ba470 --- /dev/null +++ b/public/cookies/fetch.html @@ -0,0 +1,15 @@ + + + + +
+        
+ + + diff --git a/public/cookies/index.html b/public/cookies/xhr.html similarity index 100% rename from public/cookies/index.html rename to public/cookies/xhr.html diff --git a/puppeteer/cookies-fetch.js b/puppeteer/cookies-fetch.js new file mode 100644 index 0000000..2bb4d95 --- /dev/null +++ b/puppeteer/cookies-fetch.js @@ -0,0 +1,56 @@ +// Copyright 2023-2025 Lightpanda (Selecy SAS) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +'use scrict' + +import puppeteer from 'puppeteer-core'; +import assert from 'assert'; + +const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222'; + +// use browserWSEndpoint to pass the Lightpanda's CDP server address. +const browser = await puppeteer.connect({ + browserWSEndpoint: browserAddress, +}); +// The rest of your script remains the same. +const context = await browser.createBrowserContext(); +const page = await context.newPage(); + +await context.setCookie({name: 'hello', value: 'world', url: "http://127.0.0.1:1234/"}); + +await page.goto('http://127.0.0.1:1234/cookies/set', {waitUntil: 'load'}); +await page.goto('http://127.0.0.1:1234/cookies/fetch.html', {waitUntil: 'load'}); + +const found_cookies = await context.cookies(); +for (const cookie of found_cookies) { + const { name, ...details } = cookie + console.log(`Cookie: ${name} = ${JSON.stringify(details)}`); +} +if (found_cookies.length != 2) { + throw new Error("Wrong number of cookies found"); +} + +// check the output from the srv POV. +const element = await page.$('pre'); +const pre = await page.evaluate(el => el.textContent, element); +const obj = JSON.parse(pre); + +assert.equal(obj[0].Name, "hello"); +assert.equal(obj[0].Value, "world"); +assert.equal(obj[1].Name, "lightpanda"); +assert.equal(obj[1].Value, "browser"); + +await page.close(); +await context.close(); +await browser.disconnect(); + diff --git a/puppeteer/cookies-xhr.js b/puppeteer/cookies-xhr.js index ba3be25..00b9272 100644 --- a/puppeteer/cookies-xhr.js +++ b/puppeteer/cookies-xhr.js @@ -29,7 +29,7 @@ const page = await context.newPage(); await context.setCookie({name: 'hello', value: 'world', url: "http://127.0.0.1:1234/"}); await page.goto('http://127.0.0.1:1234/cookies/set', {waitUntil: 'load'}); -await page.goto('http://127.0.0.1:1234/cookies/', {waitUntil: 'load'}); +await page.goto('http://127.0.0.1:1234/cookies/xhr.html', {waitUntil: 'load'}); const found_cookies = await context.cookies(); for (const cookie of found_cookies) {