Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.
24 changes: 23 additions & 1 deletion lib/expectedConditions.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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);
};
}
}
6 changes: 6 additions & 0 deletions spec/basic/expected_conditions_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down