Skip to content

Commit

Permalink
fixed download with saveAs and Remote Browser (#3433)
Browse files Browse the repository at this point in the history
Signed-off-by: René <[email protected]>
  • Loading branch information
Snooz82 authored Feb 16, 2024
1 parent 795355c commit 3754c50
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Browser/keywords/promises.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ def promise_to_wait_for_download(
With default filepath downloaded files are deleted when Context the download happened in is closed.
If browser is connected remotely with `Connect To Browser` then ``saveAs`` must be set to store it locally where the browser runs!
| =Arguments= | =Description= |
| ``saveAs`` | Defines path where the file is saved persistently. File will also temporarily be saved in playwright context's default download location. If empty, generated unique path (GUID) is used and file is deleted when the context is closed. |
| ``wait_for_finished`` | If true, promise will wait for download to finish. If false, promise will resolve immediately after download has started. |
Expand Down
30 changes: 29 additions & 1 deletion atest/test/02_Content_Keywords/files.robot
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Force Tags slow

*** Variables ***
${CUSTOM_DL_PATH} = ${CURDIR}/download_file
${ORIGINAL_TIMEOUT} 1s

*** Test Cases ***
Upload Upload_test_file
Expand Down Expand Up @@ -123,7 +124,35 @@ Wait For Download Relative To downloadsPath
File Should Exist ${OUTPUT DIR}/test_download_file_saveAs.js
Remove File ${download_file_object}[saveAs]

Wait For Download with Remote Browser
[Setup] Launch and Connect To Remote Browser
New Context acceptDownloads=True
New Page ${LOGIN_URL}
${dl_promise} = Promise To Wait For Download saveAs=persistent_downloads
Click \#file_download
${file_object} = Wait For ${dl_promise}
File Should Exist ${file_object}[saveAs]
Should Be Equal ${file_object}[suggestedFilename] test_download_file.js
Remove File ${file_object}[saveAs]

Wait For Download with Remote Browser Without SaveAs
[Setup] Launch and Connect To Remote Browser
New Context acceptDownloads=True
New Page ${LOGIN_URL}
${dl_promise} = Promise To Wait For Download
Click \#file_download
TRY
${file_object} = Wait For ${dl_promise}
EXCEPT *Path is not available when connecting remotely. Use saveAs() to save a local copy. type=GLOB
Log Correct Error
END

*** Keywords ***
Launch and Connect To Remote Browser
${ws} Launch Browser Server headless=${HEADLESS}
Connect To Browser wsEndpoint=${ws}
Set Browser Timeout 90 seconds

Set Library Timeout
${open_browsers} = Get Browser Ids
IF $open_browsers == []
Expand All @@ -132,7 +161,6 @@ Set Library Timeout
${current_contexts} = Get Context Ids Active Active
IF $current_contexts == [] New Context
${timeout} = Set Browser Timeout 90 seconds
Set Suite Variable ${ORIGINAL_TIMEOUT} 1s

Restore Library Timeout
Set Browser Timeout ${ORIGINAL_TIMEOUT}
Expand Down
12 changes: 4 additions & 8 deletions node/playwright-wrapper/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ export async function _waitForDownload(
}
}
const fileName = downloadObject.suggestedFilename();
const pathPromise = downloadObject.path();
if (!waitForFinished) {
const downloadID = uuidv4();
const activeIndexedPage = state.activeBrowser?.page;
Expand All @@ -170,25 +169,22 @@ export async function _waitForDownload(
throw new Error('No active page found');
}
}
let downloadPath;
if (downloadTimeout > 0) {
downloadPath = await Promise.race([
pathPromise,
const readStream = await Promise.race([
downloadObject.createReadStream(),
new Promise((resolve) => setTimeout(resolve, downloadTimeout)),
]);
if (!downloadPath) {
if (!readStream) {
downloadObject.cancel();
throw new Error('Download failed, Timeout exceeded.');
}
} else {
downloadPath = await pathPromise;
}
let filePath;
if (saveAs) {
await downloadObject.saveAs(saveAs);
filePath = path.resolve(saveAs);
} else {
filePath = downloadPath;
filePath = await downloadObject.path();
}
logger.info('suggestedFilename: ' + fileName + ' saveAs path: ' + filePath);
return jsonResponse(
Expand Down

0 comments on commit 3754c50

Please sign in to comment.