|
| 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 | +} |
0 commit comments