Ask a Question

Regular Expressions in JavaScript not working

SOLVED
bb1832j
Occasional Contributor

Regular Expressions in JavaScript not working

Hello,

I am trying to use the native Regular Expression with Javascript to find a particlar TextNode in a SVG object.  I have been trying the following code, but still do not get a matching result:

 

var sectionTest;
var sectionTest1;
var testText = "TextNode(5)";
var testText1 = "TextNode\(\d+\)";
var sectionRegExp = new RegExp(testText);
var sectionRegExp1 = new RegExp(testText1);

for(var tabLoop = 0; tabLoop < 1; tabLoop++)
{
broadAssetChartPath.Panel(tabLoop).Panel("chart*").Panel(0).SVG(0).Refresh();
sectionCount = broadAssetChartPath.Panel(tabLoop).Panel("chart*").Panel(0).SVG(0).ChildCount;

 

for(sectionLoop = 0; sectionLoop < sectionCount; sectionLoop++)
{
sectionCheck = broadAssetChartPath.Panel(tabLoop).Panel("chart*").Panel(0).SVG(0).Child(sectionLoop);
sectionTest = sectionRegExp.test(sectionCheck.Name);
sectionTest1 = sectionRegExp1.test(sectionCheck.Name);
Log.Message(sectionCheck.Name);
Log.Message(sectionTest);
Log.Message(sectionTest1);
if(sectionTest == true || sectionTest1 == true)
Log.Message(sectionCheck.Name + ":" + sectionCheck.contentText);
}
}

 

The Children of the SVG object have TextNodes with Names from 0-12 and with some "id_#".

 

I am not sure what I am doing wrong here.  As far as I can tell the RegExp is correct and should return a match.
Any help is welcomed.  Thanks.

8 REPLIES 8
tristaanogre
Esteemed Contributor

So, just to check... you're trying to find all the child objects from the SVG item that are TextNode, correct?  That, I'm assuming, is the "ObjectType" of the objects.

 

I'm not saying don't use regular expressions, but I don't see that it's necessary.  You can do the same, I believe, with the following.

 

    for(var tabLoop = 0; tabLoop < 1; tabLoop++){
        broadAssetChartPath.Panel(tabLoop).Panel("chart*").Panel(0).SVG(0).Refresh();
        sectionCount = broadAssetChartPath.Panel(tabLoop).Panel("chart*").Panel(0).SVG(0).ChildCount;
        for(sectionLoop = 0; sectionLoop < sectionCount; sectionLoop++){
            sectionCheck = broadAssetChartPath.Panel(tabLoop).Panel("chart*").Panel(0).SVG(0).Child(sectionLoop);
            Log.Message(sectionCheck.Name);
            if(sectionCheck.ObjectType = 'TextNode')
                Log.Message(sectionCheck.Name + ":" + sectionCheck.contentText);
        }
    }

Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
bb1832j
Occasional Contributor

I am not just looking for Child objects that are of TextNode ObjectType.  I am also looking for TextNode types that have an ObjectIndentifier of a digit(s) without any letters.  That is the reason I am trying to use a RegExp.

 

 

tristaanogre
Esteemed Contributor

You could add an additional clause to the if to check is a number. Use isNaN (https://www.w3schools.com/jsref/jsref_isnan.asp)and, if it returns false, then it's a numeric value.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available
bb1832j
Occasional Contributor

I will give that a try.

However, I do notice a pattern here.  Only recently  have I been having issues with the use of Regular Expressions in TestComplete, both native and non-native.  Each time when I would look through the posts on the community boards dealing with RegExp, the solutions (mostly from the mods) would almost exclusively steer the posters from using RegExp.  I know that the TestComplete docs state that we can use RegExp in our code and the many ways to do it.  If there is some issue with Regular Expressions in TestComplete, then we need to be notified and told how to workaround it.  For many of us, using Regular Expressions is a straightforward way to get what we need done.

Marsha_R
Champion Level 2

Most of the answers here are from volunteers and we're not mods.  Just so you know.  🙂

 

We steer users away from regular expressions if there's an easier way to do it in TC.  Many times users don't know that the TC options exist and we will always point you to the easiest way we know.  I would not count regular expressions as easy, and I would certainly not give them to a beginner.

 

If you want to spend all your time debugging a reg exp when you could have been done hours ago by using an easier TC function, go right ahead!  

 

eta:  You can express your doubts about the reg exp in TestComplete to support directly at this link.  

https://support.smartbear.com/message/?prod=TestComplete

AlexKaras
Champion Level 2

Hi,

 

TestComplete supports regular expressions as parameters for .FindXXX() methods and they worked fine for me.

For example:

https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/common-for-all/find-m...

and
oButton = obj.Parent.FindChild(["ObjectType", "contentText"], ["Link", "regexp:(Open)|(Download)|(Launch)"], 5);

control = root.FindChild('WPFControlAutomationId', 'regexp:(svScenariosScroller)|(icScenarios)', 10);

 

Note, that in the examples above, search expressions *must* be enclosed in parenthesis in order to match.

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
tristaanogre
Esteemed Contributor

@bb1832j There is no "issue" with regular expressions in TestComplete.  Just that, as @Marsha_R said, there may be a way to accomplish the same thing without having to use them.  Regular expressions are powerful, useful, and do some great things.  But, they can be tricky, sometimes, to get the syntax proper. So, if there is a way of achieving the same result without having to debug the expression... I take it every time.

 

Please let us know if my suggestion works for you.

 

THAT SAID...

 

The problem with your regular expressions is that you don't have them defined as regular expressions.  You have them working off of a string... and that's not the correct syntax for the native regular expression object in JavaScript. (see https://www.w3schools.com/jsref/jsref_obj_regexp.asp)

 

Change your declaration to

 

var testText = /TextNode(5)/;
var testText1 = /TextNode\(\d+\)/;

and try again.  Note the use of the "/" character and rthe removal of the quotes.


Robert Martin
[Hall of Fame]
Please consider giving a Kudo if I write good stuff
----

Why automate?  I do automated testing because there's only so much a human being can do and remain healthy.  Sleep is a requirement.  So, while people sleep, automation that I create does what I've described above in order to make sure that nothing gets past the final defense of the testing group.
I love good food, good books, good friends, and good fun.

Mysterious Gremlin Master
Vegas Thrill Rider
Extensions available

And one more note about the syntax:

var testText = /TextNode(5)/;

as parenthesis indicate mathing groups in regular expression, basically, the above regex looks for the 'TextNode5' string.

/TextNode(\(5\))/

looks for 'TextNode(5)'.

 

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
cancel
Showing results for 
Search instead for 
Did you mean: