Skip to content

Commit 1702bf7

Browse files
authored
Merge pull request #39 from lightpanda-io/playwright_click
Playwright click
2 parents 75dc520 + 6d3fede commit 1702bf7

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

playwright/click.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 : 'http://127.0.0.1:1234';
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+
const context = await browser.newContext({
40+
baseURL: baseURL,
41+
});
42+
43+
const page = await context.newPage();
44+
await page.goto("/");
45+
46+
await page.getByText('Campfire Commerce').click();
47+
48+
if (page.url() !== 'http://127.0.0.1:1234/campfire-commerce/') {
49+
throw new Error('The new page URL is not as expected.');
50+
}
51+
52+
// ensure product's details is loaded
53+
const price = parseFloat((await page.locator('#product-price').textContent()).substring(1));
54+
if (price !== 244.99) {
55+
console.log(price);
56+
throw new Error("invalid product price");
57+
}
58+
59+
// ensure reviews are loaded
60+
const reviews = await page.locator('#product-reviews > div').evaluateAll((rows) => {
61+
return rows.map(row => ({
62+
name: row.querySelector('h4').textContent,
63+
text: row.querySelector('p').textContent,
64+
}));
65+
});
66+
if (reviews.length !== 3) {
67+
console.log(reviews);
68+
throw new Error("invalid reviews length");
69+
}
70+
71+
await page.close();
72+
await context.close();
73+
74+
// Turn off the browser to clean up after ourselves.
75+
await browser.close();

puppeteer/click.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ assert.strictEqual(page.url(), 'http://127.0.0.1:1234/campfire-commerce/', 'The
4141
// ensure product's details is loaded
4242
const price = parseFloat(await page.evaluate(() => { return document.querySelector('#product-price').textContent.substring(1); }));
4343
if (price != 244.99) {
44-
console.log(res);
44+
console.log(price);
4545
throw new Error("invalid product price");
4646
}
4747

@@ -55,7 +55,7 @@ const reviews = await page.evaluate(() => {
5555
});
5656
});
5757
if (reviews.length != 3) {
58-
console.log(res);
58+
console.log(reviews);
5959
throw new Error("invalid reviews length");
6060
}
6161

runner/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
103103
{Bin: "node", Args: []string{"puppeteer/click.js"}},
104104
{Bin: "node", Args: []string{"playwright/connect.js"}},
105105
{Bin: "node", Args: []string{"playwright/cdp.js"}, Env: []string{"RUNS=2"}},
106+
{Bin: "node", Args: []string{"playwright/click.js"}},
106107
{Bin: "go", Args: []string{"run", "fetch/main.go", "http://127.0.0.1:1234/"}, Dir: "chromedp"},
107108
{Bin: "go", Args: []string{"run", "links/main.go", "http://127.0.0.1:1234/"}, Dir: "chromedp"},
108109
} {

0 commit comments

Comments
 (0)