diff --git a/lib/expectedConditions.ts b/lib/expectedConditions.ts index 43d399fb9..f3116bab6 100644 --- a/lib/expectedConditions.ts +++ b/lib/expectedConditions.ts @@ -1,6 +1,9 @@ import {error as wderror} from 'selenium-webdriver'; + import {ProtractorBrowser} from './browser'; -import {ElementFinder} from './element'; +import {ElementArrayFinder, ElementFinder} from './element'; +import {element} from './index'; +import {Locator} from './locators'; import {falseIfMissing, passBoolean} from './util'; /** @@ -434,4 +437,23 @@ export class ProtractorExpectedConditions { return elementFinder.isSelected().then(passBoolean, falseIfMissing); }); } + + /** + * An expectation for checking the number of elements with given locator + * + * @example + * var EC = protractor.ExpectedConditions; + * browser.wait(EC.numberOfElementsToBe(by.repeater('cat in pets'), 3), 5000); + * + * @param {Locator} locator The locator to be used + * @param {number} numberElem The expected number of elements + * @returns {!function} An expected condition that returns a promise + * representing whether the size of elements list is equal to defined. + */ + numberOfElementsToBe(locator: Locator, numberElem: number): Function { + return () => { + let elemArray: ElementArrayFinder = element.all(locator); + return elemArray.count().then((count) => count === numberElem); + }; + } } diff --git a/spec/basic/expected_conditions_spec.js b/spec/basic/expected_conditions_spec.js index e91155070..3c0bc5307 100644 --- a/spec/basic/expected_conditions_spec.js +++ b/spec/basic/expected_conditions_spec.js @@ -176,6 +176,12 @@ describe('expected conditions', function() { expect(EC.or(EC.not(valid), EC.and(valid, invalid)).call()).toBe(false); }); + it('should have numberOfElementsToBe', function() { + var loc = by.model('username'); + expect(EC.numberOfElementsToBe(loc, 1).call()).toBe(true); + expect(EC.numberOfElementsToBe(loc, 0).call()).toBe(false); + }); + describe('for forked browsers', function() { // ensure that we can run EC on forked browser instances it('should have alertIsPresent', function() {