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

Commit 6cdd01d

Browse files
committed
Allow for partial matches with ng-repeat and ng-binding, since typing out
{{}} and filter options is quite annoying. Closes #4
1 parent ab8d6a5 commit 6cdd01d

File tree

3 files changed

+87
-20
lines changed

3 files changed

+87
-20
lines changed

protractor.js

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ clientSideScripts.findBinding = function() {
3737
var matches = [];
3838
var binding = arguments[0];
3939
for (var i = 0; i < bindings.length; ++i) {
40-
if (angular.element(bindings[i]).data().$binding == binding) {
41-
matches.push(bindings[i]);
42-
} else if (angular.element(bindings[i]).data().$binding[0].exp == binding) {
40+
var bindingName = angular.element(bindings[i]).data().$binding[0].exp ||
41+
angular.element(bindings[i]).data().$binding;
42+
if (bindingName.indexOf(binding) != -1) {
4343
matches.push(bindings[i]);
4444
}
4545
}
@@ -58,15 +58,37 @@ clientSideScripts.findBindings = function() {
5858
var matches = [];
5959
var binding = arguments[0];
6060
for (var i = 0; i < bindings.length; ++i) {
61-
if (angular.element(bindings[i]).data().$binding == binding) {
62-
matches.push(bindings[i]);
63-
} else if (angular.element(bindings[i]).data().$binding[0].exp == binding) {
61+
var bindingName = angular.element(bindings[i]).data().$binding[0].exp ||
62+
angular.element(bindings[i]).data().$binding;
63+
if (bindingName.indexOf(binding) != -1) {
6464
matches.push(bindings[i]);
6565
}
6666
}
6767
return matches; // Return the whole array for webdriver.findElements.
6868
};
6969

70+
/**
71+
* Find a row within an ng-repeat.
72+
*
73+
* arguments[0] {string} The text of the repeater, e.g. 'cat in cats'
74+
* arguments[1] {number} The row index
75+
*
76+
* @return {Element} The row element
77+
*/
78+
clientSideScripts.findRepeaterRow = function() {
79+
var repeater = arguments[0];
80+
var index = arguments[1];
81+
82+
var rows = [];
83+
var repeatElems = document.querySelectorAll('[ng-repeat]');
84+
for (var i = 0; i < repeatElems.length; ++i) {
85+
if (repeatElems[i].getAttribute('ng-repeat').indexOf(repeater) != -1) {
86+
rows.push(repeatElems[i]);
87+
}
88+
}
89+
return rows[index - 1];
90+
};
91+
7092
/**
7193
* Find an element within an ng-repeat by its row and column.
7294
*
@@ -82,12 +104,19 @@ clientSideScripts.findRepeaterElement = function() {
82104
var index = arguments[1];
83105
var binding = arguments[2];
84106

85-
var rows = document.querySelectorAll('[ng-repeat="'
86-
+ repeater + '"]');
107+
var rows = [];
108+
var repeatElems = document.querySelectorAll('[ng-repeat]');
109+
for (var i = 0; i < repeatElems.length; ++i) {
110+
if (repeatElems[i].getAttribute('ng-repeat').indexOf(repeater) != -1) {
111+
rows.push(repeatElems[i]);
112+
}
113+
}
87114
var row = rows[index - 1];
88115
var bindings = row.getElementsByClassName('ng-binding');
89116
for (var i = 0; i < bindings.length; ++i) {
90-
if (angular.element(bindings[i]).data().$binding[0].exp == binding) {
117+
var bindingName = angular.element(bindings[i]).data().$binding[0].exp ||
118+
angular.element(bindings[i]).data().$binding;
119+
if (bindingName.indexOf(binding) != -1) {
91120
matches.push(bindings[i]);
92121
}
93122
}
@@ -108,12 +137,19 @@ clientSideScripts.findRepeaterElement = function() {
108137
var repeater = arguments[0];
109138
var binding = arguments[1];
110139

111-
var rows = document.querySelectorAll('[ng-repeat="'
112-
+ repeater + '"]');
140+
var rows = [];
141+
var repeatElems = document.querySelectorAll('[ng-repeat]');
142+
for (var i = 0; i < repeatElems.length; ++i) {
143+
if (repeatElems[i].getAttribute('ng-repeat').indexOf(repeater) != -1) {
144+
rows.push(repeatElems[i]);
145+
}
146+
}
113147
for (var i = 0; i < rows.length; ++i) {
114148
var bindings = rows[i].getElementsByClassName('ng-binding');
115149
for (var j = 0; j < bindings.length; ++j) {
116-
if (angular.element(bindings[j]).data().$binding[0].exp == binding) {
150+
var bindingName = angular.element(bindings[j]).data().$binding[0].exp ||
151+
angular.element(bindings[j]).data().$binding;
152+
if (bindingName.indexOf(binding) != -1) {
117153
matches.push(bindings[j]);
118154
}
119155
}
@@ -346,9 +382,11 @@ ProtractorBy.prototype.repeater = function(repeatDescriptor) {
346382
return {
347383
row: function(index) {
348384
return {
349-
using: 'css selector',
350-
value: '[ng-repeat="' + repeatDescriptor
351-
+ '"]:nth-of-type(' + index + ')',
385+
findOverride: function(driver) {
386+
return driver.findElement(
387+
webdriver.By.js(clientSideScripts.findRepeaterRow),
388+
repeatDescriptor, index);
389+
},
352390
column: function(binding) {
353391
return {
354392
findOverride: function(driver) {

spec/testAppSpec.js

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ describe('test application', function() {
4141
});
4242
});
4343

44+
it('should find a binding by partial match', function(done) {
45+
catchPromiseErrors(done);
46+
ptor.findElement(protractor.By.binding('greet')).
47+
getText().then(function(text) {
48+
expect(text).toEqual('Hiya');
49+
done();
50+
});
51+
})
52+
4453
it('should find an element by binding with attribute', function(done) {
4554
catchPromiseErrors(done);
4655
ptor.findElement(protractor.By.binding('username')).
@@ -78,13 +87,36 @@ describe('test application', function() {
7887
});
7988
});
8089

90+
it('should find a repeater by partial match', function(done) {
91+
catchPromiseErrors(done);
92+
ptor.findElement(
93+
protractor.By.repeater('baz in days | filter:\'T\'').
94+
row(1).column('{{baz}}')).
95+
getText().then(function(text) {
96+
expect(text).toEqual('Tue');
97+
});
98+
99+
ptor.findElement(
100+
protractor.By.repeater('baz in days').row(1).column('b')).
101+
getText().then(function(text) {
102+
expect(text).toEqual('Tue');
103+
});
104+
105+
ptor.findElement(
106+
protractor.By.repeater('baz in days').row(1)).
107+
getText().then(function(text) {
108+
expect(text).toEqual('Tue');
109+
done();
110+
});
111+
});
112+
81113
xit('should find a repeater using data-ng-repeat', function(done) {
82114
catchPromiseErrors(done);
83115
ptor.findElement(protractor.By.repeater('day in days').row(3)).
84116
getText().then(function(text) {
85117
expect(text).toEqual('Wed');
86-
done();
87118
});
119+
88120
ptor.findElement(protractor.By.repeater('day in days').row(3).
89121
column('day')).
90122
getText().then(function(text) {
@@ -98,7 +130,6 @@ describe('test application', function() {
98130
ptor.findElement(protractor.By.repeater('bar in days').row(3)).
99131
getText().then(function(text) {
100132
expect(text).toEqual('Wed');
101-
done();
102133
});
103134
ptor.findElement(protractor.By.repeater('bar in days').row(3).
104135
column('bar')).
@@ -113,7 +144,6 @@ describe('test application', function() {
113144
ptor.findElement(protractor.By.repeater('foo in days').row(3)).
114145
getText().then(function(text) {
115146
expect(text).toEqual('Wed');
116-
done();
117147
});
118148
ptor.findElement(protractor.By.repeater('foo in days').row(3)).
119149
column('foo').
@@ -128,7 +158,6 @@ describe('test application', function() {
128158
ptor.findElement(protractor.By.repeater('qux in days').row(3)).
129159
getText().then(function(text) {
130160
expect(text).toEqual('Wed');
131-
done();
132161
});
133162
ptor.findElement(protractor.By.repeater('qux in days').row(3)).
134163
column('quz').

testapp/app/partials/form.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</div>
1010
<input ng-model="show" type="checkbox"/> Show?
1111
<span id="shower" ng-show="show">Shown!!</span>
12-
<ul><li ng-repeat="baz in days | filter:'T'">{{baz}}</li></ul>
12+
<ul><li ng-repeat="baz in days | filter:'T'"><span>{{baz}}</span></li></ul>
1313
<ul><li data-ng-repeat="day in days">{{day}}</li></ul>
1414
<ul><li ng:repeat="bar in days">{{bar}}</li></ul>
1515
<ul><li ng_repeat="foo in days">{{foo}}</li></ul>

0 commit comments

Comments
 (0)