Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to enter text into monaco-editor control using Playwright. #14126

Closed
raiajithkumarr opened this issue May 12, 2022 · 8 comments
Closed

How to enter text into monaco-editor control using Playwright. #14126

raiajithkumarr opened this issue May 12, 2022 · 8 comments

Comments

@raiajithkumarr
Copy link

Hi Team,
please tell me how enter text into .monaco-editor.

I tried below options but no use.
await page.fill('#monaco-editor-selector', 'YOUR_CODE');
or
await page.fill(".view-lines monaco-mouse-cursor-text","tests");

please help here.

@mxschmitt
Copy link
Member

Monaco editor has no native input element which you can automate, but you can issue keyboard events, see the following example:

// @ts-check
const playwright = require('playwright');

(async () => {
  const browser = await playwright.chromium.launch({
    headless: false,
  });
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('https://microsoft.github.io/monaco-editor/playground.html', { waitUntil: 'networkidle' });
  const monacoEditor = page.locator(".monaco-editor").nth(0);
  await monacoEditor.click();
  await page.keyboard.press("Meta+KeyA")
  await page.keyboard.type('This is a line of text\n');
  await page.keyboard.type('This is another line of text\n');
  await page.screenshot({ path: `example.png` });
  await browser.close();
})();

@mofojed
Copy link

mofojed commented Nov 3, 2022

The above method works, but sometimes instead of entering the text I have inputted, the new-line (or Enter key) will select the first autocomplete suggestion instead, changing the text intended to be inputted.

@hashboard-charlie
Copy link

hashboard-charlie commented Sep 20, 2023

We have been bumping into this problem for awhile and felt that all the proposed solutions overtested Monaco. We don't care about how Monaco works, or how it responds to keystrokes. So we just attach a handler directly to the element that Playwright can call. For example:

In the app code:

// attach an imperative method to the element so tests can programmatically update
// the value of the editor without dealing with how Monaco handles the exact keystrokes
monacoElement.__uiTestingSetValue = (text: string) => {
  onChange(text);
 // or however you want to mutate the underlying stored value...
 // you could change the editor's `model` instead
};

In Playwright:

export async function setMonacoEditorText(locator: Locator, text: string) {
  await locator.evaluate(
    (el, passedText) => (el as any).__uiTestingSetValue(passedText),
    text,
  );
}

It isn't "pure" in that we're changing our app's code to suit the test suite... but it beats having flakes, and it allows people to just drop text into Monaco inputs without needing to figure out the precise set of keystrokes and inner containers to send them to.

@mofojed
Copy link

mofojed commented Sep 20, 2023

We ended up using paste to paste text into monaco, but it is pretty obtuse: https://github.com/deephaven/web-client-ui/blob/9d905fca86aa8ba4ff53debd1fd12dcc9132299b/tests/utils.ts#L107

@Verten
Copy link

Verten commented Mar 6, 2024

Monaco editor has no native input element which you can automate, but you can issue keyboard events, see the following example:

// @ts-check
const playwright = require('playwright');

(async () => {
  const browser = await playwright.chromium.launch({
    headless: false,
  });
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('https://microsoft.github.io/monaco-editor/playground.html', { waitUntil: 'networkidle' });
  const monacoEditor = page.locator(".monaco-editor").nth(0);
  await monacoEditor.click();
  await page.keyboard.press("Meta+KeyA")
  await page.keyboard.type('This is a line of text\n');
  await page.keyboard.type('This is another line of text\n');
  await page.screenshot({ path: `example.png` });
  await browser.close();
})();

Thanks, you saved me all day

@StuartAtmosi
Copy link

Monaco editor has no native input element which you can automate, but you can issue keyboard events, see the following example:

// @ts-check
const playwright = require('playwright');

(async () => {
  const browser = await playwright.chromium.launch({
    headless: false,
  });
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('https://microsoft.github.io/monaco-editor/playground.html', { waitUntil: 'networkidle' });
  const monacoEditor = page.locator(".monaco-editor").nth(0);
  await monacoEditor.click();
  await page.keyboard.press("Meta+KeyA")
  await page.keyboard.type('This is a line of text\n');
  await page.keyboard.type('This is another line of text\n');
  await page.screenshot({ path: `example.png` });
  await browser.close();
})();

Where can I find a list of the key press commands please?
I am currently looking for CTRL+V for paste, but would be interested in other commands at a later date! 😊

@naltatis
Copy link

naltatis commented Jun 5, 2024

Struggled a bit to get this code running in GitHub actions just to find out the modifier key has to be adapted depending on operating system. This works on mac and linux:

await page.keyboard.press("ControlOrMeta+KeyA")

@StuartAtmosi
Copy link

I have foud the .FillAsync to be the best and simplest approach for my needs.

await Page.GetByLabel("Editor content;Press Alt+F1").FillAsync(Variables.TextToFillExample);

Typing takes too long.

The other fallback method is CTRL+V

            var monacoEditor = Page.Locator(".monaco-editor").Nth(0);
            await monacoEditor.ClickAsync();
            await Page.EvaluateAsync("text => navigator.clipboard.writeText(text)", TextToPasteExample);
            await Page.Keyboard.PressAsync("Control+V");

sergei-maertens added a commit to open-formulieren/open-forms that referenced this issue Jul 12, 2024
Added a helper to put (JSON) expressions in the editors so that the
user interaction can be simulated. For speed reasons, it uses copy and
paste. Solution is taken from microsoft/playwright#14126
sergei-maertens added a commit to open-formulieren/open-forms that referenced this issue Jul 12, 2024
Added a helper to put (JSON) expressions in the editors so that the
user interaction can be simulated. For speed reasons, it uses copy and
paste. Solution is taken from microsoft/playwright#14126
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants