electron 如何在cypress电子测试中禁用contextIsolation并启用nodeIntegration?

lyfkaqu1  于 2022-12-16  发布在  Electron
关注(0)|答案(2)|浏览(299)

我正在尝试访问这些属性(通过this更新提供)。
我在cypress.config.ts中尝试了以下操作,但似乎不起作用:

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // implement node event listeners here
      on("before:browser:launch", (browser, launchOptions) => {
        launchOptions.preferences.webPreferences.contextIsolation = false;
        launchOptions.preferences.webPreferences.nodeIntegration = true;
      });
    },
  },
});

先谢谢你。

m528fe3b

m528fe3b1#

添加浏览器检查,因为启动选项是特定于Electron的,并且将停止其他浏览器的运行。然后返回启动选项。

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      on('before:browser:launch', (browser = {}, launchOptions) => {
        if (browser.name === 'electron') {
          launchOptions.preferences.webPreferences.nodeIntegration = true;
          launchOptions.preferences.webPreferences.contextIsolation = false;
          launchOptions.preferences.webPreferences.enableRemoteModule = true;
        }
        return launchOptions;
      })
    },
  },
});
fae0ux8s

fae0ux8s2#

您必须在callaback中返回launchOptions对象才能使它们生效。
https://docs.cypress.io/api/plugins/browser-launch-api#Modify-browser-launch-arguments-preferences-extensions-and-environment

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // implement node event listeners here
      on("before:browser:launch", (browser, launchOptions) => {
        launchOptions.preferences.webPreferences.contextIsolation = false;
        launchOptions.preferences.webPreferences.nodeIntegration = true;
        return launchOptions;
      });
    },
  },
});

相关问题