import * as SUT from '../index' ; import { showMousePosition } from '../../../dom-actions' ; import { defaultPasteTextOptions } from '../paste-text' ; import { sleep } from '../../../../utils' ; import { Browser , chromium } from 'playwright' ; import * as path from 'path' ; describe ( 'paste text' , ( ) : void => { let browser = undefined ; beforeEach ( ( ) : void => { } ) ; afterEach ( async ( ) : Promise < void > => { if ( browser ) { await browser . close ( ) ; } } ) ; test ( 'should paste text in a content-editable element that do not have paste event handler - chromium' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; await showMousePosition ( page ) ; const url = `file:${ path . join ( __dirname , 'paste-text.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const selector = '#target' ; await page . click ( selector ) ; const handle = await page . $ ( selector ) ; await SUT . pasteText ( 'foobar' , page , { ... defaultPasteTextOptions , handlePasteEvent : true , } ) ; expect ( await handle ! . evaluate ( ( node ) => node . innerHTML ) ) . toBe ( 'foobar' ) ; } ) ; test ( 'should paste text in an input element that do not have paste event handler - chromium' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; await showMousePosition ( page ) ; const url = `file:${ path . join ( __dirname , 'paste-text.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const selector = '#targetInput' ; await page . click ( selector ) ; const handle = await page . $ ( selector ) ; await SUT . pasteText ( 'foobar' , page , { ... defaultPasteTextOptions , handlePasteEvent : true , } ) ; expect ( await handle ! . evaluate ( ( node ) => ( node as HTMLInputElement ) . value ) ) . toBe ( 'foobar' ) ; } ) ; test ( 'should paste text in a content-editable element that have paste event handler - chromium' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; await showMousePosition ( page ) ; const url = `file:${ path . join ( __dirname , 'paste-text.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const selector = '#emptyInput' ; await page . click ( selector ) ; await SUT . pasteText ( 'foobar' , page , defaultPasteTextOptions ) ; const handle = await page . $ ( selector ) ; expect ( await handle ! . evaluate ( ( node ) => ( node as HTMLInputElement ) . value ) ) . toBe ( 'FOOBAR' ) ; } ) ; test ( 'should paste text without clearing existing content in an input element that do not have paste event handler - chromium' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; await showMousePosition ( page ) ; const url = `file:${ path . join ( __dirname , 'paste-text.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const selector = '#targetInput' ; await page . click ( selector ) ; const handle = await page . $ ( selector ) ; await SUT . pasteText ( ' here' , page , { ... defaultPasteTextOptions , handlePasteEvent : true , clearExistingContent : false , } ) ; expect ( await handle ! . evaluate ( ( node ) => ( node as HTMLInputElement ) . value ) ) . toBe ( 'paste your text here' ) ; } ) ; test ( 'should paste text without clearing existing content in a content-editable element that do not have paste event handler - chromium' , async ( ) : Promise < void > => { browser = await chromium . launch ( { headless : true } ) ; const browserContext = await browser . newContext ( { viewport : null } ) ; const page = await browserContext . newPage ( ) ; await showMousePosition ( page ) ; const url = `file:${ path . join ( __dirname , 'paste-text.test.html' ) }` ; await page . goto ( url ) ; await sleep ( 1000 ) ; const selector = '#target' ; await page . click ( selector ) ; const handle = await page . $ ( selector ) ; await SUT . pasteText ( ' here' , page , { ... defaultPasteTextOptions , handlePasteEvent : true , clearExistingContent : false , } ) ; expect ( await handle ! . evaluate ( ( node ) => node . innerHTML ) ) . toBe ( 'paste your text here' ) ; } ) ; } ) ;