Finding a window when only part of the window title is known in Coded UI Tests
Asked Answered
B

2

6

The application I am writing coded UI tests for has a window where part of the window title is based on a random filename generated for a temporary file, so the test can only know the static part of the window title.

Occasionally, when no other windows are open, the test runs fine. It is a bit problematic when other windows are open, however. If the other windows have similar controls, which window the test picks to work in is unpredictable.

Binding answered 18/6, 2012 at 16:5 Comment(0)
B
14

I've narrowed it down to this: When searching for a control, the Coded UI Test uses search properties and a tree-like structure of controls. If it can't find an exact match it finds a close match (so it can't find the exact window title name, it excludes that and keeps searching for a window that matches any other given properties) which is why it works with controls in other windows.

The solution is really to give it more search properties to work with. One method I use is to add a property using a PropertyExpression and pass it PropertyExpressionOperator.Contains.

As an example, I recorded opening MS Word and closing it. This generates a control in the UIMap, and in its constructor is the following:

this.SearchProperties[WinWindow.PropertyNames.Name] = "Document1 - Microsoft Word";
this.SearchProperties[WinWindow.PropertyNames.ClassName] = "OpusApp";

Rather, the first line should be:

this.SearchProperties.Add(new PropertyExpression(WinWindow.PropertyNames.Name, "Microsoft Word", PropertyExpressionOperator.Contains));
Binding answered 18/6, 2012 at 16:5 Comment(2)
Do you have Feature Pack 2 installed? In FP2, you can edit these search properties without having to do that in the code. Just right click an object in the UIMap, select Properties, select Search Properties and click the EqualsTo operator behind Name. Select Contains and make the Value field to a generic matching name. Noteworthy: In the Search Configuration you can select ExpandWhileSearching; this will search for an item matching your total child object tree instead of just the single object.Tiffanitiffanie
Btw +1 for finding this solution in code and posting it as answer.Tiffanitiffanie
R
3

Or even easier, you can use:

this.SearchProperties.Add(WinWindow.PropertyNames.Name, "Microsoft Word", PropertyExpressionOperator.Contains);
Rampart answered 3/12, 2015 at 21:29 Comment(1)
True, either will work but you have omitted the "new PropertyExpression" and it still works, only with less code! This will keep code short and I will be using this approach many times.Chasteen

© 2022 - 2024 — McMap. All rights reserved.