Forum Discussion

royd's avatar
royd
Regular Contributor
8 years ago

Problem with "FindAllChildren"

I am trying to find all menu items. I am getting error "Unable to find the object aspnetForm.", see the screcapture bellow. I am new to Jscript, Probably making a simple mistake, help is much appreciated.

 

Besides the issue I am having, I would also like to know is, once found how do I click on one of the menu, say "Admission Form". An exaple would be very helpful. Here is the code:

 

var page = Sys.Browser("*").Page("https://<testapplication>.com/MainMenu.aspx*");
var mainMenu = page.aspnetForm.panelMainMenu; //see the capture bellow
// Search for all menu links
var menuChild = mainMenu.FindAllChildren("contentText", 50, true).toArray();
menuChild = (new VBArray(menuChild)).toArray();

// Log the search results
if (menuChild.length > 0)
{
for (i = 0; i < menuChild.length; i++)
Log.Message(menuChild[i].contentText);
Log.Message("Total number of menu items found are: " + menuChild.length);
}
else
Log.Warning("No menu items found.");

 

 

 

 

 

Thanks in advance.

  • You dont't need to convert the below to array.

     

    mainMenu.FindAllChildren("ObjectType","link", 50, true);

    When you use FindAllChildren by default it will return as a Array.

     

    FYI.

    • FindAllChildren Method (Common for All Test Objects)
      Declaration

      TestObj.FindAllChildren(PropNamesPropValuesDepthRefresh)

      TestObjA variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
      PropNames[in]   Required   Variant   
      PropValues[in]   Required   Variant   
      Depth[in]   Optional   IntegerDefault value: 1   
      Refresh[in]   Optional   BooleanDefault value: True   
      ResultAn array of the tested objects.

     

     

     

    Once you got the list of menus available from the Pane to menuChild object.

     

    You can script something like below.

     

    var menuChild = mainMenu.FindAllChildren("ObjectType","link", 50, true);
    
    // Log the search results
    if (menuChild.length > 0)
    {
    for (i = 0; i < menuChild.length; i++)
    //For clicking 
    menuChild[i].Click();

    //You can do your stuffs } else Log.Warning("No menu items found.");

     

  • shankar_r's avatar
    shankar_r
    Community Hero

    You dont't need to convert the below to array.

     

    mainMenu.FindAllChildren("ObjectType","link", 50, true);

    When you use FindAllChildren by default it will return as a Array.

     

    FYI.

    • FindAllChildren Method (Common for All Test Objects)
      Declaration

      TestObj.FindAllChildren(PropNamesPropValuesDepthRefresh)

      TestObjA variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
      PropNames[in]   Required   Variant   
      PropValues[in]   Required   Variant   
      Depth[in]   Optional   IntegerDefault value: 1   
      Refresh[in]   Optional   BooleanDefault value: True   
      ResultAn array of the tested objects.

     

     

     

    Once you got the list of menus available from the Pane to menuChild object.

     

    You can script something like below.

     

    var menuChild = mainMenu.FindAllChildren("ObjectType","link", 50, true);
    
    // Log the search results
    if (menuChild.length > 0)
    {
    for (i = 0; i < menuChild.length; i++)
    //For clicking 
    menuChild[i].Click();

    //You can do your stuffs } else Log.Warning("No menu items found.");

     

    • royd's avatar
      royd
      Regular Contributor

      Hi Shankar

       

      You are absolutely right! It worked.

       

      Thank you so very much for your help, really appreciate it! 

       

      Dave

    • royd's avatar
      royd
      Regular Contributor

      Thanks for replying Shankar,

       

      I thought you need to convert VBArray to Jscript arry, at least that's what I have been reading in this board! Anyway, I will give it a shot and post my results.

       

      Thanks

       

      Dave 

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        There are certain situations that require that conversion from JavaScript Array to a variant array and vice-versa. If you create a JScript/JavaScript array and want to pass it into a method that requires a variant array, then you need to do a conversion.  Likewise, if you want to take the array passed back from FindAllChildren and use it as a JScript native array, you need to convert.  However, the array type that is returned by FindAllChildren is usable "as is" by TestComplete.  

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    I'm guessing that your problem is not with FindAllChildren initially but with your line that is doing the assign to mainMenu

    var mainMenu = page.aspnetForm.panelMainMenu

    This is the only place where "aspnetForm" being referenced so I'm betting this is the error.

    What this means is that, at the point where your scripts are doing this assignment, that object does not exist.  Double check in your object browser to make sure it's there.  I'm curious, though, as to how that object is being defined.  The reason being is that I would expect to see it as page.Form("aspnet") in the Object browser.  Are you using NameMapping/Aliases?  If so, then you should do the following:

    var page = Aliases.browser.pageMainMenu;
    var mainMenu = page.aspnetForm.panelMainMenu;

    Beyond that... your FindAllChildren actually has a syntax problem.  You are missing a parameter.  FindAllChildren has two required parameters and two optional.  From the help

    FindAllChildren Method (Common for All Test Objects)

    TestObj.FindAllChildren(PropNamesPropValuesDepthRefresh)

    TestObj A variable, parameter or expression that specifies a reference to one of the objects listed in the Applies To section
    PropNames [in]    Required    Variant    
    PropValues [in]    Required    Variant    
    Depth [in]    Optional    Integer Default value: 1   
    Refresh [in]    Optional    Boolean Default value: True   
    Result An array of the tested objects.

     

    So, you are using "contentText" as your PropNames parameter... but you're not actually searching for a value.  So... you need to change that line to:

    var menuChild = mainMenu.FindAllChildren("contentText", "whateveryouarelookingfor", 50, true).toArray();
    • royd's avatar
      royd
      Regular Contributor

      Hi Robert,

       

      Thanks for responding so promptly. I have been through the documentation and everything that I can find in the user group for find all children. That being said, I cannot say that I understood everything. Mainly because of my lack of knowledge of TestComplete and JavaScript/Jscript. I always wondered what does “[in]” means in FindAllChildren documentation page?

       

      To answer to your question, I’m trying to run away from name mapping. At present I am using name mapping while I tried to master other alternatives. As per your suggestion, I looked at the object browser and the frame is:

       

      Form("aspnetForm").Table(0).Cell(0, 0).Panel(0).Panel(0).Panel("mainMenuInner")

       

      You were right about that! To find that out, I had to delete the name mapping and later restoring it back to normal. As per your suggestion, I edited the line as follows:

       

      var mainMenu = page.Form("aspnetForm").Table(0).Cell(0, 0).Panel(0).Panel(0).Panel("mainMenuInner");

       

      As far as the FindAllChildren syntax error is concerned, I could not figure out how to express multiple values for contentText, as each menu contentText values are the name of the menu itself. Since you pointed that out, something tells me that I need to find a PropertyName that has same PropertyValue for all menu items. Which is contrary to what I thought, that one should find a property value that is unique for every individual objects. Guess it is opposite of that when using FindAllChildren. In that case, I found that “ObjectType” has a property value of link for all menu items. Should I edit the line as follows?

       

      var menuChild = mainMenu.FindAllChildren("ObjectType ", “link”, 50, true).toArray();

       

      Is there anything else I should be concerned about?

       

      Once it works as expected, how do I interact with those menu items? Can you give me an example?

       

      Again, thank you very much for being so prompt and helping me out.

      • royd's avatar
        royd
        Regular Contributor

        After editing the script I ran it now I'm getting error on:

         

        menuChild = (new VBArray(menuChild)).toArray();

         

        Runtime error

         

        VBArray expected