如何使用 WebdriverIO 设置下载 .dmg 文件的首选项?

How to set preference for downloading .dmg file using WebdriverIO?

我想使用 webdriverIO 下载一个 .dmg 文件,当 link 被点击时,文件下载弹出窗口弹出,这是一个 .dmg 文件。如何处理询问 "save" 或 "cancel" 的弹出窗口?

如果问题是保存弹窗,那么您可以通过设置浏览器的默认下载位置来避免弹窗。

对于CHROME:

wdio.conf.js 文件中,在 capabilities 中添加以下内容:

Note: Before adding make sure you have defined downloadDir with the default path where the file needs to be saved.

capabilities: [{
    browserName: 'chrome',
    // this overrides the default chrome download directory with our temporary one
    goog:chromeOptions: {
      prefs: {
        'download.default_directory': downloadDir
      }
    }
  }]

此代码将设置默认下载目录,您将不会再看到弹出窗口。

更多信息:https://blog.kevinlamping.com/downloading-files-using-webdriverio/

对于登陆这里寻找在无头浏览器中使用 WebdriverIO 下载文件的方法的人,您可以尝试下面的解决方案。在使用新的 Devtools automation protocol

时,上述解决方案不适用于处于无头模式的浏览器 运行

我的设置是 headless Chromium + WebdriverIOv6 using Devtools protocol 通过使用 click() 方法单击按钮来下载文件。如果您使用相同的设置或使用 Devtools 协议的任何其他浏览器,那么下面的内容应该有效。

  1. 安装devtools-servicenpm install @wdio/devtools-service --save-dev

package.json 应该提到并安装 devtoolsdevtools-service

 "devDependencies": {
    "@wdio/cli": "^6.2.0",
    "@wdio/devtools-service": "^6.4.0",
    "@wdio/local-runner": "^6.2.0",
    "@wdio/mocha-framework": "^6.2.0",
    "@wdio/spec-reporter": "^6.1.23",
    "@wdio/sync": "^6.2.0",
    "devtools": "^6.2.0"
  },
  1. 在您的规格文件中添加 browser.cdp() 片段,以无头模式将 Page.setDownloadBehavior() 命令发送到您的浏览器。 需要在单击下载按钮之前添加该代码段。将 download_path 更改为您需要的内容,确保它是绝对的。
browser.cdp('Page', 'setDownloadBehavior', {
        behavior: 'allow',
        downloadPath: download_path,
    });

// place before clicking the download button
$(.button).click()
  1. 在 wdio.conf.js 文件中,您的 capabilities 属性需要看起来像这样,以便下载路径也适用于 GUI 模式。
capabilities: [{
        browserName: "chrome",
        'goog:chromeOptions':{
          binary: "/usr/bin/chromium",
          args: ["--headless", "--disable-gpu", "--no-sandbox"],
        prefs: {
            'safebrowsing.enabled': false,
            'safebrowsing.disable_download_protection': true,
            "download": {
                "prompt_for_download": false,
                "directory_upgrade": true,
                "default_directory": download_path
            },
          }
        },
    }],

这应该有效。阅读更多关于 how it works 和其他几个解决方案的信息,如果这不起作用。