Skip to content

Commit 1172f9c

Browse files
committed
Playwright links.js
1 parent 0fb82a2 commit 1172f9c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

playwright/links.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2023-2024 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+
15+
// Import the Chromium browser into our scraper.
16+
import { chromium } from 'playwright';
17+
18+
// browserAddress
19+
const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222';
20+
21+
// web serveur url
22+
const baseURL = process.env.BASE_URL ? process.env.BASE_URL : 'https://en.wikipedia.org/wiki/Web_browser';
23+
24+
// measure general time.
25+
const gstart = process.hrtime.bigint();
26+
// store all run durations
27+
let metrics = [];
28+
29+
// Connect to an existing browser
30+
console.log("Connection to browser on " + browserAddress);
31+
const browser = await chromium.connectOverCDP({
32+
endpointURL: browserAddress,
33+
logger: {
34+
isEnabled: (name, severity) => true,
35+
log: (name, severity, message, args) => console.log(`${name} ${message}`)
36+
}
37+
});
38+
39+
40+
const context = await browser.newContext({
41+
baseURL: baseURL,
42+
});
43+
44+
const page = await context.newPage();
45+
await page.goto(baseURL);
46+
47+
const links = await page.getByRole('link').evaluateAll(links =>
48+
links.map(link => link.getAttribute('href'))
49+
);
50+
51+
console.log(links);
52+
53+
await page.close();
54+
await context.close();
55+
56+
// Turn off the browser to clean up after ourselves.
57+
await browser.close();

runner/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
108108
{Bin: "node", Args: []string{"playwright/connect.js"}},
109109
{Bin: "node", Args: []string{"playwright/cdp.js"}, Env: []string{"RUNS=2"}},
110110
{Bin: "node", Args: []string{"playwright/dump.js"}},
111+
{Bin: "node", Args: []string{"playwright/links.js"}, Env: []string{"BASE_URL=http://127.0.0.1:1234/campfire-commerce/"}},
111112
{Bin: "node", Args: []string{"playwright/click.js"}},
112113
{Bin: "go", Args: []string{"run", "fetch/main.go", "http://127.0.0.1:1234/"}, Dir: "chromedp"},
113114
{Bin: "go", Args: []string{"run", "links/main.go", "http://127.0.0.1:1234/"}, Dir: "chromedp"},

0 commit comments

Comments
 (0)