Opening a File or Take Screenshot on headless mode

Hello All, is there a way or library that opens, views, and takes a screenshot of a file while on a headless mode?

Hi Shiwo,

I’m not entirely sure what you’re asking? there’s Take Screenshot, that takes screenshots in Browser Library and it works in headless mode.

Any other screenshot library external to Browser Library would not be able to take a screen shot of the browser if it’s in headless mode, as the browser wouldn’t be visible.

If a browser can display a file then Browser Library can load the file and Browser Library’s Take Screenshot keyword will take a screenshot of it.

Opening a local file should be just a matter of using Go To with a file:// type url.

I hope that helps, if not can you clarify what you want to do?

Dave.

1 Like

Hello Dave,

Thank you for your reply.
So what I want to do is I want to check the downloaded attachment from the email by verifying if the attachment can be opened successfully.

And this is my script:
${dl_promise}= Promise To Wait For Download
Click div#messageAttachmentList a[download=‘My Attachment.pdf’]
${fileObject}= Wait For ${dl_promise}
New Page file://${fileObject[saveAs]}
Take Screenshot

The download path that I set is /home/shiwo/robot-test/data/temp/

But I encountered an error on New Page part:
Error: page.goto: net::ERR_ABORTED at file:///home/shiwo/robot-test/data/temp/5a8ac8e7-70a0-48da-8f71-815abcc50393 Call log: [2m- navigating to "file:///home/shiwo/robot-test/data/temp/5a8ac8e7-70a0-48da-8f71-815abcc50393", waiting until "load"[22m 14:09:21.185 INFO File path of the downloaded file: /home/shiwo/robot-test/data/temp/5a8ac8e7-70a0-48da-8f71-815abcc50393

I also tried Go To, but I still got the same error.
New Page
Go To file://${fileObject[saveAs]}

The script is working okay in a headful mode.

Hi Shiwo,

First issue I see is the filename:

/home/shiwo/robot-test/data/temp/5a8ac8e7-70a0-48da-8f71-815abcc50393

From what I remember the browser uses the file extension to determine the mime type when loading a local file, and your file has no extension, so the browser is probably saying I don’t know what to do with this file

First thing I’d try is to use Move File (from OperatingSystem Library) to rename the files as:

/home/shiwo/robot-test/data/temp/5a8ac8e7-70a0-48da-8f71-815abcc50393.pdf

And see if that then works with Go To?

Depending on what you need to validate, there’s a couple of suggestions to consider:

If you just need to verify the file downloaded is a valid PDF file, you could use the pypdf python module, with the builtin Evaluate keyword something like this:

${result}=    Evaluate     pypdf.PdfReader("/home/shiwo/robot-test/data/temp/5a8ac8e7-70a0-48da-8f71-815abcc50393")    modules=pypdf

*Note: I’ve not tried this, so it may take some experimenting

If you need to verify the content of the PDF against a known good PDF, then this library might be useful:

Dave.

Add wait in script i think it works
Sleep 3s

Hello Dave,

I added the .pdf extension by adding pdf filename in saveAs argument of Promise To Wait For Download:

${dl_promise}= Promise To Wait For Download saveAs=My Attachment.pdf
Click div#messageAttachmentList a[download=‘My Attachment.pdf’]
${fileObject}= Wait For ${dl_promise}
New Page file://${fileObject}[saveAs]
Take Screenshot

I changed the browser from chromium to firefox and I noticed new log message ‘Download is starting’
Error: page.goto: Download is starting
Call log: navigating to “file:///home/shiwo/robot-test/data/temp/My%20Attachment.pdf”, waiting until “load”

So I assumed that on headless mode, browser is attempting to download the file instead of rendering it in the browser.

I tried the scripts above on headful mode and the file displays in the browser.

Hi Phoenix,

I tried adding wait (Sleep 3s) but still encountered the error.

Hi Shiwo,

That’s interesting, I never knew there was a difference between the headless mode and headful mode, maybe one of the dev’s from Browser Library will be able to chime in with some details :crossed_fingers: I would have expected the browser to behave the same way in both modes.

Dave.

Thank you so much for the inputs, Dave!

1 Like