async-awaitprotractorwebdriver-iointercept

Trouble trying to use lib protractor-intercept : probably a Javascript async issue


I am having trouble trying to use lib protractor-intercept which I think is probably a Javascript async issue that I am having. This code works but it wont print out the console.log line. Any idea on how I might have got this wrong?

When('the {string} button is clicked', async (buttonName: string) => {
 var intercept = new Intercept(browser);
 intercept.addListener();
 await page.submitApplication(buttonName);
 intercept.getRequests().then(function(reqs) {
   console.log("Intercepted!! " + reqs);
   //TODO extract a value from submit http query response
 });
 await page.waitForSpinnerButtonFinish();
});

And here is my page object method:

  submitApplication(buttonName: string) {
    return new Promise(function (resolve) {
      var promised = element(by.css('app form')).element(
         by.cssContainingText('button span', buttonName)).click();
      return resolve(promised);
    });
  }

Solution

  • I didn't find documentation what intercept.addListener(); returns but to be safe I'd go with await. And then I wouldn't mix .then with await. See if that will do the work

    submitApplication(buttonName: string) {
        return element(by.css('app form'))
          .element(by.cssContainingText('button span', buttonName))
          .click();
        });
      }
    
    When('the {string} button is clicked', async (buttonName: string) => {
     var intercept = new Intercept(browser);
     await intercept.addListener();
     await page.submitApplication(buttonName);
     let reqs = await intercept.getRequests()
     console.log("Intercepted!! " + reqs);
     await page.waitForSpinnerButtonFinish();
    });