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

Commit a02b669

Browse files
committed
Add support for regex in cssContainingText.
In order to get this working, I had to serialize the regex before we send it over the wire to the browser. Since there is no standard way to do this, I took guidance from a stackoverflow answer, where they call toString on the regex. Then, on the browser, you use a regex to extract out the text in between /someregex/ The hard part is to also extract out the modifiers, like i for ignore case. Anyhow, this approach seems to work, and I have tests to prove it.
1 parent 862e8be commit a02b669

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

Diff for: lib/clientsidescripts.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -676,20 +676,26 @@ functions.findByPartialButtonText = function(searchText, using) {
676676
* Find elements by css selector and textual content.
677677
*
678678
* @param {string} cssSelector The css selector to match.
679-
* @param {string} searchText The exact text to match.
679+
* @param {string} searchText The exact text to match. Can be a serialized regex.
680680
* @param {Element} using The scope of the search.
681681
*
682682
* @return {Array.<Element>} An array of matching elements.
683683
*/
684684
functions.findByCssContainingText = function(cssSelector, searchText, using) {
685685
using = using || document;
686686

687+
if (searchText.indexOf('__REGEXP__') === 0) {
688+
var match = searchText.split('__REGEXP__')[1].match(/\/(.*)\/(.*)?/);
689+
searchText = new RegExp(match[1], match[2] || '');
690+
}
687691
var elements = using.querySelectorAll(cssSelector);
688692
var matches = [];
689693
for (var i = 0; i < elements.length; ++i) {
690694
var element = elements[i];
691695
var elementText = element.textContent || element.innerText || '';
692-
if (elementText.indexOf(searchText) > -1) {
696+
697+
if ((searchText instanceof RegExp && searchText.test(elementText)) ||
698+
elementText.indexOf(searchText) > -1) {
693699
matches.push(element);
694700
}
695701
}

Diff for: lib/locators.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -415,10 +415,11 @@ export class ProtractorBy extends WebdriverBy {
415415
* var dog = element(by.cssContainingText('.pet', 'Dog'));
416416
*
417417
* @param {string} cssSelector css selector
418-
* @param {string} searchString text search
418+
* @param {string|RegExp} searchString text search
419419
* @returns {ProtractorLocator} location strategy
420420
*/
421-
cssContainingText(cssSelector: string, searchText: string): ProtractorLocator {
421+
cssContainingText(cssSelector: string, searchText: string|RegExp): ProtractorLocator {
422+
searchText = (searchText instanceof RegExp) ? '__REGEXP__' + searchText.toString() : searchText;
422423
return {
423424
findElementsOverride: (driver: WebDriver, using: WebElement, rootSelector: string):
424425
wdpromise.Promise<WebElement[]> => {

Diff for: spec/basic/locators_spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,16 @@ describe('locators', function() {
353353
expect(element(by.cssContainingText('#transformedtext div', 'capitalize'))
354354
.getAttribute('id')).toBe('textcapitalize');
355355
});
356+
357+
it('should find elements with a regex', function() {
358+
element.all(by.cssContainingText('#transformedtext div', /(upper|lower)case/i))
359+
.then(function(found) {
360+
expect(found.length).toEqual(2);
361+
expect(found[0].getText()).toBe('UPPERCASE');
362+
expect(found[1].getText()).toBe('lowercase');
363+
});
364+
});
365+
356366
});
357367

358368
describe('by options', function() {

0 commit comments

Comments
 (0)