Skip to content

Instantly share code, notes, and snippets.

@nzbart
Last active January 29, 2019 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzbart/1fbde6d451a46c60519e7b92a67b2c38 to your computer and use it in GitHub Desktop.
Save nzbart/1fbde6d451a46c60519e7b92a67b2c38 to your computer and use it in GitHub Desktop.
This is an attempt to reproduce the issue downloading files in headless Chrome, as reported at: https://bugs.chromium.org/p/chromium/issues/detail?id=916113

Steps to reproduce

These steps run the same file download test twice:

  • Once in GUI mode.
  • Once in headless mode.

For me, the download never starts or completes in headless mode.

Bug: Issue 916113: Unable to download a file when using in headless mode (selenium, chromedriver)

Prerequisites:

  • Chrome or Chromium installed.
  • npm and node available on the path.

These instructions have been tested on both Windows and Ubuntu 18.04:

git clone https://gist.github.com/1fbde6d451a46c60519e7b92a67b2c38.git DownloadBugRepro
cd DownloadBugRepro
npm install
node index.js
require('chromedriver');
const fs = require('fs');
const glob = require('glob');
const process = require('process');
const uuidv4 = require('uuid/v4');
const tempDir = require('temp-dir')
const { Builder, By, Key, until, logging } = require('selenium-webdriver');
const chromeDriver = require('selenium-webdriver/chrome');
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function download(shouldBeHeadless) {
const options = new chromeDriver.Options();
const newTempDir = `${tempDir}/${uuidv4()}`
console.log(`Creating new directory for downloads at: ${newTempDir}`);
fs.mkdirSync(newTempDir);
if(shouldBeHeadless) {
options.addArguments('headless', 'disable-gpu');
}
options.addArguments('no-sandbox');
options.setUserPreferences({ 'download.default_directory': newTempDir });
const driver = await new Builder().forBrowser('chrome').setChromeOptions(options).build();
try
{
await driver.get('https://download-chromium.appspot.com/');
await driver.findElement(By.id('download')).sendKeys(Key.RETURN);
const startTime = process.hrtime();
while(glob.sync(`${newTempDir}/chrome-*.zip`).length == 0) {
const now = process.hrtime();
const elapsedSeconds = now[0] - startTime[0]
if(elapsedSeconds > 300) {
throw "Download didn't complete.";
}
console.log(`Waiting for file to download (${elapsedSeconds} seconds elapsed)...`);
await sleep(1000);
}
console.log('Successfully downloaded.');
}
catch(e)
{
console.error(e);
}
finally
{
await driver.quit();
}
}
(async function run() {
await download(false);
await download(true);
})();
{
"name": "DownloadBugRepro",
"version": "1.0.0",
"description": "Repro of headless mode file download bug",
"main": "index.js",
"author": "Bart Joy",
"license": "ISC",
"devDependencies": {
"chromedriver": "2.45.0",
"selenium-webdriver": "3.6.0",
"temp-dir": "1.0.0",
"uuid": "3.3.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment