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

feat(locators): Add support for regex in cssContainingText #4532

Merged
merged 3 commits into from
Nov 6, 2017

Conversation

joeheyming
Copy link
Contributor

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.

This is related to #1360

@joeheyming
Copy link
Contributor Author

I looked at the continuous test failure, and the failures are unrelated to my changes.
I touched cssContainingText.
The test that failed, protractor/spec/ts/basic/element_spec.ts:177:3)
has nothing to do with cssContaining text. It is trying to fill out a form

@joeheyming
Copy link
Contributor Author

Not sure what to do with this:

running: node scripts/interactive_tests/with_base_url.js
.
/home/ubuntu/protractor/node_modules/q/q.js:155
throw e;
^
Did not start interactive server in 10s. Server startup output: [19:50:49] I/local - Starting selenium standalone server...
[19:50:51] I/local - Selenium standalone server started at http://172.17.197.126:44518/wd/hub

fail: Error: expecting exit code: 0, actual: 1
running: node node_modules/jasmine/bin/jasmine.js JASMINE_CONFIG_PATH=scripts/unit_test.json

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.
@joeheyming joeheyming changed the title Add support for regex in cssContainingText. feat(locators): Add support for regex in cssContainingText Oct 16, 2017
@joeheyming
Copy link
Contributor Author

Hey so... Ping.... echo... echo... echo... echoo........

@wswebcreation
Copy link
Contributor

wswebcreation commented Oct 31, 2017

Hi @joeheyming

Sorry for not reacting. I'll add someone from the core team to review this PR.

@vikerman and @qiyigg

Can you please take a look at this PR? Tnx in advance

* @param {Element} using The scope of the search.
*
* @return {Array.<Element>} An array of matching elements.
*/
functions.findByCssContainingText = function(cssSelector, searchText, using) {
using = using || document;

if (searchText.indexOf('__REGEXP__') === 0) {
var match = searchText.split('__REGEXP__')[1].match(/\/(.*)\/(.*)?/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure enough whether this match method works well here

Would you mind adding another test that using a regex with no regex flags, we can be more convinced of this change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, will do

@@ -676,20 +676,26 @@ functions.findByPartialButtonText = function(searchText, using) {
* Find elements by css selector and textual content.
*
* @param {string} cssSelector The css selector to match.
* @param {string} searchText The exact text to match.
* @param {string} searchText The exact text to match. Can be a serialized regex.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better if we say: searchText The exact text to match or a serialized regex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

if (elementText.indexOf(searchText) > -1) {

if ((searchText instanceof RegExp && searchText.test(elementText)) ||
elementText.indexOf(searchText) > -1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is "elementText.indexOf(searchText) > -1" should only be applied to "searchText is not a RegExp"
For now, if elementText doesn't satisfy searchText regex, "elementText.indexOf(searchText) > -1" will still be called

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no side-effects to calling indexOf. The way || works is that javascript will take the first true value.
So I don't think indexOf will be called.

Here is an example I tried out:
x = () => { console.log('here'); return true; }
y = () { console.log('there'); return false; }

z = x() || y()
here
true

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the "elementText.indexOf(searchText) > -1" won't evaluate if and only if the first expression return true; however, if searchText is a RegExp and searchText.test(elementText) return false, "(searchText instanceof RegExp && searchText.test(elementText)) " will return false and the remaining indexOf will still be evaluated even though searchText is a RegExp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I tried to address this issue. Let me know if you like the updated code.

fixed a comment, added a test.
Now, I compute the result of the client side match in a variable:
elementMatches

It uses a ternary to determine if we use a regex or indexOf for matching.
This make the matching more explicit.
@joeheyming
Copy link
Contributor Author

The last CircleCi failure was a pip install failure. Nothing to see here

@qiyigg qiyigg merged commit a62efc6 into angular:master Nov 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants