Forum Discussion

mgreen's avatar
mgreen
Contributor
5 years ago
Solved

FindAll returns blank in log - help!

Hi All - 

Trying to get back some data when doing the "FindAll" method.  "Find" seems to work fine and prints to the log fine.  Whenever I run FindAll, it does not give me any error - but the log is literally empty.  Can someone point out what I'm doing wrong here?  

 

This works:

function Find() {
Results = NameMapping.Sys.browser.page.nav.Find("contentText", "administrative", 1, 1000);
Log.Message(Results.MappedName);
}

 

This does not work: 

function Find() {
Results = NameMapping.Sys.browser.page.nav.FindAll("contentText", "*", 1, 1000);
Log.Message(Results.MappedName);
}

 

Any ideas? Many thanks

 

 

  • further to cunderw 

     

    You may need to explicitly convert the items that FindAll returns to an array

    function Find() {
    Results = NameMapping.Sys.browser.page.nav.FindAll("contentText", "*", 1, 1000).toArray();
    for(var i=0;i<Results.Length;i++){
      Log.Message(Results[i].MappedName);
      //you could break early:
      if(Results[I].MappedName=='TableName')  {
        break;
      }
    } 
    } //I tend to use for(var i=Results.length-1;i>=0;i--) because TC returns the find results from the leaf furthest down the hierarchy first and traverses up and in the majority of cases I want to look from the top level down - personal preference though

12 Replies

  • cunderw's avatar
    cunderw
    Community Hero

    FindAll returns an array of objects, not a single object so it doesn't have a property "MappedName", you will have to loop throught the results and print each one of the objects in the array. 

    • RUDOLF_BOTHMA's avatar
      RUDOLF_BOTHMA
      Community Hero

      further to cunderw 

       

      You may need to explicitly convert the items that FindAll returns to an array

      function Find() {
      Results = NameMapping.Sys.browser.page.nav.FindAll("contentText", "*", 1, 1000).toArray();
      for(var i=0;i<Results.Length;i++){
        Log.Message(Results[i].MappedName);
        //you could break early:
        if(Results[I].MappedName=='TableName')  {
          break;
        }
      } 
      } //I tend to use for(var i=Results.length-1;i>=0;i--) because TC returns the find results from the leaf furthest down the hierarchy first and traverses up and in the majority of cases I want to look from the top level down - personal preference though
    • a_g_reed's avatar
      a_g_reed
      Occasional Contributor

      Hi cunderw I'm running into a similar issue as mentioned by the OP. My question relating to that and your response is, how can I loop through the results and print each one of the objects in the array? My apologies if this is something basic or beginner; I'm a complete newbie to this and trying to learn by doing & from the TC Academy, Courses, Community Board, etc.

      Thanks in advance for any reply or assistance.

       

      Cheers,

      Allen

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        Not a problem, a_g_reed .  Newbies who seek to learn are better than ones who assume they know everything already. ;-)

         

        Anyways... this is a basic coding task, looping through an array.  While this is test automation, you are, effectively, writing code so it is a good idea to find a coding tutorial somewhere as well.  For JavaScript, I'd recommend https://www.w3schools.com/js/default.asp

         

        That said... two things... you say "print the object"... that's not exactly what you're seeking to do.  An object has properties like "Caption" or "wText" or "ContentText" or something like that.  So, you first need to know what you're looking to print.  From there, it's a for loop like what is posted above.  Keep in mind that if the property is blank (like "MappedName" for the OP), then you'll print blank.