11import { Locator , expect , Page } from '@playwright/test' ;
2+ import os from 'node:os' ;
23import shortid from 'shortid' ;
34
45export enum TableTypes {
@@ -99,18 +100,44 @@ export async function typeInMonaco(
99100}
100101
101102/**
102- * Pastes text into a monaco input
103+ * Pastes text into a monaco input. The input will have focus after pasting.
103104 * @param locator Locator to use for monaco editor
104105 * @param text Text to be pasted
105106 */
106107export async function pasteInMonaco (
107108 locator : Locator ,
108109 text : string
109110) : Promise < void > {
111+ const page = locator . page ( ) ;
112+ const isMac = os . platform ( ) === 'darwin' ;
113+ const modifier = isMac ? 'Meta' : 'Control' ;
114+
115+ // Create a hidden textarea with the contents to paste
116+ const inputId = await page . evaluate ( async evalText => {
117+ const tempInput = document . createElement ( 'textarea' ) ;
118+ tempInput . id = 'super-secret-temp-input-id' ;
119+ tempInput . value = evalText ;
120+ tempInput . style . width = '0' ;
121+ tempInput . style . height = '0' ;
122+ document . body . appendChild ( tempInput ) ;
123+ tempInput . select ( ) ;
124+ return tempInput . id ;
125+ } , text ) ;
126+
127+ // Copy the contents of the textarea which was selected above
128+ await page . keyboard . press ( `${ modifier } +C` ) ;
129+
130+ // Remove the textarea
131+ await page . evaluate ( id => {
132+ document . getElementById ( id ) ?. remove ( ) ;
133+ } , inputId ) ;
134+
135+ // Focus monaco
136+ await locator . click ( ) ;
137+
110138 const browserName = locator . page ( ) . context ( ) . browser ( ) ?. browserType ( ) . name ( ) ;
111- if ( browserName === 'firefox' ) {
112- await typeInMonaco ( locator , text ) ;
113- } else {
139+ if ( browserName === 'webkit' ) {
140+ // Webkit doesn't seem to paste w/ the keyboard shortcut in headless mode
114141 await locator . locator ( 'textarea' ) . evaluate ( async ( element , evalText ) => {
115142 const clipboardData = new DataTransfer ( ) ;
116143 clipboardData . setData ( 'text/plain' , evalText ) ;
@@ -119,6 +146,13 @@ export async function pasteInMonaco(
119146 } ) ;
120147 element . dispatchEvent ( clipboardEvent ) ;
121148 } , text ) ;
149+ } else {
150+ await page . keyboard . press ( `${ modifier } +V` ) ;
151+ }
152+
153+ if ( text . length > 0 ) {
154+ // Sanity check the paste happened
155+ await expect ( locator . locator ( 'textarea' ) ) . not . toBeEmpty ( ) ;
122156 }
123157}
124158
0 commit comments