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

Commit a62efc6

Browse files
joeheymingqiyigg
authored andcommitted
feat(locators): Add support for regex in cssContainingText (#4532)
* feat(locators): 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.
1 parent 95dd3ca commit a62efc6

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

lib/clientsidescripts.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -676,20 +676,28 @@ 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 or 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+
var elementMatches = searchText instanceof RegExp ?
697+
searchText.test(elementText) :
698+
elementText.indexOf(searchText) > -1;
699+
700+
if (elementMatches) {
693701
matches.push(element);
694702
}
695703
}

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[]> => {

spec/basic/locators_spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,26 @@ 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+
366+
it('should find elements with a regex with no flags', function() {
367+
// this test matches the non-transformed text.
368+
// the text is actually transformed with css,
369+
// so you can't match the Node innerText or textContent.
370+
element.all(by.cssContainingText('#transformedtext div', /Uppercase/))
371+
.then(function(found) {
372+
expect(found.length).toEqual(1);
373+
expect(found[0].getText()).toBe('UPPERCASE');
374+
});
375+
});
356376
});
357377

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

0 commit comments

Comments
 (0)