Forum Discussion

iamraj09's avatar
iamraj09
Contributor
11 years ago

How to wait for a child object till it is available

Hello everyone ....I am trying to click on a button...it looks enabled but when I click it is in frozen state ....and I would get an error stating that the one of the object is not available .



My question is how can we wait till the last object is available......



Here is the code and the screenshot for the reference. 



//---------------------------------------------Code Snippets-----------------------------------------------//




function Open_Existing_Project()


{


var Open_Project_Button;


var Open_Project_Window;


var OpenProject_TextBox;


var ProjectUpDownGradeWindow;


var ProjectUpdate;


var OK_Button;


var Central_StatusBar;


var Central_Menu_bar;


var Stop_Watch;


var LogicBuilder_Button;


var Logic_StatusBar;


var Central;


 


 


Central=Sys.Process("Central");




Open_Project_Button=Sys.Process("Central").WPFObject("HwndSource: mainWindow").WPFObject("mainWindow").WPFObject("LayoutRoot").WPFObject("headerBar").WPFObject("LayoutRoot").WPFObject("Grid", "", 1).WPFObject("ContentControl", "", 1).WPFObject("DockPanel", "", 1).WPFObject("StackPanel", "", 1).WPFObject("Button", "Open project", 2);




Open_Project_Button.Click();






Central_Menu_bar=Sys.Process("Central").WPFObject("HwndSource: mainWindow").WPFObject("mainWindow").WPFObject("LayoutRoot").WPFObject("headerBar").WPFObject("LayoutRoot").WPFObject("Border", "", 1);






Open_Project_Window=Sys.Process("Central").Window("#32770", "Open project", 1).Window("DUIViewWndClassName", "", 1).UIAObject("Explorer_Pane").Window("CtrlNotifySink", "", 3).Window("SHELLDLL_DefView", "ShellView", 1).UIAObject("Items_View");






OpenProject_TextBox=Sys.Process("Central").Window("#32770", "Open project", 1).Window("ComboBoxEx32", "", 1).Window("ComboBox", "", 1).Window("Edit", "", 1);


OpenProject_TextBox.setText("30148RE-HP_F1404107.project");



 


Central_StatusBar=Sys.Process("Central").WPFObject("HwndSource: mainWindow").WPFObject("mainWindow").WPFObject("LayoutRoot").WPFObject("StatusBar", "", 1).WPFObject("StatusBarItem", "", 1).WPFObject("Rectangle", "", 1);


 


 


Open_Project_Window.Keys("~o"); // Opening the project   *Note Project is too big and it take verz long time ...2-3 minutes to load completely ....


 


Stop_Watch=HISUtils.StopWatch;


 


Stop_Watch.Start();               // Staring the stop watch..........


 


 


while(!Central_Menu_bar.Enabled)    // Checking whether the menu bar is enabled or not ....


{


Delay(500);


}


while(!Central_StatusBar.Enabled)    // Checking whether the status bar is enabled or not ....


{


Delay(500);


}


 


// This is the button trying to click .....but it is in frozen state...




LogicBuilder_Button=Sys.Process("Central").WPFObject("HwndSource: mainWindow").WPFObject("mainWindow").WPFObject("LayoutRoot").WPFObject("headerBar").WPFObject("LayoutRoot").WPFObject("Grid", "", 1).WPFObject("ContentControl", "", 1).WPFObject("DockPanel", "", 1).WPFObject("ScrollViewer", "", 1).WPFObject("StackPanel", "", 1).WPFObject("ItemsControl", "", 1).WPFObject("ContentPresenter", "", 1).WPFObject("SEDropDownButton", "", 1).WPFObject("StackPanel", "", 1).WPFObject("TextBlock", "Logic Builder", 1);


 


LogicBuilder_Button.Click();             // Trying to click the button .....


 


Logic_StatusBar=Sys.Process("Central").WinFormsObject("MainForm").WinFormsObject("_statusBar");



 


while(!Logic_StatusBar.Enabled){


Delay(100);  


if(Logic_StatusBar.Enabled){   


while(Logic_StatusBar.wPartCount==4)


{


Log.Message("If Part count is four "+Logic_StatusBar.wPartCount.toString());


Delay(100);


}    


}   


}


Delay(100);


 


 


Stop_Watch.Stop();


var Time=Stop_Watch.toString();


Stop_Watch.Reset();


 


 


Log.Message("Time take to open the project is "+Time);


 


}

5 Replies

  • Put it in a loop with two possible exit conditions.



    Exit condition 1 = the object is now in the state you require (check for this inside the loop obviously)

    Exit condition 2 = how long to wait until it becomes available (so you can't get stuck inside an eternal loop)



    Then base your actions after the loop on which exit condition was met.
  • Hi Colin,



    Thank you  for the reply ....actually i didnt get you ....can you help me out with the example ..?? 



    Because I have tried something similar to this before ..... 






    while(!Central_StatusBar.Enabled)         // Checking whether the status bar is enabled or not ....


    {


    Delay(500);


    Log.Message("Status_Bar no enabled...yet..!!")


    }


     


    if(Central_StatusBar.Enabled){


    while(!Central_Menu_bar.Enabled)        // Checking whether the menu bar is enabled or not ....


    {


    Delay(1000);


    Log.Message("Menu_Bar no enabled...yet..!!");


    }


    }



    // And here, once the menu bar is active i am trying to click the button ...






    LogicBuilder_Button=Sys.Process("Central").WPFObject("HwndSource: mainWindow").WPFObject("mainWindow").WPFObject("LayoutRoot").WPFObject("headerBar").WPFObject("LayoutRoot").WPFObject("Grid", "", 1).WPFObject("ContentControl", "", 1).WPFObject("DockPanel", "", 1).WPFObject("ScrollViewer", "", 1).WPFObject("StackPanel", "", 1).WPFObject("ItemsControl", "", 1).WPFObject("ContentPresenter", "", 1).WPFObject("SEDropDownButton", "", 1).WPFObject("StackPanel", "", 1).WPFObject("TextBlock", "Logic Builder", 1);


     


    LogicBuilder_Button.Click(35,7);             // Trying to click the button .....


     


    Central.WaitWindow("*", "*.project - SoMachine Logic Builder - V4.1 (Administrator)", -1, 1000000);   // Waiting for this window to pop up.....



     


    Delay(2000);



     



  • I'm a VBScript guy .... so I can't do it in code for you.



    But I can do pseudo code.



    Your example above wait until an object becomes available. But it has no timeout. So if the object never enables, you'll be stuck in that loop forever. (Eternal loop = BAD!)



    So .....



    ------------------------------------------------------------------------------------



    Reset Timer





    Do



    checkStatus = object.property-you-want-to-check



    Loop Until (checkStatus = TRUE) or (timer > Allowed Time)





    If (checkStatus = TRUE)



    <do stuff with your object>



    Else



    <Log error saying the object timed out>



    End If



    ------------------------------------------------------------------------------------





    Something along those lines.



    Make sense?

  • NisHera's avatar
    NisHera
    Valued Contributor
    Just scanning through your code it seems not works..

    (Hvn't done through investigation...)



    while(!Central_StatusBar.Enabled)         // Checking whether the status bar is enabled or not 



    in hear your code trying search for "Enabled" property of  
    Central_StatusBar But Central_StatusBar is yet to create/appear on screen. so it could fail. 

    Insted try Central_StatusBar.WaitProperty("Exists",true,6000) 
  • WaitProperty *should* do the same as my loop. In theory. But I've found it unreliable in practice. Hence why I prefer my own function (I have the above pseudo code wrapped in a generic function used throughout my tests) over it. Plus it's also a little more flexible and allows for multiple conditions ....



    Using my own one also allows to do other things while waiting for the property I want.