Ayush Singhania
Ayush Singhania

Reputation: 165

Clicking on a download link and applying .wait(120000) throws an error

I am creating a test where it goes to a specific page and clicks on and the button which downloads an excel file.

The file is hosted in a far off location it takes generally 1.5 mins to gather it from the host server before the downloading is started, which only takes 2-3 seconds to download it completely.

From the time between I click on the submit button and the time the download is started, there is a gap of 1.5 mins(as mentioned before).

I tried applying .wait(120000) - 2 mins to be on the safe side.

The test gives an error (see the attached image below).

Screenshot of the error

This is my test code.

test('R03', async t => {
await t  
    .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Year_1'))
    .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Location_1'))
    .click(Selector('span').withText('SUBMIT'))
    .wait(120000); // in ms
});

Debugging the test showed me the following error:

 × R03

   1) Failed to complete a request to

   "http://example.com/Reports/ViewerPartial/DXXRDV.axd?actionKey=exportTo&arg=%7B%22documentId%22%3A%227c93875b0e0247e391d50759c00ef3a7%22%2C%22exportOptions%22%3A%22%7B%5C%22Html%5C%22%3A%7B%5C%22%40EmbedImagesInHTML%5C%22%3A%5C%22true%5C%22%7D%7D%22%2C%22format%22%3A%22xlsx%22%7D"
      within the timeout period. The problem may be related to local 
      machine's network or firewall settings, server outage, or network 
      problems that make the server inaccessible.

I have hidden the domain name changed it to example.com for company reasons.
If I removed the .wait(120000) the test is completed and is shown successful. Any suggestion would be appreciated. Trying to get the hang of it (testcafe)

Upvotes: 3

Views: 173

Answers (1)

hdorgeval
hdorgeval

Reputation: 3030

As a workaround, you could wait for the file to arrive in the download folder by using a for loop:

import { join } from 'path';
import { existsSync } from 'fs';
import {t} from 'testcafe';

test("My Test", async (t) => {
    await t  
    .click(Selector('[data-bind^="css:{ \\\'dx-state- disabled\\\'].find('div').withText('Year_1'))
    .click(Selector('[data-bind^="css:{ \\\'dx-state-disabled\\\'].find('div').withText('Location_1'))
    .click(Selector('span').withText('SUBMIT'));

   await waitUntilFileIsDownloaded();
});

async function waitUntilFileIsDownloaded(){
    const downloadsFolder= `${process.env.HOME}/Downloads`;
    const expectedFile = join(downloadsFolder, 'data.csv');
    console.log(`waiting for file '${expectedFile}' ...`);
    for (let index = 0; index < 120; index++) {
        if (existsSync(expectedFile)) {
            console.log(`file downloaded after ${index} seconds`);
            return;
        }
        await t.wait(1000);
        console.log(`waiting for '${index}' seconds`);
    }
    console.log('File has not been downloaded in due time');
}

Upvotes: 1

Related Questions