🌊

How to enable download in chrome-headless when using webdriverio

2021/08/21に公開

Sample code

async mode

import { Browser, remote } from 'webdriverio';

(async () => {
  // Create Browser instance
  const browser: Browser<'async'> = await remote({
    capabilities: {
      browserName: 'chrome',
      'goog:chromeOptions': {
        args: ['--headless']
      },
    },
    automationProtocol: 'devtools',
    services: ['devtools'],
  });

  // Enable download in headless mode
  const puppeteerBrowser = await browser.getPuppeteer();
  const pages = await puppeteerBrowser.pages();
  const client = await pages[0].target().createCDPSession();
  await client.send('Page.setDownloadBehavior', {
    behavior: 'allow',
    downloadPath: '/path/to/download_directory',
  });

  // Download file
  const downloadButton = await browser.react$('DownloadButtonSelector');
  await downloadButton.click();
})();

Discussion