Skip to content

Commit 2c0055c

Browse files
committed
Add case for form submission (via the JS submit() function call)
This tests the code in lightpanda-io/browser#723
1 parent c1a4b8e commit 2c0055c

File tree

4 files changed

+121
-2
lines changed

4 files changed

+121
-2
lines changed

public/form/get.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<form id=f action=submit>
4+
<input type=hidden name=h1 value=v1>
5+
<input type=text name=h2 value=v2 disabled>
6+
<input name=h3 value=v3>
7+
<select name="favorite drink"><option>tea</option></select>
8+
</form>
9+
<script>document.getElementById('f').submit()</script>
10+
</body>

public/form/post.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<body>
3+
<form id=f method=post action=submit enctype="application/x-www-form-urlencoded">
4+
<input type=hidden name=h1 value=v1>
5+
<input type=text name=h2 value=v2 disabled>
6+
<input name=h3 value=v3>
7+
<select name="favorite drink"><option>tea</option></select>
8+
</form>
9+
<script>document.getElementById('f').submit()</script>
10+
</body>

puppeteer/form.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 testForm(page, '/form/get.html', {
31+
method: 'GET',
32+
body: '',
33+
query: 'h1=v1&h3=v3&favorite+drink=tea',
34+
});
35+
36+
await testForm(page, '/form/post.html', {
37+
method: 'POST',
38+
body: 'h1=v1&h3=v3&favorite+drink=tea',
39+
query: '',
40+
});
41+
42+
43+
await context.close();
44+
await browser.disconnect();
45+
46+
47+
async function testForm(page, url, expected) {
48+
await page.goto(baseURL + url);;
49+
50+
await page.waitForFunction(() => {
51+
const p = document.querySelector('#method');
52+
return p.textContent != '';
53+
}, {timeout: 4000});
54+
55+
const method = await page.evaluate(() => { return document.querySelector('#method').textContent; });
56+
if (method !== expected.method) {
57+
console.log(method);
58+
throw new Error("invalid method");
59+
}
60+
61+
const body = await page.evaluate(() => { return document.querySelector('#body').textContent; });
62+
if (body !== expected.body) {
63+
console.log(body);
64+
throw new Error("invalid body");
65+
}
66+
67+
const query = await page.evaluate(() => { return document.querySelector('#query').textContent; });
68+
if (query !== expected.query) {
69+
console.log(query);
70+
throw new Error("invalid query");
71+
}
72+
}

runner/main.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func run(ctx context.Context, args []string, stdout, stderr io.Writer) error {
104104
{Bin: "node", Args: []string{"puppeteer/wait_for_network.js"}},
105105
{Bin: "node", Args: []string{"puppeteer/dynamic_scripts.js"}},
106106
{Bin: "node", Args: []string{"puppeteer/location_write.js"}},
107+
{Bin: "node", Args: []string{"puppeteer/form.js"}},
107108
{Bin: "node", Args: []string{"playwright/connect.js"}},
108109
{Bin: "node", Args: []string{"playwright/cdp.js"}, Env: []string{"RUNS=2"}},
109110
{Bin: "node", Args: []string{"playwright/click.js"}},
@@ -163,11 +164,11 @@ func runtest(ctx context.Context, t Test) error {
163164

164165
// run the local http server
165166
func runhttp(ctx context.Context, addr, dir string) error {
166-
handler := http.FileServer(http.Dir(dir))
167+
fs := http.FileServer(http.Dir(dir))
167168

168169
srv := &http.Server{
169170
Addr: addr,
170-
Handler: handler,
171+
Handler: Handler{fs: fs},
171172
BaseContext: func(net.Listener) context.Context {
172173
return ctx
173174
},
@@ -203,3 +204,29 @@ func env(key, dflt string) string {
203204

204205
return val
205206
}
207+
208+
type Handler struct {
209+
fs http.Handler
210+
}
211+
212+
func (h Handler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
213+
switch req.URL.Path {
214+
case "/form/submit":
215+
defer req.Body.Close()
216+
body, err := io.ReadAll(req.Body)
217+
if err != nil {
218+
panic(err)
219+
}
220+
221+
res.Header().Add("Content-Type", "text/html")
222+
res.Write([]byte("<html><ul><li id=method>"))
223+
res.Write([]byte(req.Method))
224+
res.Write([]byte("<li id=body>"))
225+
res.Write(body)
226+
res.Write([]byte("<li id=query>"))
227+
res.Write([]byte(req.URL.RawQuery))
228+
res.Write([]byte("</ul>"))
229+
default:
230+
h.fs.ServeHTTP(res, req)
231+
}
232+
}

0 commit comments

Comments
 (0)