Forum Discussion

royd's avatar
royd
Regular Contributor
8 years ago

Handling different values of the same object attribute, in two areas of the application?

Using TestComplete 12.3, JScript.

 

The menu panel of our application "idStr" (id) value are different in two areas of our application ("mainMenuInner" and "navMenuInner" repectively). Is there a way to include both values into the mix to set a variable? Something like-

 

var mainMenu = page.Find("idStr", "mainMenuInner" || "navMenuInner", 10, 1000);

I did try that, but did not work on the page where the value is "navMenuInner".

 

Thanks for help in advance.

 

Dave

  • Hi Dave,

     

    Two more options:

    a) Wildcards: e.g. var mainMenu = page.Find("idStr", "*MenuInner", 10, 1000);

    b) Regular expressions: e.g. var mainMenu = page.Find("idStr", "regexp: (mainMenuInner)|(navMenuInner)", 10, 1000);

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    UPD: AlexKaras beat me to it. :)

     

    If there are no other objects with IDs ending in "MenuInner", you can use wildcard search:

    var mainMenu = page.FindChild("idStr", "*MenuInner", 10);

    Since v. 11.2, Find methods also support regular expressions:

    var mainMenu = page.FindChild("idStr", "regexp:^(main)|(nav)MenuInner$", 10);

    A couple of notes:

    • Use FindChild and not Find. Find starts searching from the parent object itself (in your example - page), FindChild skips the parent object.
    • The fourth parameter of Find/FindChild is a boolean (Refresh=true/false) and not a number. Review the parameters to make sure they are correct.
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi Dave,

     

    Two more options:

    a) Wildcards: e.g. var mainMenu = page.Find("idStr", "*MenuInner", 10, 1000);

    b) Regular expressions: e.g. var mainMenu = page.Find("idStr", "regexp: (mainMenuInner)|(navMenuInner)", 10, 1000);

    • royd's avatar
      royd
      Regular Contributor

      Hi Alex

       

      Thanks for a very clever solution (at least simple enough for me to understand it!). :) I will definitely give it a try and let you know (I don't see why it won't work).

       

      Helen

      I never thought about using wild card,  brilliant! Fortunately, there is only one instance  MenuInner. Now I have three different solutions to the my problem, love it!! Also thanks for clarifying Find  and FindChild issue, I will correct that.

       

      Thanks to you both! :D

  • shankar_r's avatar
    shankar_r
    Community Hero

    Hi,

     

    If i was in that situation, i will do like below:

     

    var mainMenu = page.Find("idStr", "mainMenuInner", 10, 1000);
    var navMenu = page.Find("idStr","navMenuInner", 10, 1000);
    
    var menuObject = null;
    
    if(mainMenu.Exists)
    {
    menuObject = mainMenu;
    Log.Message("selected the object with idStr as mainMenuIner");
    }
    else if(navMenu.Exists)
    {
    menuObject = navMenu)
    Log.Message("selected the object with idStr as navMenuInner");
    }
    
    //Here use if to check whether that has object or not 
    
    if(menuObject != null)
    {
    
    //Do your stuff
    }
    • royd's avatar
      royd
      Regular Contributor

      Thank you Shankar, I'll give it a try and let you know how it goes.

       

      Good to see you.

       

      Dave