Faster way of searching for Top Level Windows in WPF CodedUI

时间:2015-09-14 16:10:00

标签: c# wpf coded-ui-tests

I have several parts of my application that are supposed to close a window. Checking that these windows have been closed using Coded-UI is incredibly slow. Right now my code looks like this:

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

The problem is, this takes around 30s to search, and there are around 5 times this is used, and I have around 10 similar windows all being tested. I'd like to trim this time if possible, as it's making my tests slow.

I have also tried a dynamic solution (which is basically identical to the UIMap implementation):

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

This is just as slow. It would be nice to use ApplicationUnderTest as a search parent, but as the window is Top Level, it doesn't seem to work.

Surely it shouldn't be too hard just to look at the open windows on my system (5), and check their titles against the search parameter?

Edit: Using SearchConfiguration.VisibleOnly doesn't seem to help either.

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我认为可能有更好的答案。如果设置这样的全局配置并且必须处理WPF表并找到特定的单元格,则可能找不到它。

使用窗口名称通常不是一个好主意,如果有任何动态标题,控制名称是一个很好的常量。听起来像是你传递了糟糕的SearchProperties。您可以使用DrawHighlight()来查看CUI是否实际上正在查找您的控件。首先将主父窗口传递给close窗口方法,然后将其用作try。

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