Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit cfc6438

Browse files
committed
Adding support for isElementPresent with Protractor locators.
Closes #11.
1 parent 8329b01 commit cfc6438

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

lib/protractor.js

+32
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ Protractor.prototype.wrapWebElement = function(element) {
365365

366366
var originalFindElement = element.findElement;
367367
var originalFindElements = element.findElements;
368+
var originalIsElementPresent = element.isElementPresent;
368369

369370
/**
370371
* @see webdriver.WebElement.findElement
@@ -391,6 +392,22 @@ Protractor.prototype.wrapWebElement = function(element) {
391392
return originalFindElements.apply(element, arguments);
392393
}
393394

395+
/**
396+
* @see webdriver.WebElement.isElementPresent
397+
* @return {!webdriver.promise.Promise} A promise that will be resolved with
398+
* whether an element could be located on the page.
399+
*/
400+
element.isElementPresent = function(locator, varArgs) {
401+
thisPtor.waitForAngular();
402+
if (locator.findArrayOverride) {
403+
return locator.findArrayOverride(element.getDriver(), element).
404+
then(function (arr) {
405+
return !!arr.length;
406+
});
407+
}
408+
return originalIsElementPresent.apply(element, arguments);
409+
};
410+
394411
/**
395412
* Evalates the input as if it were on the scope of the current element.
396413
* @param {string} expression
@@ -448,6 +465,21 @@ Protractor.prototype.findElements = function(locator, varArgs) {
448465
return this.driver.findElements(locator, varArgs);
449466
};
450467

468+
/**
469+
* Tests if an element is present on the page.
470+
* @see webdriver.WebDriver.isElementPresent
471+
* @return {!webdriver.promise.Promise} A promise that will resolve to whether
472+
* the element is present on the page.
473+
*/
474+
Protractor.prototype.isElementPresent = function(locatorOrElement, varArgs) {
475+
if (locatorOrElement.findArrayOverride) {
476+
return locatorOrElement.findArrayOverride(this.driver).then(function(arr) {
477+
return !!arr.length;
478+
});
479+
}
480+
return this.driver.isElementPresent(locatorOrElement, varArgs);
481+
};
482+
451483
/**
452484
* Add a module to load before Angular whenever Protractor.get is called.
453485
* Modules will be registered after existing modules already on the page,

spec/findelements_spec.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ describe('finding elements', function() {
123123
column('qux'));
124124
expect(byCol.getText()).toEqual('Wed');
125125
});
126+
127+
it('should determine if an element is present', function() {
128+
expect(ptor.isElementPresent(protractor.By.binding('greet'))).toBe(true);
129+
expect(ptor.isElementPresent(protractor.By.binding('nopenopenope'))).
130+
toBe(false);
131+
});
126132
});
127133

128134
describe('further examples', function() {
@@ -204,7 +210,6 @@ describe('finding elements', function() {
204210

205211
it('should find multiple elements scoped properly with chaining',
206212
function() {
207-
ptor.debugger();
208213
ptor.findElements(protractor.By.binding('item')).then(function(elems) {
209214
expect(elems.length).toEqual(4);
210215
});
@@ -214,6 +219,16 @@ describe('finding elements', function() {
214219
expect(elems.length).toEqual(2);
215220
});
216221
});
222+
223+
it('should determine element presence properly with chaining', function() {
224+
expect(ptor.findElement(protractor.By.id('baz')).
225+
isElementPresent(protractor.By.binding('item.resuedBinding'))).
226+
toBe(true);
227+
228+
expect(ptor.findElement(protractor.By.id('baz')).
229+
isElementPresent(protractor.By.binding('nopenopenope'))).
230+
toBe(false);
231+
})
217232
});
218233

219234
describe('evaluating statements', function() {

0 commit comments

Comments
 (0)