当使用 Playwright 启动时,竞争条件导致资产从 Electron 中的错误位置加载

发布于 2025-01-20 16:55:41 字数 2099 浏览 0 评论 0原文

我有一个电子应用程序,我正在尝试将剧作家测试添加到,并且当我使用playwright _Electron.launch()启动它时,我有一些SVG文件在我的CSS中使用,以尝试从node_modules/electron/dist/resources/electron.asar文件,而不是来自webpack /build>/build目录。从命令行手动启动它可以正常工作,而无需重建或其他任何内容。

文件在左侧正确加载,右侧SVG文件尝试从electron.asar加载node_modules文件夹中”

当我检查图标 -加载元素在DevTools元素选项卡中,URL显示为这样:

.icon-load {
    background: url(file:///F:/Programming/REDACTED/node_modules/electron/dist/resources/electron.asar/renderer/287f560038de84fa9ced.svg);
}

到目前为止,我已经尝试验证bose.baseuri以及_Electron.launch()上的一些不同选项。

DevTools Console会话:

window.$("body").baseURI
"file:///F:/Programming/REDACTED/build/"

启动-Electron.js:

// Launch Electron app.
const electronApp = await electron.launch({
  args: ["."],
  //cwd: "F:\\Programming\\REDACTED", // no change
  //bypassCSP: true, // no change
});

更新2:刷新页面会导致SVG文件从正确的位置加载!我们似乎有一个种族条件,并在电子发射中具有某种上下文切换。我将再次添加WebPack配置的片段:

module.exports = {
  context: __dirname + "/renderer",
  entry: "./App.js",
  output: {
    path: __dirname + "/build",
    filename: "bundle.[fullhash].js",
  },
  stats: {
    errorDetails: true,
    children: true,
  },
  module: {
    rules: [
      {
        test: /App\.js$/i,
        loader: "expose-loader",
        options: {
          exposes: {
            globalName: "FF",
          },
        },
      },
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"], // disabling URL loading in CSS loader makes app load but without CSS images
      },
      {
        test: /\.(jpg|png|ico|svg)$/i,
        type: "asset/resource",
      },
    ],
  },
};

I have an electron app I am trying to add playwright tests to, and when I launch it using the playwright _electron.launch(), I have some SVG files that are used in my CSS trying to load from the node_modules/electron/dist/resources/electron.asar file, instead of from the Webpack /build directory. Launching it manually from the command line works fine, without rebuilding or anything.

showing all files loading properly on the left, and on the right side SVG files try to load from electron.asar inside node_modules folder

Any ideas what could cause this?

When I inspect the icon-load element in devtools element tab, the URL shows up like so:

.icon-load {
    background: url(file:///F:/Programming/REDACTED/node_modules/electron/dist/resources/electron.asar/renderer/287f560038de84fa9ced.svg);
}

So far I have tried verifying body.baseURI as well as a few different options on _electron.launch().

Devtools console session:

window.$("body").baseURI
"file:///F:/Programming/REDACTED/build/"

launch-electron.js:

// Launch Electron app.
const electronApp = await electron.launch({
  args: ["."],
  //cwd: "F:\\Programming\\REDACTED", // no change
  //bypassCSP: true, // no change
});

Update 2: Refreshing the page causes the SVG files to load from the correct location!! We appear to have a race condition with some kind of context switching in the electron launch. I'll add a snippet of my Webpack config again:

module.exports = {
  context: __dirname + "/renderer",
  entry: "./App.js",
  output: {
    path: __dirname + "/build",
    filename: "bundle.[fullhash].js",
  },
  stats: {
    errorDetails: true,
    children: true,
  },
  module: {
    rules: [
      {
        test: /App\.js$/i,
        loader: "expose-loader",
        options: {
          exposes: {
            globalName: "FF",
          },
        },
      },
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"], // disabling URL loading in CSS loader makes app load but without CSS images
      },
      {
        test: /\.(jpg|png|ico|svg)$/i,
        type: "asset/resource",
      },
    ],
  },
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文