Specflow 获取模态 window 需要很多时间

Specflow getting modal window takes a lot of time

我目前正在为 WPF 应用程序编写自动测试,遇到一个问题,即获取不存在的 window 需要花费大量时间(每次自动测试至少需要 1 分钟,这是不可接受的)。

我有一个有时会打开的文件保存对话框 window。为了不打扰其他场景,我不得不在拆机时关闭这样的window

问题是,如果这样的 window 不存在(例如,它已关闭),则在每个场景中尝试获取它至少需要一分钟。有没有可能让它表现得更好?

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;
            }
        }
    }

使用 Get<WindowedAppScreen>("Printing", InitializeOption.NoCache) 获取 window 也很慢。 使用来自 here.

的信息解决了它

不需要测量精确的性能,但它对我来说足够快了。

现在我的代码如下所示:

[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;
        }
    }
}

希望对大家有所帮助。