Anyone know the proper way to reliably locate a desktop icon using Coded UI Testing APIs?
The auto generated code created from recorded actions that clicks on a specific desktop icon works the day it was recorded. When I turn on my laptop the next day the recording action no longer works as icon cannot be found.
I am trying to handcraft this so I tried:
public void LaunchOi()
{
var desktop = ApplicationUnderTest.Desktop.GetChildren().First(c => c.Name == "Desktop");
var folderView = new WinWindow(desktop);
// I copied these numbers from autogenerated code.
// Yesterday, PropertyNames.Instance = 7, today = 9. Why so?
folderView.SearchProperties[WinWindow.PropertyNames.ControlId] = "1";
folderView.SearchProperties[WinWindow.PropertyNames.Instance] = "9";
folderView.Find();
var winList = new WinListItem(folderView);
winList.SearchProperties[WinListItem.PropertyNames.Name] = "OCC600 OI";
winList.Find();
Mouse.DoubleClick(winList, MouseButtons.Left);
}
As indicated in my comments, WinWindow.PropertyNames.Instance does not remain constant. When I peformed this recording yesterday, WinWindow.PropertyNames.Instance was 7. Today, it is 9. Anyone know why? I cannot find any documentation on this.
As Schaliasos mentioned in the comments you should start by deleting the instance property. Instance properties are very unreliable especially in something that changes as frequently as the desktop.
A little more explanation on instance properties… Instance properties are usually used in recordings when there is a lack of valid search properties. So as a last ditch effort to make the recording, the recorder will say I have 20 controls with the same search properties and the control that should be used is number 7. Then the next day when you go to run the application you probably added or removed a couple of desktop icons and say you have 22 icons now. Now the framework is still looking for instance 7 of the control but what was 7 yesterday is 9 today.
If there are any other search properties available you should find and use those instead of instance. If that isn’t possible look for other methods to achieve the same goal, maybe try opening the file directly in C# instead of using of using the UI.