Si alguna vez has tratado de interactuar con el Explorer, sabrás que los controles internos que posee (treeview, listview, etc) son un poco particulares y da bastante trabajo trabajar con los mismos.
He pensado la siguiente prueba unitaria donde a partir de un directorio y un nombre de archivo, creo un nuevo archivo de texto en desde Windows Explorer y luego valido si se ha creado este archivo.
Como se puede ver en el siguiente ejemplo, la prueba queda muy simple.
Pero lo que no es tan simple es el código del UI Automation. En primer lugar hay que identificar una instancia de Windows Explorer y como el caption no es estático, hay que recurrir a algún método alternativo (que todavía no encontré). Luego identificar la barra de direcciones y escribir en la misma la ubicación a la que queremos acceder. Finalmente desplegar el menú contextual y seleccionar la opción de crear un nuevo documento de texto.
1: public static void TestExplorer(string folderPath, string documentName)
2: {
3: // Click 'Computer' title bar
4: UITestControl computerWindow = new UITestControl();
5: computerWindow.FrameworkName = "MSAA";
6: computerWindow.PrimarySearchProperties.Add("Name", "Computer");
7: computerWindow.PrimarySearchProperties.Add("ClassName", "CabinetWClass");
8: computerWindow.SearchScope = SearchScope.VisibleOnly;
9: computerWindow.Find();
10:
11: // Click 'Address: ' in the tool bar
12: UITestControl addressComputerWindow = new UITestControl(computerWindow);
13: addressComputerWindow.FrameworkName = "MSAA";
14: addressComputerWindow.PrimarySearchProperties.Add("ControlId", "1001");
15: addressComputerWindow.SearchScope = SearchScope.VisibleOnly;
16: addressComputerWindow.Find();
17:
18: // Type folderPath in 'Address' text box
19: UITestControl addressWindow1 = new UITestControl(computerWindow);
20: addressWindow1.FrameworkName = "MSAA";
21: addressWindow1.PrimarySearchProperties.Add("ControlId", "41477");
22: addressWindow1.PrimarySearchProperties.Add("Instance", "4");
23: addressWindow1.Find();
24: WinEdit addressEdit = new WinEdit(addressWindow1);
25: addressEdit.PrimarySearchProperties.Add("Name", "Address");
26: addressEdit.SearchScope = SearchScope.VisibleOnly;
27: addressEdit.Find();
28: addressEdit.SendKeys(folderPath);
29:
30: // Type '{Enter}' in 'Address' text box
31: addressEdit.SendKeys("{Enter}");
32:
33:
34: // Right-click 'Unknown Name' list box
35: UITestControl folderViewWindow = new UITestControl(computerWindow);
36: folderViewWindow.FrameworkName = "MSAA";
37: folderViewWindow.PrimarySearchProperties.Add("ControlId", "1");
38: folderViewWindow.SearchScope = SearchScope.VisibleOnly;
39: folderViewWindow.Find();
40:
41: WinList itemList = new WinList(folderViewWindow);
42: itemList.PrimarySearchProperties.Add("ClassName", "SysListView32");
43: itemList.SearchScope = SearchScope.VisibleOnly;
44: itemList.Find();
45: itemList.Click(MouseButtons.Right, ModifierKeys.None, new Point(53, 248));
46:
47: // Click 'New' -> 'Text Document' menu item
48: UITestControl contextPopupMenu = new UITestControl();
49: contextPopupMenu.FrameworkName = "MSAA";
50: contextPopupMenu.PrimarySearchProperties.Add("AccessibleName", "Context");
51: contextPopupMenu.PrimarySearchProperties.Add("Role", "popup menu");
52: contextPopupMenu.SearchScope = SearchScope.VisibleOnly;
53: contextPopupMenu.ExpandWhileSearching = true;
54: contextPopupMenu.Find();
55: UITestControl newMenuItem = new UITestControl(contextPopupMenu);
56: newMenuItem.FrameworkName = "MSAA";
57: newMenuItem.PrimarySearchProperties.Add("Name", "New");
58: newMenuItem.PrimarySearchProperties.Add("Role", "menu item");
59: newMenuItem.ExpandWhileSearching = true;
60: newMenuItem.Find();
61: WinMenuItem textDocumentMenuItem = new WinMenuItem(newMenuItem);
62: textDocumentMenuItem.PrimarySearchProperties.Add("Name", "Text Document");
63: textDocumentMenuItem.ExpandWhileSearching = true;
64: textDocumentMenuItem.Find();
65: textDocumentMenuItem.Click();
66:
67: // Type document name in 'New Document' inline text box
68: UITestControl folderViewWindow1 = new UITestControl(computerWindow);
69: folderViewWindow1.FrameworkName = "MSAA";
70: folderViewWindow1.PrimarySearchProperties.Add("Role", "window");
71: folderViewWindow1.PrimarySearchProperties.Add("ClassName", "SysListView32");
72: folderViewWindow1.SearchScope = SearchScope.VisibleOnly;
73: folderViewWindow1.Find();
74: WinEdit itemEdit = new WinEdit(folderViewWindow1);
75: itemEdit.PrimarySearchProperties.Add("ClassName", "Edit");
76: itemEdit.SearchScope = SearchScope.VisibleOnly;
77: itemEdit.Find();
78: itemEdit.Value = documentName;
79:
80: // Type '{Enter}' to set the value
81: itemEdit.SendKeys("{Enter}");
82: }
Una vez más el video correspondiente.