Search code examples
selenium-webdriverasync-awaitprotractor

Protractor not failing current spec after expected condition error


When executing specs on protractor, I have noticed that some of my conditions when failed, the spec is marked as pass but it should fail. I'm probably doing something wrong with my async/await method and need someone to point out to me why I'm not getting the spec as fail and how to fix it.

enter image description here

static async SelectDocument(){
try{
  let docXpath = 'this is an error on purpose. it is not showing as fail in the specs. Why?';
  let docElement = element(by.xpath(docXpath));
  await browser.wait(.EC.vidibilityOf(docElement),3000);//code stops here but spec is not mark as fail.
  await browser.sleep(1000);
  docElement.click();
 }catch(e){
  await logger.log().error(e)}
 }
}

The output: enter image description here enter image description here


Solution

  • Thanks to @Gaurav Lad, I was able to come with a solution.

    I was not aware that as mentioned by him above: "Script reports failure on any Assertion failure and not statement failure."

    Thanks to this comment I was able to wrap the expected condition of visibility and then make an assertion that successfully fails the spec. No more silent errors on my tests!.

        static async ExpectVisibilityByElement(Elem: ElementFinder, waitTime?: number) {
      {
        let wait = 3000
        if (waitTime != undefined) {
          wait = waitTime;
        }
        let output;
        let errorObject;
        try {
            await browser.wait(EC.visibilityOf(Elem), wait);
            await logger.Log().info("Element " + Elem.locator() + " is visible.");
            output = true;
        } catch (error) {
            output = false;
            errorObject = error;
            await logger.Log().error(error);
        }
        expect(output).toBe(true,Elem.locator()+ 'is not visible.')
        if(!output) {
          await logger.Log().error('SPEC FAILED. The rest of the spec will not be excecuted.');
          throw errorObject.name +': '+ errorObject.message;
        }
      }
    };