Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions public/cookies/fetch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<body>
<script>
(async function () {
const response = await fetch('/cookies/get');
const cookies = await response.json();
document.getElementById('result').innerHTML = JSON.stringify(cookies);
}());
</script>

<pre id="result">
</pre>
</body>
</html>

File renamed without changes.
56 changes: 56 additions & 0 deletions puppeteer/cookies-fetch.js
Original file line number Diff line number Diff line change
@@ -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();

2 changes: 1 addition & 1 deletion puppeteer/cookies-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down