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

Commit 8b088fd

Browse files
bcaudanjuliemr
authored andcommitted
feat(locators): Added a By.cssContainingText locator.
This new locator find elements by css selector and inner text in response to the lack of ':contains' selector. Example: By.cssContainingText('ul .pet', 'Dog') will find all ul children with class 'pet' containing the text 'Dog'. Closes #488, Closes #709
1 parent 6f31b56 commit 8b088fd

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

lib/clientsidescripts.js

+23
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,29 @@ clientSideScripts.findTextareas = function(model, using) {
471471
}
472472
};
473473

474+
/**
475+
* Find elements by css selector and textual content.
476+
*
477+
* @param {string} cssSelector The css selector to match.
478+
* @param {string} searchText The exact text to match.
479+
* @param {Element} using The scope of the search. Defaults to 'document'.
480+
*
481+
* @return {Array.<Element>} An array of matching elements.
482+
*/
483+
clientSideScripts.findByCssContainingText = function(cssSelector, searchText, using) {
484+
var using = using || document;
485+
var elements = using.querySelectorAll(cssSelector);
486+
var matches = [];
487+
for (var i = 0; i < elements.length; ++i) {
488+
var element = elements[i];
489+
var elementText = element.innerText || element.textContent;
490+
if (elementText.indexOf(searchText) > -1) {
491+
matches.push(element);
492+
}
493+
}
494+
return matches;
495+
};
496+
474497
/**
475498
* Tests whether the angular global variable is present on a page. Retries
476499
* in case the page is just loading slowly.

lib/locators.js

+24
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,28 @@ ProtractorBy.prototype.repeater = function(repeatDescriptor) {
321321
};
322322
};
323323

324+
/**
325+
* Find elements by CSS which contain a certain string.
326+
*
327+
* @view
328+
* <ul>
329+
* <li class="pet">Dog</li>
330+
* <li class="pet">Cat</li>
331+
* </ul>
332+
*
333+
* @example
334+
* // Returns the DIV for the dog, but not cat.
335+
* var dog = element(by.cssContainingText('.pet', 'Dog'));
336+
*/
337+
ProtractorBy.prototype.cssContainingText = function(cssSelector, searchText) {
338+
return {
339+
findElementsOverride: function(driver, using) {
340+
return driver.findElements(
341+
webdriver.By.js(clientSideScripts.findByCssContainingText,
342+
cssSelector, searchText, using));
343+
},
344+
message: 'by.cssContainingText("' + cssSelector + '", "' + searchText + '")'
345+
};
346+
};
347+
324348
exports.ProtractorBy = ProtractorBy;

spec/basic/findelements_spec.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ describe('locators', function() {
336336
});
337337

338338
it('should find an individual element', function() {
339-
browser.debugger();
340339
var firstInitial = element(
341340
by.repeater('bloop in days').row(0).column('bloop.initial'));
342341

@@ -345,6 +344,16 @@ describe('locators', function() {
345344
});
346345
});
347346

347+
describe('by css containing text', function () {
348+
it('should find elements by css and partial text', function () {
349+
element.all(by.cssContainingText('#inner ul .pet', 'dog')).then(function(arr) {
350+
expect(arr.length).toEqual(2);
351+
expect(arr[0].getAttribute('id')).toBe('bigdog');
352+
expect(arr[1].getAttribute('id')).toBe('smalldog');
353+
});
354+
});
355+
});
356+
348357
it('should determine if an element is present', function() {
349358
expect(browser.isElementPresent(by.binding('greet'))).toBe(true);
350359
expect(browser.isElementPresent(by.binding('nopenopenope'))).toBe(false);

testapp/form/form.html

+10
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,13 @@ <h4>Buttons</h4>
7272
<input type="submit" value="Exact text" id="submitbutton"/>
7373
<input type="button" value="Hello text" id="inputbutton"/>
7474
</div>
75+
76+
<div id="inner">
77+
<h4>Inner text</h4>
78+
<ul>
79+
<li class="pet" id="bigdog">Big dog</li>
80+
<li class="pet" id="smalldog">Small dog</li>
81+
<li class="notpet" id="otherdog">Other dog</li>
82+
<li class="pet" id="cat">Cat</li>
83+
</ul>
84+
</div>

0 commit comments

Comments
 (0)