0

I have an electron app which loads an HTML file on opening. When I tried to wait for an element with waitUntil method from the opening page, Spectron tries to find that while page loading and it crashes my app and app staying at the blank page. How can I wait for loading of this HTML?

My application launch code is below :

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();
 })
Jay Patel
  • 2,341
  • 2
  • 22
  • 43
sayhan
  • 1,168
  • 1
  • 16
  • 22
  • can u use `webContents` events? [`'dom-ready'`](https://electronjs.org/docs/api/web-contents#event-dom-ready) for example – pergy Oct 10 '18 at 13:40
  • not yet i am now trying it now but it is electron's function, I need to handle this issue on spectron. – sayhan Oct 10 '18 at 14:26
  • yes, I see that. I'm not familiar with spectron, just seen this in docs: "Spectron makes the entire Chromium and Electron APIs available to your tests." That's why I thought it's easy to access webContents – pergy Oct 10 '18 at 14:30
  • "Spectron makes the entire Chromium and Electron APIs available to your tests." but when you add some os level uı elements spectron can't handle it.(for example right click opening a menu but it is comes from os) I am not believing that anymore. I will try your answer thank you about it. – sayhan Oct 10 '18 at 14:33
  • I’m not familiar with spectron, but in your example you spelled ‘beforeEach’ wrong. Could the typo be the problem? – Andre Oct 11 '18 at 07:15
  • I just wrote it manually not pasted it from code :) Now I am editing it to true. – sayhan Oct 11 '18 at 11:06
  • Thank you guys for your supports we found solution and I shared it below. – sayhan Oct 11 '18 at 11:18

1 Answers1

1

I found a solution like below code :

Firstly when I call app.start() ,

start() function calls _checkWindowReady()

_checkWindowReady calls waitFor()

And finally waitFor calls _callClientAPI() and it looks for specific function and element.

 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++;
      }
    });
  }
sayhan
  • 1,168
  • 1
  • 16
  • 22