I've run into a quirky issue with application window focus when running tests with the following setup:
- Vista SP1
- .NET 3.5 SP1
- WinForms App
- Running tests from nunit-console
When I run tests from a console window and the console window has focus/is topmost everything works fine. When another window besides the console window has focus at the time the tested application starts, attempts to find components on the tested application
window fail quietly.
For example, the code below will proceed quietly with no errors until throwing a null-reference exception on the last line:
Application myApp = Application.Launch("MyApp.exe");
Window mainWindow = myApp.GetWindow(SearchCriteria.ByAutomationId("MainForm"), InitializeOption.NoCache);
Window modal = mainWindow.ModalWindow("myModalWindow");
- The app starts but the window is not top-most.
- The Application.GetWindow() call returns a window reference.
- MenuBar.MenuItem.Click() hangs unitl the BusyTimeout expires and then proceeds with no timeout exception.
- mainWindow.ModalWindow() hangs and then proceeds with no error, but does not actually return the modal window (which doesn't exist because the menu item was never clicked).
The only way I've found to eliminate this is to call Window.Focus() after getting the main window.
Just to be clear:
- There is no user mouse or keyboard activity going on during the test.
- The exact same code works fine 100% of the time when the console window has initial focus and fails 100% of the time when it does not have focus.
I'm fine with the Focus() workaround, but this did cost me a fair bit of time and is frustrating because of its intermittent nature. I thought it was worth pointing out to help other folks and in case a fix can be implemented in White itself.