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

Commit

Permalink
Merge pull request #812 from hankduan/exactBinding
Browse files Browse the repository at this point in the history
feat(locators): Add By.exactBinding locator
  • Loading branch information
hankduan committed May 17, 2014
2 parents cbcdb48 + 65844fb commit 98f4ba5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
15 changes: 12 additions & 3 deletions lib/clientsidescripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,30 @@ clientSideScripts.waitForAngular = function(selector, callback) {
* Find a list of elements in the page by their angular binding.
*
* @param {string} binding The binding, e.g. {{cat.name}}.
* @param {boolean} exactMatch Whether the binding needs to be matched exactly
* @param {Element} using The scope of the search.
*
* @return {Array.<Element>} The elements containing the binding.
*/
clientSideScripts.findBindings = function(binding, using) {
clientSideScripts.findBindings = function(binding, exactMatch, using) {
using = using || document;
var bindings = using.getElementsByClassName('ng-binding');
var matches = [];
for (var i = 0; i < bindings.length; ++i) {
var dataBinding = angular.element(bindings[i]).data('$binding');
if(dataBinding) {
var bindingName = dataBinding.exp || dataBinding[0].exp || dataBinding;
if (bindingName.indexOf(binding) != -1) {
matches.push(bindings[i]);
if (exactMatch) {
var matcher = new RegExp('([^a-zA-Z\d]|$)' + binding + '([^a-zA-Z\d]|^)');
if (matcher.test(bindingName)) {
matches.push(bindings[i]);
}
} else {
if (bindingName.indexOf(binding) != -1) {
matches.push(bindings[i]);
}
}

}
}
return matches; /* Return the whole array for webdriver.findElements. */
Expand Down
22 changes: 21 additions & 1 deletion lib/locators.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,32 @@ ProtractorBy.prototype.binding = function(bindingDescriptor) {
findElementsOverride: function(driver, using) {
return driver.findElements(
webdriver.By.js(clientSideScripts.findBindings,
bindingDescriptor, using));
bindingDescriptor, false, using));
},
message: 'by.binding("' + bindingDescriptor + '")'
};
};

/**
* Find an element by exact binding.
*
* @alias by.exactBinding()
* Same as by.binding() except this does not allow for partial matches
*
* @param {string} bindingDescriptor
* @return {{findElementsOverride: findElementsOverride, message: string}}
*/
ProtractorBy.prototype.exactBinding = function(bindingDescriptor) {
return {
findElementsOverride: function(driver, using) {
return driver.findElements(
webdriver.By.js(clientSideScripts.findBindings,
bindingDescriptor, true, using));
},
message: 'by.exactBinding("' + bindingDescriptor + '")'
};
};

/**
* @deprecated Use 'model' instead.
*
Expand Down
12 changes: 12 additions & 0 deletions spec/basic/findelements_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ describe('locators', function() {
expect(greeting.getText()).toEqual('Hiya');
});

it('should find exact match by exactBinding', function() {
var greeting = element(by.exactBinding('greeting'));

expect(greeting.getText()).toEqual('Hiya');
});

it('should not find partial match by exactBinding', function() {
var greeting = element(by.exactBinding('greet'));

expect(greeting.isPresent()).toBe(false);
});

it('should find an element by binding with ng-bind attribute',
function() {
var name = element(by.binding('username'));
Expand Down

0 comments on commit 98f4ba5

Please sign in to comment.