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

Commit

Permalink
Adding support for isElementPresent with Protractor locators.
Browse files Browse the repository at this point in the history
Closes #11.
  • Loading branch information
juliemr committed Sep 4, 2013
1 parent 8329b01 commit cfc6438
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
32 changes: 32 additions & 0 deletions lib/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ Protractor.prototype.wrapWebElement = function(element) {

var originalFindElement = element.findElement;
var originalFindElements = element.findElements;
var originalIsElementPresent = element.isElementPresent;

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

/**
* @see webdriver.WebElement.isElementPresent
* @return {!webdriver.promise.Promise} A promise that will be resolved with
* whether an element could be located on the page.
*/
element.isElementPresent = function(locator, varArgs) {
thisPtor.waitForAngular();
if (locator.findArrayOverride) {
return locator.findArrayOverride(element.getDriver(), element).
then(function (arr) {
return !!arr.length;
});
}
return originalIsElementPresent.apply(element, arguments);
};

/**
* Evalates the input as if it were on the scope of the current element.
* @param {string} expression
Expand Down Expand Up @@ -448,6 +465,21 @@ Protractor.prototype.findElements = function(locator, varArgs) {
return this.driver.findElements(locator, varArgs);
};

/**
* Tests if an element is present on the page.
* @see webdriver.WebDriver.isElementPresent
* @return {!webdriver.promise.Promise} A promise that will resolve to whether
* the element is present on the page.
*/
Protractor.prototype.isElementPresent = function(locatorOrElement, varArgs) {
if (locatorOrElement.findArrayOverride) {
return locatorOrElement.findArrayOverride(this.driver).then(function(arr) {
return !!arr.length;
});
}
return this.driver.isElementPresent(locatorOrElement, varArgs);
};

/**
* Add a module to load before Angular whenever Protractor.get is called.
* Modules will be registered after existing modules already on the page,
Expand Down
17 changes: 16 additions & 1 deletion spec/findelements_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ describe('finding elements', function() {
column('qux'));
expect(byCol.getText()).toEqual('Wed');
});

it('should determine if an element is present', function() {
expect(ptor.isElementPresent(protractor.By.binding('greet'))).toBe(true);
expect(ptor.isElementPresent(protractor.By.binding('nopenopenope'))).
toBe(false);
});
});

describe('further examples', function() {
Expand Down Expand Up @@ -204,7 +210,6 @@ describe('finding elements', function() {

it('should find multiple elements scoped properly with chaining',
function() {
ptor.debugger();
ptor.findElements(protractor.By.binding('item')).then(function(elems) {
expect(elems.length).toEqual(4);
});
Expand All @@ -214,6 +219,16 @@ describe('finding elements', function() {
expect(elems.length).toEqual(2);
});
});

it('should determine element presence properly with chaining', function() {
expect(ptor.findElement(protractor.By.id('baz')).
isElementPresent(protractor.By.binding('item.resuedBinding'))).
toBe(true);

expect(ptor.findElement(protractor.By.id('baz')).
isElementPresent(protractor.By.binding('nopenopenope'))).
toBe(false);
})
});

describe('evaluating statements', function() {
Expand Down

0 comments on commit cfc6438

Please sign in to comment.