Skip to content

Commit 9fce224

Browse files
committed
add location write and dynamic script cases
1 parent 0b24a3c commit 9fce224

File tree

9 files changed

+117
-0
lines changed

9 files changed

+117
-0
lines changed

public/dynamic_scripts/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<script>
4+
const el = document.createElement('script');
5+
el.src = 'script.js';
6+
document.getElementsByTagName('body')[0].appendChild(el);
7+
</script>
8+
<div id=product></div>
9+
</body>

public/dynamic_scripts/script.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(() => {
2+
document.getElementById('product').innerText = 'Keemun';
3+
})();

public/location_write/index1.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<div id=page>1</div>
4+
<script>location.assign('index2.html')</script>
5+
</body>

public/location_write/index2.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<div id=page>2</div>
4+
<script>document.location = `http://${location.host}/location_write/index3.html`</script>
5+
</body>

public/location_write/index3.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<div id=page>3</div>
4+
<script>top.location = '../location_write/index4.html'</script>
5+
</body>

public/location_write/index4.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<div id=page>4</div>
4+
</body>

puppeteer/dynamic_scripts.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
'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 baseURL = process.env.URL ? process.env.URL : 'http://127.0.0.1:1234'
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+
await page.goto(baseURL + '/dynamic_scripts/index.html');;
31+
32+
await page.waitForFunction(() => {
33+
const products = document.querySelector('#product');
34+
return products.textContent.length > 0;
35+
});
36+
37+
const product = await page.evaluate(() => { return document.querySelector('#product').textContent; });
38+
if (product !== 'Keemun') {
39+
console.log(res);
40+
throw new Error("invalid product");
41+
}
42+
43+
await page.close();
44+
await context.close();
45+
await browser.disconnect();

puppeteer/location_write.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
'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 baseURL = process.env.URL ? process.env.URL : 'http://127.0.0.1:1234'
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+
await page.goto(baseURL + '/location_write/index1.html');;
31+
await page.waitForFunction(() => {
32+
const p = document.querySelector('#page');
33+
return p.textContent == '4';
34+
35+
}, {timeout: 2000});
36+
37+
await page.close();
38+
await context.close();
39+
await browser.disconnect();

runner/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
102102
{Bin: "node", Args: []string{"puppeteer/links.js"}, Env: []string{"URL=http://127.0.0.1:1234/campfire-commerce/"}},
103103
{Bin: "node", Args: []string{"puppeteer/click.js"}},
104104
{Bin: "node", Args: []string{"puppeteer/wait_for_network.js"}},
105+
{Bin: "node", Args: []string{"puppeteer/dynamic_scripts.js"}},
106+
{Bin: "node", Args: []string{"puppeteer/location_write.js"}},
105107
{Bin: "node", Args: []string{"playwright/connect.js"}},
106108
{Bin: "node", Args: []string{"playwright/cdp.js"}, Env: []string{"RUNS=2"}},
107109
{Bin: "node", Args: []string{"playwright/click.js"}},

0 commit comments

Comments
 (0)