askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 56 minutes ago
History
forms.mjs · 6.2 KB · 72 lines
| 1 | // Form tools: page.forms and page.find list what is on a page with refs, and click/hover/type accept those refs. |
| 2 | export default async function (ctx) { |
| 3 | const { call: T, check, base } = ctx; |
| 4 | const rejects = (p) => p.then(() => null, (e) => e); |
| 5 | |
| 6 | await T("page.goto", { url: base + "/forms" }); |
| 7 | const r = await T("page.forms"); |
| 8 | const by = (name) => r.fields.find((f) => f.name === name); |
| 9 | |
| 10 | check(r.forms.length === 1 && r.forms[0].method === "post" && r.forms[0].action === base + "/forms/submit", "the form is listed with its method and an address without the query string", JSON.stringify(r.forms)); |
| 11 | check(by("first")?.label === "First name" && by("first").required && by("first").value === "Ada" && by("first").autocomplete === "given-name", "a text field has its label (from <label for>), required flag, value and autocomplete", JSON.stringify(by("first"))); |
| 12 | check(by("email")?.label === "Email" && by("email").placeholder === "you@example.com", "a wrapping <label> and a placeholder are reported"); |
| 13 | check(by("phone")?.label === "Phone number", "aria-label is used as the label"); |
| 14 | check(by("country")?.value === "ca" && by("country").options.length === 2 && by("country").options.find((o) => o.value === "ca").selected, "a select reports its options and the chosen one"); |
| 15 | const radios = r.fields.filter((f) => f.name === "via"); |
| 16 | check(radios.length === 2 && radios[0].checked === true && radios[1].checked === false && radios[0].group === "Contact by", "radios report checked state and the fieldset legend", JSON.stringify(radios.map((x) => [x.value, x.checked, x.group]))); |
| 17 | check(by("news")?.checked === false, "a checkbox reports checked"); |
| 18 | check(by("note")?.kind === "textarea" && by("note").value === "hello", "a textarea reports its value"); |
| 19 | |
| 20 | // Things that must never leave the page. |
| 21 | const pw = by("pw"), card = by("cardnumber"), otp = by("code"); |
| 22 | check(pw?.sensitive && pw.reason === "password" && pw.filled === true && !("value" in pw), "a password field is flagged and its value is not returned", JSON.stringify(pw)); |
| 23 | check(card?.sensitive && card.reason === "card" && !("value" in card), "a card-number field (autocomplete cc-number) is flagged and its value is not returned"); |
| 24 | check(otp?.sensitive && otp.reason === "one-time code" && !("value" in otp), "a one-time-code field is flagged and its value is not returned"); |
| 25 | const text = JSON.stringify(r); |
| 26 | check(!/hunter2|4111111111111111|123456|csrf-secret-value/.test(text), "none of the secret values appear anywhere in the reply"); |
| 27 | check(!by("csrf") && !by("display-none") && r.skippedHidden >= 2, "hidden and display:none fields are skipped by default and counted", `skipped ${r.skippedHidden}`); |
| 28 | |
| 29 | const all = await T("page.forms", { includeHidden: true }); |
| 30 | const csrf = all.fields.find((f) => f.name === "csrf"); |
| 31 | check(csrf?.sensitive && csrf.reason === "hidden" && !("value" in csrf), "includeHidden lists a hidden field but still never its value", JSON.stringify(csrf)); |
| 32 | |
| 33 | check(r.buttons.some((b) => b.text === "Create account" && b.type === "submit" && b.form === 0) && r.buttons.some((b) => b.text === "Cancel"), "buttons are listed with their text and the form they belong to"); |
| 34 | check(r.fields.some((f) => f.kind === "editable" && f.label === "Rich note"), "a contenteditable region is listed as an editable field"); |
| 35 | check(typeof r.frames.count === "number", "frames are counted (their fields are not listed)", JSON.stringify(r.frames)); |
| 36 | |
| 37 | // find |
| 38 | const f1 = await T("page.find", { text: "read the news" }); |
| 39 | check(f1.matches.length === 1 && f1.matches[0].role === "link" && f1.matches[0].href === base + "/free", "find by words returns a link with its address", JSON.stringify(f1.matches[0])); |
| 40 | const f2 = await T("page.find", { label: "Email" }); |
| 41 | check(f2.matches.length >= 1 && f2.matches[0].role === "textbox", "find by label returns the field", JSON.stringify(f2.matches[0])); |
| 42 | const f3 = await T("page.find", { role: "button" }); |
| 43 | check(f3.matches.length === 2, "find by role lists the buttons", String(f3.matches.length)); |
| 44 | check((await rejects(T("page.find")))?.code === "bad-params", "find with nothing to look for is a bad-params error"); |
| 45 | const f4 = await T("page.find", { text: "nothing like this" }); |
| 46 | check(f4.total === 0 && f4.matches.length === 0, "find with no match is an empty list, not an error"); |
| 47 | |
| 48 | // refs drive the real input methods |
| 49 | const cancel = r.buttons.find((b) => b.text === "Cancel"); |
| 50 | const clicked = await T("page.click", { ref: cancel.ref }); |
| 51 | const nClicks = (await T("page.eval", { js: "window.clicks" })).value; |
| 52 | check(nClicks === 1, "page.click accepts a ref and really clicks it", JSON.stringify({ clicked, nClicks, cancel })); |
| 53 | await T("page.type", { ref: by("phone").ref, text: "555-0100" }); |
| 54 | check((await T("page.eval", { js: "document.querySelector('[name=phone]').value" })).value === "555-0100", "page.type accepts a ref and types into it"); |
| 55 | await T("page.type", { ref: by("first").ref, text: "Grace" }); |
| 56 | check((await T("page.eval", { js: "document.querySelector('[name=first]').value" })).value === "Grace", "typing replaces the old value by default"); |
| 57 | check((await T("page.hover", { ref: f1.matches[0].ref })).hovered.x > 0, "page.hover accepts a ref"); |
| 58 | |
| 59 | // refs stay the same for the same element, and a ref to something that is gone is an error, never a wrong click |
| 60 | const again = await T("page.forms"); |
| 61 | check(again.fields.find((f) => f.name === "first").ref === by("first").ref, "the same element keeps the same ref between calls"); |
| 62 | await T("page.eval", { js: "document.querySelector('[name=phone]').remove()" }); |
| 63 | const gone = await rejects(T("page.click", { ref: by("phone").ref })); |
| 64 | check(gone?.code === "no-element" && /not on the page/.test(gone.message), "a ref to a removed element is 'no-element'", gone?.message); |
| 65 | check((await rejects(T("page.click", { ref: "e99999" })))?.code === "no-element", "a ref that never existed is 'no-element'"); |
| 66 | const sel = await rejects(T("page.click", { selector: "#nope" })); |
| 67 | check(sel?.code === "no-element", "a selector that matches nothing is still 'no-element'"); |
| 68 | |
| 69 | // After a navigation the old refs do not resolve (new document, new registry). |
| 70 | await T("page.goto", { url: base + "/free" }); |
| 71 | check((await rejects(T("page.click", { ref: cancel.ref })))?.code === "no-element", "refs do not survive navigation"); |
| 72 | } |