Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Specflow获取模式窗口需要很多时间_C#_Wpf_Automated Tests_White Framework_Teststack - Fatal编程技术网

C# Specflow获取模式窗口需要很多时间

C# Specflow获取模式窗口需要很多时间,c#,wpf,automated-tests,white-framework,teststack,C#,Wpf,Automated Tests,White Framework,Teststack,我目前正在为WPF应用程序编写自动测试,遇到了一个问题,即获取不存在的窗口需要花费大量时间(每次自动测试至少需要1分钟,这是不可接受的) 我有一个文件保存对话框窗口,有时会打开。为了不干扰其他场景,我必须在拆卸时关闭此类窗口 问题是,如果这样的窗口不存在(例如,它已关闭),则在每个场景中尝试获取它至少需要一分钟。有可能让它表现得更好吗 public Window SavePrintOutputWindow { get { try

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

我有一个文件保存对话框窗口,有时会打开。为了不干扰其他场景,我必须在拆卸时关闭此类窗口

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

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(“Printing”,InitializeOption.NoCache)
获取窗口也很慢。 使用来自的信息解决了它

没有必要测量精确的性能,但它对我来说足够快了

现在,我的代码如下所示:

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

希望它能帮助别人。

那么,你发布的代码花了多少时间?找到窗户了吗?捕获异常?最好检查
是否(object!=null)
而不是尝试捕获so@stuartd它试图找到窗口“正在打印”的行。