|
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 | import assert from 'node:assert'; |
| 7 | +import {rm, stat, mkdir, chmod, writeFile} from 'node:fs/promises'; |
| 8 | +import {tmpdir} from 'node:os'; |
| 9 | +import {join} from 'node:path'; |
7 | 10 | import {describe, it} from 'node:test'; |
8 | 11 |
|
9 | 12 | import {screenshot} from '../../src/tools/screenshot.js'; |
@@ -108,5 +111,111 @@ describe('screenshot', () => { |
108 | 111 | ); |
109 | 112 | }); |
110 | 113 | }); |
| 114 | + |
| 115 | + it('with filePath', async () => { |
| 116 | + await withBrowser(async (response, context) => { |
| 117 | + const filePath = join(tmpdir(), 'test-screenshot.png'); |
| 118 | + try { |
| 119 | + const fixture = screenshots.basic; |
| 120 | + const page = context.getSelectedPage(); |
| 121 | + await page.setContent(fixture.html); |
| 122 | + await screenshot.handler( |
| 123 | + {params: {format: 'png', filePath}}, |
| 124 | + response, |
| 125 | + context, |
| 126 | + ); |
| 127 | + |
| 128 | + assert.equal(response.images.length, 0); |
| 129 | + assert.equal( |
| 130 | + response.responseLines.at(0), |
| 131 | + "Took a screenshot of the current page's viewport.", |
| 132 | + ); |
| 133 | + assert.equal( |
| 134 | + response.responseLines.at(1), |
| 135 | + `Saved screenshot to ${filePath}.`, |
| 136 | + ); |
| 137 | + |
| 138 | + const stats = await stat(filePath); |
| 139 | + assert.ok(stats.isFile()); |
| 140 | + assert.ok(stats.size > 0); |
| 141 | + } finally { |
| 142 | + await rm(filePath, {force: true}); |
| 143 | + } |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it('with unwritable filePath', async () => { |
| 148 | + if (process.platform === 'win32') { |
| 149 | + const filePath = join( |
| 150 | + tmpdir(), |
| 151 | + 'readonly-file-for-screenshot-test.png', |
| 152 | + ); |
| 153 | + // Create the file and make it read-only. |
| 154 | + await writeFile(filePath, ''); |
| 155 | + await chmod(filePath, 0o400); |
| 156 | + |
| 157 | + try { |
| 158 | + await withBrowser(async (response, context) => { |
| 159 | + const fixture = screenshots.basic; |
| 160 | + const page = context.getSelectedPage(); |
| 161 | + await page.setContent(fixture.html); |
| 162 | + await assert.rejects( |
| 163 | + screenshot.handler( |
| 164 | + {params: {format: 'png', filePath}}, |
| 165 | + response, |
| 166 | + context, |
| 167 | + ), |
| 168 | + ); |
| 169 | + }); |
| 170 | + } finally { |
| 171 | + // Make the file writable again so it can be deleted. |
| 172 | + await chmod(filePath, 0o600); |
| 173 | + await rm(filePath, {force: true}); |
| 174 | + } |
| 175 | + } else { |
| 176 | + const dir = join(tmpdir(), 'readonly-dir-for-screenshot-test'); |
| 177 | + await mkdir(dir, {recursive: true}); |
| 178 | + await chmod(dir, 0o500); |
| 179 | + const filePath = join(dir, 'test-screenshot.png'); |
| 180 | + |
| 181 | + try { |
| 182 | + await withBrowser(async (response, context) => { |
| 183 | + const fixture = screenshots.basic; |
| 184 | + const page = context.getSelectedPage(); |
| 185 | + await page.setContent(fixture.html); |
| 186 | + await assert.rejects( |
| 187 | + screenshot.handler( |
| 188 | + {params: {format: 'png', filePath}}, |
| 189 | + response, |
| 190 | + context, |
| 191 | + ), |
| 192 | + ); |
| 193 | + }); |
| 194 | + } finally { |
| 195 | + await chmod(dir, 0o700); |
| 196 | + await rm(dir, {recursive: true, force: true}); |
| 197 | + } |
| 198 | + } |
| 199 | + }); |
| 200 | + |
| 201 | + it('with malformed filePath', async () => { |
| 202 | + await withBrowser(async (response, context) => { |
| 203 | + // Use a platform-specific invalid character. |
| 204 | + // On Windows, characters like '<', '>', ':', '"', '/', '\', '|', '?', '*' are invalid. |
| 205 | + // On POSIX, the null byte is invalid. |
| 206 | + const invalidChar = process.platform === 'win32' ? '>' : '\0'; |
| 207 | + const filePath = `malformed${invalidChar}path.png`; |
| 208 | + const fixture = screenshots.basic; |
| 209 | + const page = context.getSelectedPage(); |
| 210 | + await page.setContent(fixture.html); |
| 211 | + await assert.rejects( |
| 212 | + screenshot.handler( |
| 213 | + {params: {format: 'png', filePath}}, |
| 214 | + response, |
| 215 | + context, |
| 216 | + ), |
| 217 | + ); |
| 218 | + }); |
| 219 | + }); |
111 | 220 | }); |
112 | 221 | }); |
0 commit comments