javascript - 加载html时如何等待 Electron

标签 javascript electron webdriver-io spectron

我有一个 Electron 应用程序,它在打开时加载 HTML 文件。 当我尝试从打开的页面中使用 waitUntil 方法等待元素时,Spectron 尝试在页面加载时找到该元素,并且它会使我的应用程序崩溃并停留在空白页面。我如何等待加载此 HTML?

我的应用程序启动代码如下:

async start() {
    try {
      await this.spectron.start();
      await this.focusOnWindow(0);
      return this._checkWindowReady();
    } catch (err) {
      throw err;
    }
  }

 beforeEach(async function (){
            app = new SpectronApplication();
            common = new CommonActions();

            await app.start();
 })

最佳答案

我找到了如下代码的解决方案:

首先,当我调用 app.start() 时,

start()函数调用_checkWindowReady()

_checkWindowReady 调用 waitFor()

最后 waitFor 调用 _callClientAPI() 并查找特定的函数和元素。

 async start() {
    try {
      await this.spectron.start();
      await this.focusOnWindow(0);
      return this._checkWindowReady();
    } catch (err) {
      throw err;
    }
  }

 _checkWindowReady() {
    return this.waitFor(this.spectron.client.getHTML, '[id="myApp.main.body"]');
  }

 waitFor(func, args) {
    return this._callClientAPI(func, args);
  }

 _callClientAPI(func, args) {
    let trial = 1;
    return new Promise(async(res, rej) => {
      while (true) {
        if (trial > this._pollTrials) {
          rej(`Could not retrieve the element in ${this._pollTrials * this._pollTimeout} seconds.`);
          break;
        }

        let result;
        try {
          result = await func.call(this.client, args, false);
        } catch (e) { }

        if (result && result !== '') {
          res(result);
          break;
        }

        await this.wait();
        trial++;
      }
    });
  }

关于javascript - 加载html时如何等待 Electron ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52741110/

相关文章:

c++ - 将 node-ffi 与 Electron 一起使用无法正常工作(无法找到消息编号 0x%1 的消息文本...)

带有 Selenium 独立服务器的 Node.js 和 Webdriver.io 无法使用自定义配置文件

javascript - WebdriverIO 页面对象继承

javascript - 如何让 jQuery 在管理区工作?

javascript - 如何将 Knockout View 模型移到 html 页面之外

javascript - FireFox 中的 Sencha ExtJs TextField KeyDown 事件

electron - 在预加载脚本中调用ipc通信是一种好方法吗?

node.js - 用 Electron 而不是 Node 运行 Jest

Javascript/Dom 将图像加载到网格中

javascript - 无法使用 webdriverio 单击网页按钮