Search code examples
javascriptnode.jsselenium-webdriverwebdriverwebdriver-io

Return true or false on waituntil method webdriver.io


I am writing automation framework using webdriver.io v5. I would like to get a boolean response from the following method.

waitAndCheckForContactToBePresent(contactName) {
        return browser.waitUntil((value) => {
            return this.checkIfContactExists(contactName).firstName === contactName
        }, 240000, 'Contact not found', 60000);
}

Currently the test fails with the following error

Contact not found
[chrome  mac os x #0-0] Error: Contact not found

I would like to assert on the method response. How I can get a boolean response.


Solution

  • waitUntil returns true on success. But when it fails, it throws an error and prints the message you provide. But you could achieve what you are looking for by catching the error thrown on failure. Please check if the below works for you.

    waitAndCheckForContactToBePresent(contactName){
      let status;
      try {
        status = browser.waitUntil((value) => {
          return this.checkIfContactExists(contactName).firstName === contactName
        }, 240000, 'Contact not found', 60000);
      }
      catch (error) {
        status = false;
      };
      return status;
    }