簡體   English   中英

Electron 應用程序打開多個 windows 或 Spectron 測試中的進程

[英]Electron app opening multiple windows or processes in spectron test

我的 electron 應用程序按預期工作,但是當我運行 Spectron 測試以測試 windows 打開的數量時,它會繼續打開新的 windows。

Electron 版本 - v8.0.2,“spectron”:“^10.0.1”。 我不確定如何檢查 spectron 的確切版本。 我只是在運行一個小演示,下面我將給出代碼片段

注意:我正在運行由 electron-packager 生成的指向 .exe 文件的 spectron 測試。

如果可能的話,有誰知道問題是什么?

main.js

const { app, BrowserWindow } = require('electron')

function createWindow () {

  let win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  win.loadFile('index.html')
}

app.whenReady().then(createWindow)

index.js

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

測試.js

const assert = require('assert');
const path = require('path');
const Application = require('spectron').Application;
const electronPath = require('electron');

const app = new Application({
    path: 'C:/demoElectronApp/winx64/demoelectronapp-win32-x64/demoelectronapp.exe',
  });

  describe('client_settings_app', function () {
    this.timeout(10000);

    beforeEach(() => {
      return app.start();
    });

    it('shows only one initial window', async () => {
      const count = await app.client.getWindowCount();
      return assert.equal(count, 1);
    });
  })

package.json

{
  "name": "demoelectronapp",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "electronpackage": "electron-packager . --out winx64"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "path": "^0.12.7"
  },
  "devDependencies": {
   "electron": "^8.2.2",
    "electron-packager": "^14.2.1",
    "assert": "^2.0.0",
    "mocha": "^7.1.1",
    "spectron": "^10.0.1"
  }
}

好的,我遇到了同樣的問題。 並通過設置遠程調試端口解決。

chromeDriverArgs: ['remote-debugging-port=9222']

完整的測試代碼:

const Application = require('spectron').Application
const assert = require('assert')
const electronPath = require('electron') // Require Electron from the binaries included in node_modules.
const path = require('path')

describe('Application launch', function () {
    this.timeout(10000)

    beforeEach(function () {
        this.app = new Application({
            // Your electron path can be any binary
            // i.e for OSX an example path could be '/Applications/MyApp.app/Contents/MacOS/MyApp'
            // But for the sake of the example we fetch it from our node_modules.
            path: path.join(__dirname, '..', 'node_modules', '.bin', 'electron' + (process.platform === 'win32' ? '.cmd' : '')),

            // Assuming you have the following directory structure

            //  |__ my project
            //     |__ ...
            //     |__ main.js
            //     |__ package.json
            //     |__ index.html
            //     |__ ...
            //     |__ test
            //        |__ spec.js  <- You are here! ~ Well you should be.

            // The following line tells spectron to look and use the main.js file
            // and the package.json located 1 level above.
            args: [path.join(__dirname, '..')],
            env: {
                ELECTRON_ENABLE_LOGGING: true,
                ELECTRON_ENABLE_STACK_DUMPING: true,
                NODE_ENV: 'test'
            },
            waitTimeout: 10e3,
            requireName: 'electronRequire',
            chromeDriverLogPath: '../chromedriverlog.txt',
            chromeDriverArgs: ['remote-debugging-port=9222']
        })
        return this.app.start()
    })

    afterEach(function () {
        if (this.app && this.app.isRunning()) {
            return this.app.stop()
        }
    })

    it('shows an initial window', function () {
        return this.app.client.getWindowCount().then(function (count) {
            assert.equal(count, 1)
            // Please note that getWindowCount() will return 2 if `dev tools` are opened.
            // assert.equal(count, 2)
        })
    })
})

我終於找到了一種對我有用的方法來解決這個問題。 我在@wburgess-invision 對此 gitHub 線程的評論中找到了它: https://github.com/electron-userland/spectron/issues/60

  1. 導航到 node_modules -> spectron -> lib -> launcher.js
  2. 在“const args = appArgs.concat(chromeArgs);”行之后添加一個新行並復制並粘貼:args.splice(args.indexOf('--enable-logging'), 1)
  3. npm 運行構建

引用線程:“我注意到它的發生是因為傳入了 --enable-logging 參數。所以我制作了一個修改版的 launcher.js,它故意不包含 --enable-logging 並且它運行良好對我們來說。我找不到 spectron 啟動器從哪里獲取 --enable-logging 標志,因為我認為更好的解決方案是首先不將其提供給 launcher.js,但我不能不知道所以我只是選擇了現在有效的解決方案.我的假設是當客戶端初始化並因此調用launcher.bat時--enable-logging標志在webdriverio內部的某個地方被拾取但我不能找出哪里。”

我遇到了類似的問題,並更改了我正在使用的軟件包的版本。 此頁面有版本 map: https://github.com/electron-userland/spectron#version-map

看起來這不是您的問題,可能會幫助其他人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM