6

I need to verify the text label but it contains dynamic part, so I try to use regex but it doesn't work.

expect(aboutPage.userInterfaceText.getText()).toMatch('/- User Interface: v \d+\.\d+\.\d+/');

I always get next error:

- Expected '- User Interface: v 4.4.63' to match '/- User Interface: v d+.d+.d+/'.
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
iPogosov
  • 367
  • 1
  • 6
  • 15

2 Answers2

15

- Expected '- User Interface: v 4.4.63' to match '/- User Interface: v d+.d+.d+/'.

As you can see the slashes at both ends of the pattern are also included into the expression, but your - User Interface: v 4.4.63 test string does not contain the slashes.

You should not enclose the regular expression in the single quotes to make it a valid regular expression object:

expect(aboutPage.userInterfaceText.getText()).toMatch(/- User Interface: v \d+\.\d+\.\d+/);

Works for me on the console:

> var s = "- User Interface: v 4.4.63";
> var re = /- User Interface: v \d+\.\d+\.\d+/;
> s.match(re)
["- User Interface: v 4.4.63"]
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

You need to convert the pattern to string then you can use the match() to check the regex matchers

expect(control.errors.pattern.requiredPattern).toString().match('^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\S\./0-9]*$');

or like this

expect(control.errors.pattern.requiredPattern).toString().match(/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\S\./0-9]*$/);
Shabeer M
  • 160
  • 3
  • 9