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

Commit b501ceb

Browse files
Jeremy Morony & Clark Cutlerjuliemr
Jeremy Morony & Clark Cutler
authored andcommitted
fix(findElements): Consistently include evaluate.
When using findElements with a css locator, wrap the returned list of elements with protractor specific functionality.
1 parent 00247fa commit b501ceb

File tree

3 files changed

+131
-15
lines changed

3 files changed

+131
-15
lines changed

lib/protractor.js

+40-14
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,15 @@ Protractor.prototype.wrapWebElement = function(element) {
152152
*/
153153
element.findElement = function(locator, varArgs) {
154154
thisPtor.waitForAngular();
155+
156+
var found;
155157
if (locator.findOverride) {
156-
return locator.findOverride(element.getDriver(), element);
158+
found = locator.findOverride(element.getDriver(), element);
159+
} else {
160+
found = originalFindElement.apply(element, arguments);
157161
}
158-
return originalFindElement.apply(element, arguments);
162+
163+
return thisPtor.wrapWebElement(found);
159164
};
160165

161166
/**
@@ -165,10 +170,21 @@ Protractor.prototype.wrapWebElement = function(element) {
165170
*/
166171
element.findElements = function(locator, varArgs) {
167172
thisPtor.waitForAngular();
173+
174+
var found;
168175
if (locator.findArrayOverride) {
169-
return locator.findArrayOverride(element.getDriver(), element);
176+
found = locator.findArrayOverride(element.getDriver(), element);
177+
} else {
178+
found = originalFindElements.apply(element, arguments);
170179
}
171-
return originalFindElements.apply(element, arguments);
180+
181+
return found.then(function(elems) {
182+
for (var i = 0; i < elems.length; ++i) {
183+
thisPtor.wrapWebElement(elems[i]);
184+
}
185+
186+
return elems;
187+
});
172188
}
173189

174190
/**
@@ -212,11 +228,16 @@ Protractor.prototype.wrapWebElement = function(element) {
212228
* @return {!webdriver.WebElement}
213229
*/
214230
Protractor.prototype.findElement = function(locator, varArgs) {
231+
var found;
215232
this.waitForAngular();
233+
216234
if (locator.findOverride) {
217-
return this.wrapWebElement(locator.findOverride(this.driver));
235+
found = locator.findOverride(this.driver);
236+
} else {
237+
found = this.driver.findElement(locator, varArgs);
218238
}
219-
return this.wrapWebElement(this.driver.findElement(locator, varArgs));
239+
240+
return this.wrapWebElement(found);
220241
};
221242

222243
/**
@@ -226,17 +247,22 @@ Protractor.prototype.findElement = function(locator, varArgs) {
226247
* array of the located {@link webdriver.WebElement}s.
227248
*/
228249
Protractor.prototype.findElements = function(locator, varArgs) {
229-
var self = this;
250+
var self = this, found;
230251
this.waitForAngular();
252+
231253
if (locator.findArrayOverride) {
232-
return locator.findArrayOverride(this.driver).then(function(elems) {
233-
for (var i = 0; i < elems.length; ++i) {
234-
self.wrapWebElement(elems[i]);
235-
}
236-
return elems;
237-
});
254+
found = locator.findArrayOverride(this.driver);
255+
} else {
256+
found = this.driver.findElements(locator, varArgs);
238257
}
239-
return this.driver.findElements(locator, varArgs);
258+
259+
return found.then(function(elems) {
260+
for (var i = 0; i < elems.length; ++i) {
261+
self.wrapWebElement(elems[i]);
262+
}
263+
264+
return elems;
265+
});
240266
};
241267

242268
/**

spec/findelements_spec.js

+90
Original file line numberDiff line numberDiff line change
@@ -278,5 +278,95 @@ describe('finding elements', function() {
278278
// Make sure it works with a promise expectation.
279279
expect(element.evaluate('planet.radius')).toEqual(1516);
280280
});
281+
282+
describe('when wrapping all elements', function() {
283+
describe('when querying using a locator that specifies an override', function() {
284+
it('should wrap the results', function() {
285+
ptor.findElements(protractor.By.binding('planet.name')).then(function(elements) {
286+
for (var i = 0; i < elements.length; i++) {
287+
expect(typeof elements[i].evaluate).toBe('function');
288+
}
289+
});
290+
});
291+
});
292+
293+
describe('when querying using a locator that does not specify an override', function() {
294+
it('should wrap the results', function() {
295+
ptor.findElements(protractor.By.css('option[value="4"]')).then(function(elements) {
296+
for (var i = 0; i < elements.length; i++) {
297+
expect(typeof elements[i].evaluate).toBe('function');
298+
}
299+
});
300+
});
301+
});
302+
});
303+
304+
describe('when querying against a found element', function() {
305+
var info;
306+
307+
beforeEach(function() {
308+
info = ptor.findElement(protractor.By.css('.planet-info'));
309+
});
310+
311+
describe('when querying for a single element', function() {
312+
describe('when ng using a locator that specifies an override', function() {
313+
var planetName;
314+
315+
beforeEach(function() {
316+
planetName = info.findElement(protractor.By.binding('planet.name'));
317+
});
318+
319+
it('should wrap the result', function() {
320+
expect(typeof planetName.evaluate).toBe("function")
321+
});
322+
});
323+
324+
describe('when querying using a locator that does not specify an override', function() {
325+
var moons;
326+
327+
beforeEach(function() {
328+
moons = info.findElement(protractor.By.css('div:last-child'));
329+
});
330+
331+
it('should wrap the result', function() {
332+
expect(typeof moons.evaluate).toBe("function")
333+
});
334+
});
335+
});
336+
337+
describe('when querying for many elements', function() {
338+
describe('when using a locator that specifies an override', function() {
339+
var planetName;
340+
341+
beforeEach(function() {
342+
planetName = info.findElements(protractor.By.binding('planet.name'));
343+
});
344+
345+
it('should wrap the result', function() {
346+
planetName.then(function(result) {
347+
for (var i = 0; i < result.length; ++i) {
348+
expect(typeof result[i].evaluate).toBe("function");
349+
}
350+
});
351+
});
352+
});
353+
354+
describe('when querying using a locator that does not specify an override', function() {
355+
var moons;
356+
357+
beforeEach(function() {
358+
moons = info.findElements(protractor.By.css('div:last-child'));
359+
});
360+
361+
it('should wrap the result', function() {
362+
moons.then(function(result) {
363+
for (var i = 0; i < result.length; ++i) {
364+
expect(typeof result[i].evaluate).toBe("function");
365+
}
366+
});
367+
});
368+
});
369+
});
370+
});
281371
});
282372
});

testapp/app/partials/bindings.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<div>Details for:
66
<select ng-model="planet" ng-options="planet.name for planet in planets">
77
</select>
8-
<div>
8+
<div class="planet-info">
99
<div>{{planet.name}}</div>
1010
<div>Radius: {{getRadiusKm()}}km</div>
1111
<div>Moons: <span ng-repeat="moon in planet.moons">{{moon}}</span></div>

0 commit comments

Comments
 (0)