-
Notifications
You must be signed in to change notification settings - Fork 4k
/
drag-files.ts
173 lines (154 loc) · 4.35 KB
/
drag-files.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* External dependencies
*/
import { readFile } from 'fs/promises';
import { basename } from 'path';
import { getType } from 'mime';
/**
* Internal dependencies
*/
import type { PageUtils } from './index';
import type { Locator } from '@playwright/test';
type FileObject = {
name: string;
mimeType?: string;
buffer: Buffer;
};
type Options = {
position?: { x: number; y: number };
};
/**
* 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,
};
} )
);
// CDP doesn't actually support dragging files, this is only a _good enough_
// dummy data so that it will correctly send the relevant events.
const dragData = {
items: fileObjects.map( ( fileObject ) => ( {
mimeType: fileObject.mimeType ?? 'File',
data: fileObject.base64,
} ) ),
files: fileObjects.map( ( fileObject ) => fileObject.name ),
// Copy = 1, Link = 2, Move = 16.
dragOperationsMask: 1,
};
const cdpSession = await this.context.newCDPSession( this.page );
const position = {
x: 0,
y: 0,
};
return {
/**
* Drag the files over an element (fires `dragenter` and `dragover` events).
*
* @param selectorOrLocator A selector or a locator to search for an element.
* @param options The optional options.
* @param options.position A point to use relative to the top-left corner of element padding box. If not specified, uses some visible point of the element.
*/
dragOver: async (
selectorOrLocator: string | Locator,
options: Options = {}
) => {
const locator =
typeof selectorOrLocator === 'string'
? this.page.locator( selectorOrLocator )
: selectorOrLocator;
const boundingBox = await locator.boundingBox();
if ( ! boundingBox ) {
throw new Error(
'Cannot find the element or the element is not visible on the viewport.'
);
}
position.x =
boundingBox.x +
( options.position?.x ?? boundingBox.width / 2 );
position.y =
boundingBox.y +
( options.position?.y ?? boundingBox.height / 2 );
await cdpSession.send( 'Input.dispatchDragEvent', {
type: 'dragEnter',
...position,
data: dragData,
} );
await cdpSession.send( 'Input.dispatchDragEvent', {
type: 'dragOver',
...position,
data: dragData,
} );
},
/**
* Drop the files at the current position.
*/
drop: async () => {
const topMostElement = await this.page.evaluateHandle(
( { x, y } ) => {
const element = document.elementFromPoint( x, y );
if ( element instanceof HTMLIFrameElement ) {
const offsetBox = element.getBoundingClientRect();
return element.contentDocument!.elementFromPoint(
x - offsetBox.x,
y - offsetBox.y
);
}
return element;
},
position
);
const elementHandle = topMostElement.asElement();
if ( ! elementHandle ) {
throw new Error( 'Element not found.' );
}
const dataTransfer = await elementHandle.evaluateHandle(
async ( _node, _fileObjects ) => {
const dt = new DataTransfer();
const fileInstances = await Promise.all(
_fileObjects.map( async ( fileObject: any ) => {
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
);
await elementHandle.dispatchEvent( 'drop', { dataTransfer } );
await cdpSession.detach();
},
};
}
export { dragFiles };