Skip to content

Commit af7badd

Browse files
committed
Puppeteer cookies
1 parent 85399d4 commit af7badd

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ And Lightpanda commit [b846541ff69082f4d283155f0b3651ae0394a240](https://github.
7777
### Execution time
7878

7979
```console
80-
$ hyperfine --warmup 3 --runs 20 --shell=none "google-chrome --user-data-dir=/tmp/bench_chrome --headless=new --dump-dom http://127.0.0.1:124/campfire-commerce/" "./lightpanda --dump http://127.0.0.1:1234/campfire-commerce/"
81-
Benchmark 1: google-chrome --user-data-dir=/tmp/bench_chrome --headless=new --dump-dom http://127.0.0.1:124/campfire-commerce/
80+
$ hyperfine --warmup 3 --runs 20 --shell=none "google-chrome --user-data-dir=/tmp/bench_chrome --headless=new --dump-dom http://127.0.0.1:1234/campfire-commerce/" "./lightpanda --dump http://127.0.0.1:1234/campfire-commerce/"
81+
Benchmark 1: google-chrome --user-data-dir=/tmp/bench_chrome --headless=new --dump-dom http://127.0.0.1:1234/campfire-commerce/
8282
Time (mean ± σ): 598.7 ms ± 8.6 ms [User: 348.0 ms, System: 165.1 ms]
8383
Range (min … max): 586.3 ms … 611.9 ms 20 runs
8484

@@ -88,7 +88,7 @@ Benchmark 2: ./lightpanda --dump http://127.0.0.1:1234/campfire-commerce/
8888

8989
Summary
9090
'./lightpanda --dump http://127.0.0.1:1234/campfire-commerce/' ran
91-
30.50 ± 0.49 times faster than 'google-chrome --user-data-dir=/tmp/bench_chrome --headless=new --dump-dom http://127.0.0.1:124/campfire-commerce/'
91+
30.50 ± 0.49 times faster than 'google-chrome --user-data-dir=/tmp/bench_chrome --headless=new --dump-dom http://127.0.0.1:1234/campfire-commerce/'
9292
```
9393

9494
![aws.m5 hyperfine](./img/aws_m5_hyperfine.png)

puppeteer/cookies.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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();

runner/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
105105
{Bin: "node", Args: []string{"puppeteer/dynamic_scripts.js"}},
106106
{Bin: "node", Args: []string{"puppeteer/location_write.js"}},
107107
{Bin: "node", Args: []string{"puppeteer/form.js"}},
108+
{Bin: "node", Args: []string{"puppeteer/cookies.js"}},
108109
{Bin: "node", Args: []string{"playwright/connect.js"}},
109110
{Bin: "node", Args: []string{"playwright/cdp.js"}, Env: []string{"RUNS=2"}},
110111
{Bin: "node", Args: []string{"playwright/dump.js"}},

0 commit comments

Comments
 (0)