Search code examples
c#wpfautomated-testswhite-frameworkteststack

Specflow getting modal window takes a lot of time


I am currently writing autotests for a WPF application and faced a problem that getting the window that does not exist takes a lot of time (at least 1 minute on each autotest which is unacceptable).

I have a file saving dialog window that is sometimes opened. In order to not disturb other scenarios, I have to close such window at teardown.

The problem is that this if such window does not exist (for ex. it was closed) trying to get it takes at least a minute on each scenario. Is it possible to make it perform better?

public Window SavePrintOutputWindow
    {
        get
        {
            try
            {
                var printingScreen = MainScreen.ScreenWindow.ModalWindow("Printing");
                var saveOutputWindow = printingScreen.ModalWindow("Save Print Output As");
                return saveOutputWindow;
            }
            catch (Exception e)
            {

                return null;
            }
        }
    }

Solution

  • Getting window using Get<WindowedAppScreen>("Printing", InitializeOption.NoCache) was also slow. Solved it using info from here.

    There was no need for measuring exact performance but it works fast enough for me.

    Now my code looks like this:

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    public Window SavePrintOutputWindow
    {
        get
        {
            try
            {
                IntPtr hWnd = FindWindow(null, "Save Print Output As");
                if (hWnd == IntPtr.Zero)
                {
                     return null;
                }
                var printingScreen = MainScreen.ScreenWindow.ModalWindow("Printing");
                var saveOutputWindow = printingScreen.ModalWindow("Save Print Output As");
                return saveOutputWindow;
            }
            catch
            {
                return null;
            }
        }
    }
    

    Hope it helps somebody.