Forum Discussion

bb1832j's avatar
bb1832j
Occasional Contributor
5 years ago
Solved

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

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    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-method.html#remarks

    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.

     

  • tristaanogre's avatar
    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);
            }
        }
    • bb1832j's avatar
      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.