Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions public/dynamic_scripts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<body>
<script>
const el = document.createElement('script');
el.src = 'script.js';
document.getElementsByTagName('body')[0].appendChild(el);
</script>
<div id=product></div>
</body>
3 changes: 3 additions & 0 deletions public/dynamic_scripts/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(() => {
document.getElementById('product').innerText = 'Keemun';
})();
10 changes: 10 additions & 0 deletions public/form/get.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<body>
<form id=f action=submit>
<input type=hidden name=h1 value=v1>
<input type=text name=h2 value=v2 disabled>
<input name=h3 value=v3>
<select name="favorite drink"><option>tea</option></select>
</form>
<script>document.getElementById('f').submit()</script>
</body>
10 changes: 10 additions & 0 deletions public/form/post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<body>
<form id=f method=post action=submit enctype="application/x-www-form-urlencoded">
<input type=hidden name=h1 value=v1>
<input type=text name=h2 value=v2 disabled>
<input name=h3 value=v3>
<select name="favorite drink"><option>tea</option></select>
</form>
<script>document.getElementById('f').submit()</script>
</body>
5 changes: 5 additions & 0 deletions public/location_write/index1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE html>
<body>
<div id=page>1</div>
<script>location.assign('index2.html')</script>
</body>
5 changes: 5 additions & 0 deletions public/location_write/index2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE html>
<body>
<div id=page>2</div>
<script>document.location = `http://${location.host}/location_write/index3.html`</script>
</body>
5 changes: 5 additions & 0 deletions public/location_write/index3.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!DOCTYPE html>
<body>
<div id=page>3</div>
<script>top.location = '../location_write/index4.html'</script>
</body>
4 changes: 4 additions & 0 deletions public/location_write/index4.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!DOCTYPE html>
<body>
<div id=page>4</div>
</body>
45 changes: 45 additions & 0 deletions puppeteer/dynamic_scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023-2024 Lightpanda (Selecy SAS)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use scrict'

import puppeteer from 'puppeteer-core';

const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222';
const baseURL = process.env.URL ? process.env.URL : 'http://127.0.0.1:1234'

// use browserWSEndpoint to pass the Lightpanda's CDP server address.
const browser = await puppeteer.connect({
browserWSEndpoint: browserAddress,
});

// The rest of your script remains the same.
const context = await browser.createBrowserContext();
const page = await context.newPage();

await page.goto(baseURL + '/dynamic_scripts/index.html');;

await page.waitForFunction(() => {
const products = document.querySelector('#product');
return products.textContent.length > 0;
}, {timeout: 1000});

const product = await page.evaluate(() => { return document.querySelector('#product').textContent; });
if (product !== 'Keemun') {
console.log(res);
throw new Error("invalid product");
}

await page.close();
await context.close();
await browser.disconnect();
72 changes: 72 additions & 0 deletions puppeteer/form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2023-2024 Lightpanda (Selecy SAS)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use scrict'

import puppeteer from 'puppeteer-core';

const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222';
const baseURL = process.env.URL ? process.env.URL : 'http://127.0.0.1:1234'

// use browserWSEndpoint to pass the Lightpanda's CDP server address.
const browser = await puppeteer.connect({
browserWSEndpoint: browserAddress,
});

// The rest of your script remains the same.
const context = await browser.createBrowserContext();
const page = await context.newPage();

await testForm(page, '/form/get.html', {
method: 'GET',
body: '',
query: 'h1=v1&h3=v3&favorite+drink=tea',
});

await testForm(page, '/form/post.html', {
method: 'POST',
body: 'h1=v1&h3=v3&favorite+drink=tea',
query: '',
});


await context.close();
await browser.disconnect();


async function testForm(page, url, expected) {
await page.goto(baseURL + url);;

await page.waitForFunction(() => {
const p = document.querySelector('#method');
return p.textContent != '';
}, {timeout: 4000});

const method = await page.evaluate(() => { return document.querySelector('#method').textContent; });
if (method !== expected.method) {
console.log(method);
throw new Error("invalid method");
}

const body = await page.evaluate(() => { return document.querySelector('#body').textContent; });
if (body !== expected.body) {
console.log(body);
throw new Error("invalid body");
}

const query = await page.evaluate(() => { return document.querySelector('#query').textContent; });
if (query !== expected.query) {
console.log(query);
throw new Error("invalid query");
}
}
39 changes: 39 additions & 0 deletions puppeteer/location_write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2023-2024 Lightpanda (Selecy SAS)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use scrict'

import puppeteer from 'puppeteer-core';

const browserAddress = process.env.BROWSER_ADDRESS ? process.env.BROWSER_ADDRESS : 'ws://127.0.0.1:9222';
const baseURL = process.env.URL ? process.env.URL : 'http://127.0.0.1:1234'

// use browserWSEndpoint to pass the Lightpanda's CDP server address.
const browser = await puppeteer.connect({
browserWSEndpoint: browserAddress,
});

// The rest of your script remains the same.
const context = await browser.createBrowserContext();
const page = await context.newPage();

await page.goto(baseURL + '/location_write/index1.html');;
await page.waitForFunction(() => {
const p = document.querySelector('#page');
return p.textContent == '4';

}, {timeout: 2000});

await page.close();
await context.close();
await browser.disconnect();
33 changes: 31 additions & 2 deletions runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
{Bin: "node", Args: []string{"puppeteer/links.js"}, Env: []string{"URL=http://127.0.0.1:1234/campfire-commerce/"}},
{Bin: "node", Args: []string{"puppeteer/click.js"}},
{Bin: "node", Args: []string{"puppeteer/wait_for_network.js"}},
{Bin: "node", Args: []string{"puppeteer/dynamic_scripts.js"}},
{Bin: "node", Args: []string{"puppeteer/location_write.js"}},
{Bin: "node", Args: []string{"puppeteer/form.js"}},
{Bin: "node", Args: []string{"playwright/connect.js"}},
{Bin: "node", Args: []string{"playwright/cdp.js"}, Env: []string{"RUNS=2"}},
{Bin: "node", Args: []string{"playwright/click.js"}},
Expand Down Expand Up @@ -161,11 +164,11 @@ func runtest(ctx context.Context, t Test) error {

// run the local http server
func runhttp(ctx context.Context, addr, dir string) error {
handler := http.FileServer(http.Dir(dir))
fs := http.FileServer(http.Dir(dir))

srv := &http.Server{
Addr: addr,
Handler: handler,
Handler: Handler{fs: fs},
BaseContext: func(net.Listener) context.Context {
return ctx
},
Expand Down Expand Up @@ -201,3 +204,29 @@ func env(key, dflt string) string {

return val
}

type Handler struct {
fs http.Handler
}

func (h Handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/form/submit":
defer req.Body.Close()
body, err := io.ReadAll(req.Body)
if err != nil {
panic(err)
}

res.Header().Add("Content-Type", "text/html")
res.Write([]byte("<html><ul><li id=method>"))
res.Write([]byte(req.Method))
res.Write([]byte("<li id=body>"))
res.Write(body)
res.Write([]byte("<li id=query>"))
res.Write([]byte(req.URL.RawQuery))
res.Write([]byte("</ul>"))
default:
h.fs.ServeHTTP(res, req)
}
}