Home > database >  Headless mode and non headless mode have different behavior when downloading same file multiple time
Headless mode and non headless mode have different behavior when downloading same file multiple time

Time:12-29

While I download the file (Say "a.png") multiple time in Non-headless mode THEN it makes a copy of that file in the downloaded folder (like "a.png" and "a(1).png").

BUT

While I download the file (Say "a.png") multiple time in Headless mode THEN it does not makes a copy of that file in the downloaded folder , instead update with latest file(checked the datetime) and only one file is present.

Any Idea why this different behavior and appreciate a solution for this

I was trying to test that in each downloads the count should be incremented by 1 in downloaded folder.

Script for testing can be found on : https://gist.github.com/Anilkumar-Shrestha/a81b7b7a4381db54ae8bb9ab9bdf75b4

CodePudding user response:

Reasons

Based on your experiments, and also based on my experience:

Chrome and Headless Chrome may behave differently (by many reasons). I've not found the clear documentation, but some Chrome browser options and preferences provided during chromedriver session start may be ignored by headless browser or may lead to defferent behaviour (like in your case with download). At least you may found several relevant questions on stackoverflow or github issues. Which might confirm my assumptions.

The main reason I see is that Chrome is based on Chromium and provides a lot of adjustments and customizations. But in headless mode seems, pure Chromium browser launched (not Chrome).

How to deal with this behaviour

I assume you have access to the file system with java (based on your code example).

1 If you only want to validate the file content, you may always remove the downloaded file after validating it.

2 If you need to keep all the files, I suggest just to move the new downloaded file to another directory and rename it as you like. So you'll controll all the file names are unique.

You might use apache commons lib for manage files: https://zetcode.com/java/fileutils/.

If you run tests in parallel, try to keep tests with download steps within a single thread, or use unique download directories per browser, or implement synchronization for download actions.

  • Related