{"record":{"id":"ed7f2eed07b67e59","repo":"grafana/k6","slug":"inserting-text-w","errorCode":null,"errorMessage":"inserting text: %w","messagePattern":"inserting text: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/keyboard.go","lineNumber":83,"sourceCode":"\t}\n\treturn nil\n}\n\n// Press sends a key press message to a session target.\n// It delays the action if `Delay` option is specified.\n// A press message consists of successive key down and up messages.\nfunc (k *Keyboard) Press(key string, kbdOpts KeyboardOptions) error {\n\tif err := k.comboPress(key, kbdOpts); err != nil {\n\t\treturn fmt.Errorf(\"pressing key: %w\", err)\n\t}\n\n\treturn nil\n}\n\n// InsertText inserts a text without dispatching key events.\nfunc (k *Keyboard) InsertText(text string) error {\n\tif err := k.insertText(text); err != nil {\n\t\treturn fmt.Errorf(\"inserting text: %w\", err)\n\t}\n\treturn nil\n}\n\n// Type sends a press message to a session target for each character in text.\n// It delays the action if `Delay` option is specified.\n//\n// It sends an insertText message if a character is not among\n// valid characters in the keyboard's layout.\nfunc (k *Keyboard) Type(text string, kbdOpts KeyboardOptions) error {\n\tif err := k.typ(text, kbdOpts); err != nil {\n\t\treturn fmt.Errorf(\"typing text: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc (k *Keyboard) down(key string) error {\n\tkey = k.platformSpecificResolution(key)","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/keyboard.go#L65-L101","documentation":"Wrapper returned by Keyboard.InsertText when the private insertText() fails, i.e. the CDP Input.insertText call is rejected by the browser. insertText inserts text as IME-style input without individual key events, and fails at the transport level when the session or target is gone.","triggerScenarios":"insertText('héllo') after page.close() or browser.close(); target crashed (renderer gone) before the call; CDP session detached while the input event is in flight.","commonSituations":"Using insertText for fast form filling after an earlier action already closed or crashed the page; races between script shutdown and pending input.","solutions":["Confirm the page is still open (page.isClosed() === false) before calling","If the renderer crashed, re-launch/re-navigate and retry the fill","Use type() when you also need key events fired (event handlers watching keydown)","Wrap in try/catch and fail the iteration explicitly rather than continuing with partial input"],"exampleFix":"// before\nawait page.close();\nawait page.keyboard.insertText('done');\n\n// after\nif (!page.isClosed()) {\n  await page.keyboard.insertText('done');\n}","handlingStrategy":"try-catch","validationCode":"if (page.isClosed()) throw new Error('page closed before insertText');","typeGuard":null,"tryCatchPattern":"try {\n  await page.keyboard.insertText(text);\n} catch (e) {\n  if (/inserting text/.test(e.message) && !page.isClosed()) {\n    await page.keyboard.insertText(text); // transient CDP failure: one retry\n  } else throw e;\n}","preventionTips":["Check page liveness before insertText","Complete text insertion before closing pages or ending the iteration","Prefer insertText over long type() calls when key events are not needed"],"tags":["browser","keyboard","cdp","input"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}