javascriptelectronwebdriver-iospectron

How can I wait to electron while loading html


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();
 })

Solution

  • 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++;
          }
        });
      }