Tree55Topz Tree55Topz - 25 days ago 10
Javascript Question

Verifying amount of elements using expect

So I have an element identified

var errorAlert = element.all('[id*="type-alert"]');


I am trying to create a function that will verify there are 2 alerts displayed..

this.isAlertsDisplayed = function() {
return expect(errorAlert.count()).toEqual(2);
};


I keep gettin a friken typeError : .toEqual is not a function when I have seen this example work for others.. What am I doing wrong?

Answer

@Danny is on the right track. Except that you are simply missing the by.css() part, replace:

var errorAlert = element.all('[id*="type-alert"]');

with:

var errorAlert = element.all(by.css('[id*="type-alert"]'));

Or with:

var errorAlert = $$('[id*="type-alert"]');

Note that you can spot this kind of problems much earlier in the process - statically even before executing your tests and trying to figure out what went wrong. If you would use ESLint and eslint-plugin-protractor plugin (>=1.29.0), the valid-locator-type rule would warn you if you have passed a literal to element() or element.all(), or if you have passed a "by" locator to $() or $$().

And, if you have ESLint configured in your IDE of choice, here is how you would get the warning during the live-coding (example from PyCharm):

enter image description here