Search code examples
c#visual-studio-2010ui-automationcoded-ui-tests

WhyWaitForControlExist() returning false when querying for the second time on the same UI control?


I am facing a weird behavior with WaitForControlExist() function of Coded UI. When querying for the second time, the value is returning false even though the control exist.. Is this the expected behavior of Coded UI? My Program looks something similar like below..

//A sample program that Checks for Run window when "Windows + R" key is pressed

public void TestFunction()
{
   bool isExists = false;

   #region Variable Declarations

   UIMap uiMap = new UIMap();

   WinWindow uIRunWindow = uiMap.UIRunWindow;
   WinComboBox uIOpenComboBox = uiMap.UIRunWindow.UIItemWindow.UIOpenComboBox;
   WinButton uICancelButton = uiMap.UIRunWindow.UICancelWindow.UICancelButton;

   #endregion

   //1st Run

   Keyboard.SendKeys("R", ModifierKeys.Windows);
   isExists = uIRunWindow.WaitForControlExist(7000);
   Console.WriteLine("Is Run Window Appears :: " + isExists); 
   Mouse.Click(uICancelButton);
   Playback.Wait(2000);
   isExists = uIRunWindow.WaitForControlNotExist(7000);
   Console.WriteLine("Is Run Window Closes :: " + isExists);

   //2nd Run 

   Keyboard.SendKeys("R", ModifierKeys.Windows);
   isExists = uIRunWindow.WaitForControlExist(7000);
   Console.WriteLine("Run Window Appears :: " + isExists); 
   Mouse.Click(uICancelButton);
   Playback.Wait(2000);
   isExists = uIRunWindow.WaitForControlNotExist(7000);
   Console.WriteLine("Is Run Window Closes :: " + isExists);

}

In the above program, during the 2nd run event though Run window exists the value is returned as false.

The work around I came across is either to use 2 different objects for 1st and 2nd run, or to reinitialize the UIMap object as shown here

Please help me if there is any other solution or best practice in this scenario


Solution

  • Search results and the trees created by MSAA/UIAutomation are cached. In the search configuration for the control you can turn AlwaysSearch on which should solve the problem, without needing to recreate the control.