cancel
Showing results for 
Search instead for 
Did you mean: 

Regular Expressions in JavaScript not working

SOLVED
Highlighted
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.

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

Re: Regular Expressions in JavaScript not working

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
[Community Expert Group]
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
Extensions available
8 REPLIES 8
Community Hero

Re: Regular Expressions in JavaScript not working

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
[Community Expert Group]
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
Extensions available
Occasional Contributor

Re: Regular Expressions in JavaScript not working

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.

 

 

Community Hero

Re: Regular Expressions in JavaScript not working

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
[Community Expert Group]
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
Extensions available
Occasional Contributor

Re: Regular Expressions in JavaScript not working

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.

Community Hero

Re: Regular Expressions in JavaScript not working

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

 

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

Community Hero

Re: Regular Expressions in JavaScript not working

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", "regexpSmiley SadOpen)|(Download)|(Launch)"], 5);

control = root.FindChild('WPFControlAutomationId', 'regexpSmiley SadsvScenariosScroller)|(icScenarios)', 10);

 

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

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Community Hero

Re: Regular Expressions in JavaScript not working

@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
[Community Expert Group]
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
Extensions available
Community Hero

Re: Regular Expressions in JavaScript not working

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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
New Here?
Join us and watch the welcome video:
Join the September Hub-bub to show off, learn and win