1

I'm dealing with below element and having trouble with getting the error message text in to a variable.

<mat-error _ngcontent-c16="" class="mat-error ng-star-inserted" id="error-email-required" role="alert" style=""> Email is a required field </mat-error>

Catching the element is straight forward.

var emailReqiredLabel = element(by.id('error-email-required'));

I tried below options but all printing as >> visible error message is : [object Object]

  • var errormsg = emailReqiredLabel.getText().toString();

  • var errormsg = emailReqiredLabel.getAttribute('value').toString();

    console.log("visible error message is : " + errormsg );

can someone advice please.

Community
  • 1
  • 1
spe
  • 51
  • 1
  • 8

1 Answers1

0

mat-error does not show up by default. It only shows when the input is touched. So before getting the text try to click on the field and try getText(). If that does not work try JSON.Stringify(errormsg).

Madhan Raj
  • 1,404
  • 1
  • 7
  • 13
  • You are correct, I'm getting this at a validation point, at the time of execution this validation message is visible , when I try you'r solution I m getting "Failed: Converting circular structure to JSON". any clue on this – spe Sep 17 '18 at 09:52
  • Did u try `getText()` after clicking and try to console the text. – Madhan Raj Sep 18 '18 at 07:17
  • `npm install --save circular-json` run this in your project location. And import the same in your spec file. then use`CircularJSON.stringify(errormsg);` – Madhan Raj Sep 18 '18 at 07:21