0
votes

I am working on a coded ui project for a win forms application. I would like to get the main window of the application under test programmatically.

What I tried is:

WinWindow mainWindow = new WinWindow();
mainWindow.TechnologyName = "MSAA";
main.Window.SearchProperties.Add(new PropertyExpression(WinWindow.PropertyNames.Name, "Soft under test", PropertyExpressionOperator.Contains));
mainWindow.SearchProperties.Add(new PropertyExpression(WinWindow.PropertyNames.ClassName, "WindowsForms10.Window", PropertyExpressionOperator.Contains));
mainWindow.SearchConfigurations.Remove(SearchConfiguration.VisibleOnly);
mainWindow.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);

This fails with:

The playback failed to find the control with the given search properties. Additional Details: TechnologyName: 'MSAA' Name: 'Soft under test' ClassName: 'WindowsForms10.Window' ---> System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.

What is the correct way to get the window?

1
Let the Coded UI record and generate tool show you. Make a recording, possibly into a sandbox project, and then copy the useful bits. - AdrianHHH
Also, performance wise, try using the least amount of search properties to find the control you are looking for. The coded ui test recorder lets you do that. - PixelPlex

1 Answers

0
votes

Not sure if this question is still relevant, but here is the code I use to get the window:

this.TechnologyName = "MSAA";
this.SearchProperties.Add(UITestControl.PropertyNames.Name, "MyProgram");
this.SearchProperties.Add(UITestControl.PropertyNames.ControlType, "Window");
this.SearchProperties.Add("ControlName", "TopScreen");
this.SearchConfigurations.Add(SearchConfiguration.ExpandWhileSearching);

Perhaps, you need ExpandWhileSearching property for it to find the screen.