在 WPF CodedUI 中搜索顶级 Windows 的更快方法

Faster way of searching for Top Level Windows in WPF CodedUI

我的应用程序有几个部分应该关闭 window。 使用 Coded-UI 检查这些 windows 是否已关闭非常慢。现在我的代码如下所示:

Assert.IsFalse(UIMap.SomeWindow.TryFind(),
            "X Window found when should be closed");

问题是,这需要大约 30 秒的搜索时间,大约有 5 次使用,我有大约 10 个类似的 windows 都在测试中。如果可能的话,这次我想trim,因为它让我的测试变慢了。

我也试过动态方案(和UIMap实现基本一致):

var window = new WpfWindow();
window.SearchProperties.Add(UITestControl.PropertyNames.Name, "Window Title");
Assert.IsFalse(window.TryFind());

这同样慢。最好使用 ApplicationUnderTest 作为搜索 parent,但由于 window 是顶级,它似乎不起作用。

当然,在我的系统 (5) 上查看打开的 windows,并根据搜索参数检查它们的标题应该不会太难?

编辑:使用 SearchConfiguration.VisibleOnly 似乎也无济于事。

在 LinkedIn 上令人惊讶地找到了我的答案。

正在使用:

Playback.PlaybackSettings.SearchTimeout = 1000; //in ms
Playback.PlaybackSettings.ShouldSearchFailFast = true;

来源:https://www.linkedin.com/grp/post/3828241-5843659258196959234

(C&P):

//Search Settings

// Value is in milliseconds. Default search timeout is 2 minutes. 
// The search engine will continue making passes until the timeout has expired 
// or the window has been found.
settings.SearchTimeout = 10000;

// Default search will make 3 attempts. 
// If true the default 3 attempts is applied. If false then only one attempt should take place. 
settings.ShouldSearchFailFast = true;

我认为可能会有更好的答案。如果设置这样的全局配置并且必须处理 WPF Table 并找到特定的单元格,您可能会发现它不起作用。

如果有任何动态标题,使用 Window 名称通常不是一个好主意,控件名称是一个很好的常量。在我看来,您传递的是错误的 SearchProperties。您可以使用 DrawHighlight() 来查看 CUI 是否真的找到了您的控件。首先将 main parent window 传递给您的关闭 window 方法,然后将其用作尝试。

public static WinWindow _mainParent(string MainParentCtlName)
    {
        var _mainForm = new WinWindow();
        _mainForm.SearchProperties.Add("ControlName", MainParentCtlName);
        return _mainForm;
    }
public static void CloseWindow(string MainWinCtlName)
    {
        var close = new WinButton(_mainParent(MainWinCtlName));
        close.SearchProperties.Add("Name", "Close");
        Mouse.Click(close);
    }
try
{CloseWindow("MainWindowForm")}
catch{}