10/1/2009 22:01 El Bruno

[VSTS2010] Otro ejemplo de UI Automation Test interactuando con Windows Explorer

Buenas

sigo trabajando y probando el nuevo UI Automation en la máquina virtual de Visual Studio Team System 2010, y he querido sacarme una espina que tenia desde hace tiempo cuando he probado otros framework para la automatización de pruebas de UI: la interaccion con Windows Explorer.

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.

   1:  [TestMethod]
   2:  public void ExplorerTest()
   3:  {
   4:    string folderPath = @"C:\temp";
   5:    string documentName = @"demo.txt";
   6:   
   7:    // delete document if exists
   8:    if(File.Exists(Path.Combine(folderPath, documentName)))
   9:      File.Delete(Path.Combine(folderPath, documentName));
  10:   
  11:    // create document through windows explorer
  12:    RecordedMethods.TestExplorer(folderPath, documentName);
  13:   
  14:    // validate if the document was created
  15:    Assert.IsTrue(File.Exists(Path.Combine(folderPath, documentName)));
  16:  }

 

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.


Video: VSTS2010 CodedUITest Explorer Code Demo

 

Saludos @ Home

El Bruno

Crossposting from ElBruno.com
Archivado en: ,
Comparte este post:

# [VSTS2010] Listado de Novedades (VIII)

Monday, June 15, 2009 3:36 PM by El Bruno

Buenas, ya vamos por el 8vo recopilatorio con los posts que voy escribiendo sobre las novedades de Visual

# [VSTS2010] Listado de Novedades (X)

Monday, September 21, 2009 6:39 PM by El Bruno

Buenas, y llegó el 10mo recopilatorio con los posts que voy escribiendo sobre las novedades de

# [VSTS2010] Listado de Novedades (XI)

Tuesday, December 22, 2009 11:03 AM by El Bruno

Buenas, 11vo recopilatorio con los posts que voy escribiendo sobre las novedades de Visual Studio 2010

# [VSTS2010] Listado de Novedades (XII)

Tuesday, January 19, 2010 1:46 PM by El Bruno

Buenas, nuevo recopilatorio con los posts que voy escribiendo sobre las novedades de Visual Studio 2010