Reputation: 515
Suppose we have a file to download and we have to verify that file is downloaded so first you have to download the file and then verify that file is in the folder.
Upvotes: 0
Views: 1770
Reputation: 4274
Here's how you can do it. The actual example is always available in the 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();
});
Upvotes: 0
Reputation: 515
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();
Upvotes: 1