Forum Discussion

underqualified's avatar
underqualified
Occasional Contributor
5 years ago

Click Object based on name

Hey everybody! I'm currently trying to automate a test that clicks on a bunch of panels in repetition.

The panels are all part of the same menu that I've already mapped, let's say "Aliases.browser.website.clickMenu", and the panels are all named "Panel(0)", "Panel(1)", "Panel(2)" and so forth, but I do not know how many panels there actually are. How would a script have to look that clicks all panels under clickMenu, in order of the number in the name?

Thanks in advance!

  • Hi,

     

    As Lino (LinoTadros) suggested. You should use eval()/evaluate() function whatever is provided by the scripting language of your test project.

    Sample untested pseudo-code:

    var mainMenu = Aliases.browser.website.clickMenu;

    // open menu, so its sub-items are generated

    mainMenu.HoverMouse();

    var subitemsCount = mainMenu.FindAllChildren('ObjectIdentifier', 'Panel').length;

    for (var i = 0; i < subitemsCount; i++)

    {

      mainMenu.HoverMouse(); // required for the second and up loop iterations to open menu items

      var str = 'var submenuItem = ' + mainMenu.AliasedName + '.Panel(' + i + ')'; // should resolve to e.g. "var submenuItem = Aliases.browser.website.clickMenu.Panel(0)"

      eval(str); // this will resolve Panel object and assign it to the submenuItem variable

      submenuItem.Click();

      // ...

    }

     

  • LinoTadros's avatar
    LinoTadros
    Community Hero

    The container Panel will have a property in TestComplete Object Browser called "ChildCount".  You can find out how many panels are below the clickMenu from that property and create a For loop that will go through all the panels underneath and click on each one of them.

     

    Cheers

    Lino

    • underqualified's avatar
      underqualified
      Occasional Contributor

      I'm not sure how I'd go on to do that, though - is there any guide on that specific case in the Testcomplete documentation?

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    As Lino (LinoTadros) suggested. You should use eval()/evaluate() function whatever is provided by the scripting language of your test project.

    Sample untested pseudo-code:

    var mainMenu = Aliases.browser.website.clickMenu;

    // open menu, so its sub-items are generated

    mainMenu.HoverMouse();

    var subitemsCount = mainMenu.FindAllChildren('ObjectIdentifier', 'Panel').length;

    for (var i = 0; i < subitemsCount; i++)

    {

      mainMenu.HoverMouse(); // required for the second and up loop iterations to open menu items

      var str = 'var submenuItem = ' + mainMenu.AliasedName + '.Panel(' + i + ')'; // should resolve to e.g. "var submenuItem = Aliases.browser.website.clickMenu.Panel(0)"

      eval(str); // this will resolve Panel object and assign it to the submenuItem variable

      submenuItem.Click();

      // ...

    }

     

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Thank you so much AlexKaras and LinoTadros! 

       

      Hi underqualified , were you able to implement the eval() or evaluate() function Alex is suggesting that you use? Was it a success?