Protractor file download test fails when headless chrome
Asked Answered
E

3

5

I am having an issue with a protractor test. It was working, but now (even thought nothing has changed) it is not. The test is just opening the app (web application) and clicking on a button to download an image. The download should start straight away. The problem is that the next instruction after the download event throws an exception, Failed: chrome not reachable. I am using the latest chrome and chrome driver versions.

The capabilites section for protractor is like this:

capabilities: {
   browserName: 'chrome',
   loggingPrefs: { browser: 'ALL' },
   chromeOptions: {
      args: ['--headless', '--window-size=1240,780'],
   },
}

I am reading about using DevTools to enable downloads in headless mode (Page.setDownloadBehavior), but so far no luck.

Does anybody have this issue too? Any clue how to fix it?

Thanks.

Elodea answered 16/2, 2018 at 16:43 Comment(0)
E
4

There could be another easy way to do it, but this is what I have done in my test suite. I used got library, however, you can use any library to send an HTTP post request.

Discussion about setting download directory in headless chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=696481

let got = require('got');
let session = await browser.getSession();
let sessionId = session['id_'];
let params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': downloadDir }}
await got.post('http://localhost:4444/wd/hub/session/'+ sessionId + '/chromium/send_command', {body: JSON.stringify(params)})

If you have not disabled ControlFlow in your protractor config, change ASync/Await to .then.

Eld answered 16/2, 2018 at 19:54 Comment(1)
This solution didn't work for me unfortunately. Post request returns with no error, but after download is triggered, 'chrome is not reachable' is returned on any subsequent request. (Using Chrome 64.0.3282.119 and ChromeDriver 2.35.528161 on Windows 7) Any clue on what could go wrong?Clevis
E
4

An easier solution is to add these lines to your protractor.conf.js:

exports.config = {
  ...
  onPrepare() {
    ...
    browser.driver.sendChromiumCommand('Page.setDownloadBehavior', {
      behavior: 'allow',
      downloadPath: downloadsPath
    });
  }
};

From: https://bugs.chromium.org/p/chromium/issues/detail?id=696481#c196

Appendix

If you are to lazy to find a Download Path just paste this at the top of your protractor.conf.js:

var path = require('path');
var downloadsPath = path.resolve(__dirname, './downloads');

It will download the file to the e2e/downloads folder. Just use the same code in your tests to find out if the file downloaded.

Evolution answered 28/4, 2019 at 14:51 Comment(0)
E
0

This works for me:

 chromeOptions: {
  'args': [
    '--headless',
    '--disable-gpu',
    '--test-type=browser',
    '--disable-extensions',
    '--no-sandbox',
    '--disable-infobars',
    '--window-size=1920,1080',
    //'--start-maximized'
     "--disable-gpu",
  ],
    prefs: {    
        'download.default_directory': 'C:\\downloads',
        'download.prompt_for_download':false,
        'download.directory_upgrade':true,
        "safebrowsing.enabled":false,
        "safebrowsing.disable_download_protection":true
      },
},
Entanglement answered 6/10, 2021 at 11:59 Comment(2)
It would be helpful if you explained your answer.Anting
You need to update your conf.ts file fith this. What other info do you need?Entanglement

© 2022 - 2024 — McMap. All rights reserved.