-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix e2e test and add the pageUtils.dragFiles e2e util
- Loading branch information
1 parent
50c6d11
commit 2a95bfc
Showing
7 changed files
with
181 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
packages/e2e-test-utils-playwright/src/page-utils/drag-files.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { readFile } from 'fs/promises'; | ||
import { basename } from 'path'; | ||
import { getType } from 'mime'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { PageUtils } from './index'; | ||
|
||
type FileObject = { | ||
name: string; | ||
mimeType?: string; | ||
buffer: Buffer; | ||
}; | ||
|
||
/** | ||
* Simulate dragging files from outside the current page. | ||
* | ||
* @param this | ||
* @param files The files to be dragged. | ||
* @return The methods of the drag operation. | ||
*/ | ||
async function dragFiles( | ||
this: PageUtils, | ||
files: string | string[] | FileObject | FileObject[] | ||
) { | ||
const filesList = Array.isArray( files ) ? files : [ files ]; | ||
const fileObjects = await Promise.all( | ||
filesList.map( async ( filePathOrObject ) => { | ||
if ( typeof filePathOrObject !== 'string' ) { | ||
return { | ||
name: filePathOrObject.name, | ||
mimeType: | ||
filePathOrObject.mimeType || | ||
getType( filePathOrObject.name ), | ||
base64: filePathOrObject.buffer.toString( 'base64' ), | ||
}; | ||
} | ||
const base64 = await readFile( filePathOrObject, 'base64' ); | ||
const name = basename( filePathOrObject ); | ||
return { | ||
name, | ||
mimeType: getType( filePathOrObject ), | ||
base64, | ||
}; | ||
} ) | ||
); | ||
|
||
const dataTransfer = await this.page.evaluateHandle( | ||
async ( _fileObjects ) => { | ||
const dt = new DataTransfer(); | ||
const fileInstances = await Promise.all( | ||
_fileObjects.map( async ( fileObject ) => { | ||
const blob = await fetch( | ||
`data:${ fileObject.mimeType };base64,${ fileObject.base64 }` | ||
).then( ( res ) => res.blob() ); | ||
return new File( [ blob ], fileObject.name, { | ||
type: fileObject.mimeType ?? undefined, | ||
} ); | ||
} ) | ||
); | ||
|
||
fileInstances.forEach( ( file ) => { | ||
dt.items.add( file ); | ||
} ); | ||
|
||
return dt; | ||
}, | ||
fileObjects | ||
); | ||
|
||
// Simulate dragging over the document. | ||
await this.page.dispatchEvent( 'html', 'dragenter', { dataTransfer } ); | ||
|
||
const position = { | ||
x: 0, | ||
y: 0, | ||
}; | ||
|
||
const getCurrentTopMostElement = async () => { | ||
const elementFromPosition = await this.page.evaluateHandle( | ||
( point ) => { | ||
const element = document.elementFromPoint( point.x, point.y ); | ||
return element; | ||
}, | ||
position | ||
); | ||
|
||
return elementFromPosition.asElement(); | ||
}; | ||
|
||
return { | ||
/** | ||
* Move the cursor and drag the files to the specified position. | ||
* | ||
* @param x The X coordinate. | ||
* @param y The Y coordinate. | ||
*/ | ||
dragTo: async ( x: number, y: number ) => { | ||
position.x = x; | ||
position.y = y; | ||
|
||
const elementHandle = await getCurrentTopMostElement(); | ||
|
||
if ( ! elementHandle ) { | ||
return; | ||
} | ||
|
||
await elementHandle.dispatchEvent( 'dragenter', { dataTransfer } ); | ||
}, | ||
/** | ||
* Drop the files at the current position. | ||
*/ | ||
drop: async () => { | ||
const elementHandle = await getCurrentTopMostElement(); | ||
|
||
if ( ! elementHandle ) { | ||
throw new Error( | ||
`No element at position (${ position.x }, ${ position.y }) to drop on` | ||
); | ||
} | ||
|
||
await elementHandle.dispatchEvent( 'drop', { dataTransfer } ); | ||
}, | ||
}; | ||
} | ||
|
||
export { dragFiles }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters