Forum Discussion

iamraj09's avatar
iamraj09
Contributor
10 years ago
Solved

How to check for existence of the Object .?

Hello Everyone,

 

Currently, I am performing a test where I need to check for the existence of the object .......depending on that I need to return the value.

 

One of the functions in the test case is ...

 

function Devicespace()
{
if(!Sys.Process("Central").WinFormsObject("MainForm").WinFormsObject("DockContainer", "", 1).WinFormsObject("Devices").Exists)
{
return Sys.Process("Central").WinFormsObject("MainForm").WinFormsObject("DockContainer", "",2).WinFormsObject("Devices").WinFormsObject("Devices").WinFormsObject("_treeTableView").WinFormsObject("InternalListView", "");
}
if(!Sys.Process("Central").WinFormsObject("MainForm").WinFormsObject("DockContainer", "", 2).WinFormsObject("Devices").Exists)
{
return Sys.Process("Central").WinFormsObject("MainForm").WinFormsObject("DockContainer", "",1).WinFormsObject("Devices").WinFormsObject("Devices").WinFormsObject("_treeTableView").WinFormsObject("InternalListView", "");
}
}

 

Brief description about the code ..

 

I am trying to check for the existence of WinFormsObject("Devices") as per the examples provided on the website it should work, but when i try the same on my application....it tries to search for that particular object and the test failes if it doesnt find that object .....stating that "Unable to find the WinFormsObject(Devices)" for the reference refer the image below.

 

Please guide me through..Thank you.

 

ERROR.jpg

 

 

  • Hi iamraj09,

     

    You need to use the WaitWinFormsObjects to obtain the object you need to check:

    if(!Sys.Process("Central").WinFormsObject("MainForm").WinFormsObject("DockContainer", "", 1).WaitWinFormsObject("Devices", 5000).Exists)
    

     

5 Replies

  • TanyaYatskovska's avatar
    TanyaYatskovska
    SmartBear Alumni (Retired)

    Hi iamraj09,

     

    You need to use the WaitWinFormsObjects to obtain the object you need to check:

    if(!Sys.Process("Central").WinFormsObject("MainForm").WinFormsObject("DockContainer", "", 1).WaitWinFormsObject("Devices", 5000).Exists)
    

     

  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor

    To avoid logging an error use the WaitNNN method.

    More information here.

     

    That assumes that each parent object exists. That is your DockContainer object has multiple indexes and the Devices object may exist on one and not the other. To simplify I would suggest using .Find to see if the object exists. You can use other properties and values in the Find but here is an example:

     

    function Devicespace() {
      var foundList = Sys.Process("Central").WinFormsObject("MainForm").Find("Name","*InternalListView*", 7, true);
      if(foundList.Exists) {
        return foundList; 
      }
    }
  • colinstrydom's avatar
    colinstrydom
    Occasional Contributor

    After many failed atempts to do exactly this, the best solution I found was this. Example of the windows calcualtor

     

    if not Sys.Process('calc').WaitWindow('CalcFrame', 'Calculator', 1, 10000).Exists then
    Log.Error('The object was not found within 10000 ms.', 'Sys.Process(''calc'').Window(''CalcFrame'', ''Calculator'', 1)');

     

    You can obviously expand on this. You do waste a little time by needing to wait a certain amount of time, but at least it does not stop execution.

     

    You can also instal the "Wait for Object: script extensions to help you do this automatically. The one I have is Version 1.0 by Helen Kosova.

    • mes6073's avatar
      mes6073
      Contributor

      The following is a prototype and has not been tested, but I would think this would also accomplish the task while making the code less dependent on hard coded pathes or indexes.

       

      var proc = Sys.Process("Central");
      var index = 1;
      var wClass = "Devices";
      var dlg;
      
      if Devicespace(proc, dlg, class) {
         // do something
      }
      
      function Devicespace(proc, ctrl, index, wClass) { 
        
         try {
            var propArray = propArray = new Array("WndClass", "Exists");
            var ValuesArray = valuesArray = new Array(WndClass, true);
            var result = false;
            var ctrls = devProc.FindAllChildren(PropArray, ValuesArray, 25);
         
            // Process the search results
            if (ctrls.length > 0) {
               for (var i = 0; i < ctrls.length; i++) {
                  Log.Message("FullName: " + ctrls[i].FullName + "\r\n" + "Text: " + ctrls[i].index);
                  
                  if (ctrls[i].index == index) {
                     ctrl = ctrls[i];
                     result = true;
                     break;
                  }
               }
            }   
         } catch(e) {
            // Posts an exception message to the test log
            Log.Error(e.description);
         }
         
         return result;
         
      } // Devicespace
      • iamraj09's avatar
        iamraj09
        Contributor

        Thank you all...for your time ....Tanya's solution was simple and enough for my tests .....