|
| 1 | +// Copyright 2023-2025 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +'use scrict' |
| 15 | + |
| 16 | +import puppeteer from 'puppeteer-core'; |
| 17 | + |
| 18 | +const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222'; |
| 19 | +const url = process.env.URL ? process.env.URL : 'http://127.0.0.1:1234/campfire-commerce/'; |
| 20 | + |
| 21 | +// use browserWSEndpoint to pass the Lightpanda's CDP server address. |
| 22 | +const browser = await puppeteer.connect({ |
| 23 | + browserWSEndpoint: browserAddress, |
| 24 | +}); |
| 25 | + |
| 26 | +// The rest of your script remains the same. |
| 27 | +const context = await browser.createBrowserContext(); |
| 28 | +const page = await context.newPage(); |
| 29 | + |
| 30 | +const relevant_cookie = {name: 'left', value: 'right', url: "http://127.0.0.1:1234/"}; |
| 31 | +const irrelevant_cookie = {name: 'uo', value: 'down', url: "https://lightpanda.io/"}; |
| 32 | + |
| 33 | +await page.setCookie(relevant_cookie, irrelevant_cookie); |
| 34 | + |
| 35 | +await page.goto(url, {waitUntil: 'load'}); |
| 36 | + |
| 37 | +const found_cookies = await page.cookies(); |
| 38 | +for (const cookie of found_cookies) { |
| 39 | + const { name, ...details } = cookie |
| 40 | + console.log(`Cookie: ${name} = ${JSON.stringify(details)}`); |
| 41 | +} |
| 42 | + |
| 43 | +if (found_cookies.length != 1) { |
| 44 | + throw new Error("Wrong number of cookies found"); |
| 45 | +} |
| 46 | +if (found_cookies[0].name !== relevant_cookie.name || found_cookies[0].value !== relevant_cookie.value) { |
| 47 | + throw new Error("Cookie does not match the expected values"); |
| 48 | +} |
| 49 | + |
| 50 | +await page.close(); |
| 51 | +await context.close(); |
| 52 | +await browser.disconnect(); |
0 commit comments