可能是由于保存PDF文件的过程中,浏览器没有完全关闭而导致加载PDF文件时出现TestCafe E70错误。可以在保存PDF文件后等待一段时间(例如500毫秒),再加载PDF文件,在这段时间内确保浏览器已经完全关闭。
代码示例:
import { Selector, t } from 'testcafe';
import { writeFile } from 'fs';
import { promisify } from 'util';
const writeFilePromise = promisify(writeFile);
fixture `Save and Load PDF`
.page `https://example.com`;
test('Save and Load PDF', async t => {
const pdfButton = Selector('button').withText('Generate PDF');
const pdfPath = './test.pdf';
// 生成PDF文件
await t.click(pdfButton);
// 等待500毫秒,确保浏览器已经完全关闭
await t.wait(500);
// 保存PDF文件
const pdfBuffer = await t.getNativeDialogHistory().download(pdfPath);
await writeFilePromise(pdfPath, pdfBuffer);
// 等待500毫秒,确保浏览器已经完全关闭
await t.wait(500);
// 加载PDF文件
await t.navigateTo(`file://${process.cwd()}/${pdfPath}`);
});
需要安装依赖:
npm install fs util
上一篇:保存PDF并发送电子邮件