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.

Antonio C G
  • 331
  • 2
  • 12

3 Answers3

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.

Barney
  • 1,851
  • 2
  • 19
  • 36
  • 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? – Artemy Feb 27 '18 at 10:07
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.

Daniel Habenicht
  • 2,133
  • 1
  • 15
  • 36
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
      },
},
Robert
  • 3
  • 2