I’m designing a Browser-library check for a page that exports an image. The test needs to click Download and then open the resulting file. A file from an earlier run could have the same name, so finding that name in the download directory would be a weak check.
My intended sequence is to start listening for the download before clicking, use a fresh destination for the current run, and then try to decode the file. I’d like to keep the failure messages separate so a timeout waiting for the download looks different from an unreadable image.
Where do you draw that line in Robot Framework Browser? Is there a download keyword or result you rely on for completion before the image check starts? If you have a small example that handles a repeated filename, I’d be interested in how you set up the destination and which step reports a failed download.
I would suggest to use Get Time keyword from the BuiltIn library to generate a timestamp variable, use that in the as part of the saveAs argument to the Download keyword from Browser Library to control the file name of the file that’s downloaded, then your can search for the filename with that timestamp.
I don’t have a working example but I expect it would look something like this:
*** Settings ***
Library Browser
Library BuiltIn
Library OperatingSystem
*** Tests ***
Check Image Download
${imgurl} = Get Attribute #your_img href
${timestamp} = Get Time epoch
Download ${imgurl} saveAs=${OUTPUT DIR}/downloads/imagename_${timestamp}.png
File Should Not Exist ${OUTPUT DIR}/downloads/imagename_${timestamp}.png
I just typed that example based on the docs so you may need to tweak it but it should be close to what you’re after.
${timestamp}= Get Current Date result_format=%Y%m%d_%H%M%S
VAR ${file_path} ${OUTPUT_DIR}/${TEST_NAME}/${timestamp}.jpg
${old_timeout} Set Browser Timeout timeout=191s
${dl_promise} Promise To Wait For Download saveAs=${file_path}
Click ${download_button}
TRY
${file_object} Wait For ${dl_promise}
EXCEPT
Switch Page NEW
Take Screenshot EMBED
END
Set Browser Timeout timeout=${old_timeout}
File Should Exist ${file_object}[saveAs] msg=Image not found: ${file_object}[saveAs]
This saves images in the output folder, there under a folder with the tests name and a timestamp as filename.
Setting Browser timeout makes sure download has enough time to start and finish.
If I get a timeout, those are shown in a separate tab, therfore “Switch Page” before taking a screenshot.
Hope this helps.
It provides event-driven keywords to monitor file creation, modification, and stability, which can be useful when validating downloaded or generated files.