-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathscreenshot.test.ts
More file actions
263 lines (242 loc) · 8.31 KB
/
screenshot.test.ts
File metadata and controls
263 lines (242 loc) · 8.31 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import {rm, stat, mkdir, chmod, writeFile} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import {join} from 'node:path';
import {describe, it} from 'node:test';
import {screenshot} from '../../src/tools/screenshot.js';
import {screenshots} from '../snapshot.js';
import {html, withBrowser} from '../utils.js';
describe('screenshot', () => {
describe('browser_take_screenshot', () => {
it('with default options', async () => {
await withBrowser(async (response, context) => {
const fixture = screenshots.basic;
const page = context.getSelectedPage();
await page.setContent(fixture.html);
await screenshot.handler({params: {format: 'png'}}, response, context);
assert.equal(response.images.length, 1);
assert.equal(response.images[0].mimeType, 'image/png');
assert.equal(
response.responseLines.at(0),
"Took a screenshot of the current page's viewport.",
);
});
});
it('ignores quality', async () => {
await withBrowser(async (response, context) => {
const fixture = screenshots.basic;
const page = context.getSelectedPage();
await page.setContent(fixture.html);
await screenshot.handler(
{params: {format: 'png', quality: 0}},
response,
context,
);
assert.equal(response.images.length, 1);
assert.equal(response.images[0].mimeType, 'image/png');
assert.equal(
response.responseLines.at(0),
"Took a screenshot of the current page's viewport.",
);
});
});
it('with jpeg', async () => {
await withBrowser(async (response, context) => {
await screenshot.handler({params: {format: 'jpeg'}}, response, context);
assert.equal(response.images.length, 1);
assert.equal(response.images[0].mimeType, 'image/jpeg');
assert.equal(
response.responseLines.at(0),
"Took a screenshot of the current page's viewport.",
);
});
});
it('with webp', async () => {
await withBrowser(async (response, context) => {
await screenshot.handler({params: {format: 'webp'}}, response, context);
assert.equal(response.images.length, 1);
assert.equal(response.images[0].mimeType, 'image/webp');
assert.equal(
response.responseLines.at(0),
"Took a screenshot of the current page's viewport.",
);
});
});
it('with full page', async () => {
await withBrowser(async (response, context) => {
const fixture = screenshots.viewportOverflow;
const page = context.getSelectedPage();
await page.setContent(fixture.html);
await screenshot.handler(
{params: {format: 'png', fullPage: true}},
response,
context,
);
assert.equal(response.images.length, 1);
assert.equal(response.images[0].mimeType, 'image/png');
assert.equal(
response.responseLines.at(0),
'Took a screenshot of the full current page.',
);
});
});
it('with full page resulting in a large screenshot', async () => {
await withBrowser(async (response, context) => {
const page = context.getSelectedPage();
await page.setContent(
html`${`<div style="color:blue;">test</div>`.repeat(6500)}
<div
id="red"
style="color:blue;"
>test</div
> `,
);
await page.evaluate(() => {
const el = document.querySelector('#red');
return el?.scrollIntoViewIfNeeded();
});
await screenshot.handler(
{params: {format: 'png', fullPage: true}},
response,
context,
);
assert.equal(response.images.length, 0);
assert.equal(
response.responseLines.at(0),
'Took a screenshot of the full current page.',
);
assert.ok(
response.responseLines.at(1)?.match(/Saved screenshot to.*\.png/),
);
});
});
it('with element uid', async () => {
await withBrowser(async (response, context) => {
const fixture = screenshots.button;
const page = context.getSelectedPage();
await page.setContent(fixture.html);
await context.createTextSnapshot();
await screenshot.handler(
{
params: {
format: 'png',
uid: '1_1',
},
},
response,
context,
);
assert.equal(response.images.length, 1);
assert.equal(response.images[0].mimeType, 'image/png');
assert.equal(
response.responseLines.at(0),
'Took a screenshot of node with uid "1_1".',
);
});
});
it('with filePath', async () => {
await withBrowser(async (response, context) => {
const filePath = join(tmpdir(), 'test-screenshot.png');
try {
const fixture = screenshots.basic;
const page = context.getSelectedPage();
await page.setContent(fixture.html);
await screenshot.handler(
{params: {format: 'png', filePath}},
response,
context,
);
assert.equal(response.images.length, 0);
assert.equal(
response.responseLines.at(0),
"Took a screenshot of the current page's viewport.",
);
assert.equal(
response.responseLines.at(1),
`Saved screenshot to ${filePath}.`,
);
const stats = await stat(filePath);
assert.ok(stats.isFile());
assert.ok(stats.size > 0);
} finally {
await rm(filePath, {force: true});
}
});
});
it('with unwritable filePath', async () => {
if (process.platform === 'win32') {
const filePath = join(
tmpdir(),
'readonly-file-for-screenshot-test.png',
);
// Create the file and make it read-only.
await writeFile(filePath, '');
await chmod(filePath, 0o400);
try {
await withBrowser(async (response, context) => {
const fixture = screenshots.basic;
const page = context.getSelectedPage();
await page.setContent(fixture.html);
await assert.rejects(
screenshot.handler(
{params: {format: 'png', filePath}},
response,
context,
),
);
});
} finally {
// Make the file writable again so it can be deleted.
await chmod(filePath, 0o600);
await rm(filePath, {force: true});
}
} else {
const dir = join(tmpdir(), 'readonly-dir-for-screenshot-test');
await mkdir(dir, {recursive: true});
await chmod(dir, 0o500);
const filePath = join(dir, 'test-screenshot.png');
try {
await withBrowser(async (response, context) => {
const fixture = screenshots.basic;
const page = context.getSelectedPage();
await page.setContent(fixture.html);
await assert.rejects(
screenshot.handler(
{params: {format: 'png', filePath}},
response,
context,
),
);
});
} finally {
await chmod(dir, 0o700);
await rm(dir, {recursive: true, force: true});
}
}
});
it('with malformed filePath', async () => {
await withBrowser(async (response, context) => {
// Use a platform-specific invalid character.
// On Windows, characters like '<', '>', ':', '"', '/', '\', '|', '?', '*' are invalid.
// On POSIX, the null byte is invalid.
const invalidChar = process.platform === 'win32' ? '>' : '\0';
const filePath = `malformed${invalidChar}path.png`;
const fixture = screenshots.basic;
const page = context.getSelectedPage();
await page.setContent(fixture.html);
await assert.rejects(
screenshot.handler(
{params: {format: 'png', filePath}},
response,
context,
),
);
});
});
});
});