使用chrome cypress的标志运行测试

问题描述

我有一些使用网络摄像头的测试用例,我们的测试环境需要使用网络摄像头在chrome中设置标志--unsafely-treat-insecure-origin-as-secure

对于某些测试装置,我该如何在cypress上使用chrome设置此设置?

谢谢

解决方法

您可以通过编写 Cypress 插件将标志传递给 Cypress 中的 chrome 浏览器,如此处的官方文档所示:https://docs.cypress.io/api/plugins/browser-launch-api.html#Usage

导航到您的 cypress/plugins 目录并添加以下代码

module.exports = (on,config) => {
  on('before:browser:launch',(browser = {},launchOptions) => {
    // `args` is an array of all the arguments that will
    // be passed to browsers when it launches
  
    if (browser.name === 'chrome') {
      launchOptions.args.push('--unsafely-treat-insecure-origin-as-secure');
    }


    // whatever you return here becomes the launchOptions
    return launchOptions;
  });
};