【发布时间】:2021-01-05 16:44:25
【问题描述】:
假设我们有一个文件要下载,我们必须验证该文件是否已下载 所以首先你必须下载文件,然后验证文件是否在文件夹中。
【问题讨论】:
标签: typescript testing automated-tests e2e-testing testcafe
假设我们有一个文件要下载,我们必须验证该文件是否已下载 所以首先你必须下载文件,然后验证文件是否在文件夹中。
【问题讨论】:
标签: typescript testing automated-tests e2e-testing testcafe
const download_Image_Btn= XPathSelector("//*[text()='Download']")
const download_Png_image= XPathSelector("//li[text()='Download PNG image']")
const download_jpg_image= XPathSelector("//li[text()='Download JPEG image']")
const download_pdf_image= XPathSelector("//li[text()='Download PDF document']")
const fileNamepng = 'chart.png'
const fileNamejpg = 'chart.jpeg'
const fileNamepdf = 'chart.pdf'
const downloadLocation = '/Users/xddsd/Downloads/'; //download location in system
await t
.click(download_Image_Btn) //clicked on the link
.click(download_Png_image) //downloaded the file
await t.expect(fs.existsSync(downloadLocation + fileNamepng)).ok();
【讨论】:
您可以这样做。 TestCafe Examples repo 中始终提供实际示例。
import { RequestLogger } from 'testcafe';
const url = 'http://localhost:3000/download-file';
const logger = RequestLogger({ url, method: 'GET' }, {
logResponseHeaders: true,
logResponseBody: true,
stringifyResponseBody: true
});
fixture `Download file`
.page('./index.html')
.requestHooks(logger);
test('Check file name and content', async t => {
const fileNameRegEx = /attachment; filename=.*.txt/;
await t
.click('#download-btn')
.expect(logger.contains(r => {
if (r.response.statusCode !== 200)
return false;
const requestInfo = logger.requests[0];
if (!requestInfo)
return false;
const downloadedFileName = requestInfo.response.headers['content-disposition'];
if (!downloadedFileName)
false;
if (!fileNameRegEx.test(downloadedFileName))
return false;
const downloadedFileContent = logger.requests[0].response.body;
return downloadedFileContent === 'Test content';
})).ok();
});
【讨论】: