/// <summary> /// Finds the UI object in UIMap file. /// </summary> /// <param name="uiMapFile">The UI map file.</param> /// <param name="path">The path to UI element, e.g. "NotepadUIMap1.UIUntitledNotepadWindow.UIApplicationMenuBar.UIEditMenuItem" (i.e. including root).</param> /// <returns></returns> public UIObject FindUIObject(string path) { if (String.IsNullOrWhiteSpace(path)) { return(null); } string[] pathElements = path.Split('.'); // first two elements are UIMap and top-level window, which are handled especially UIMap uiMap = this.UITest.Maps.Where(map => map.Id == pathElements[0]).FirstOrDefault(); if (uiMap == null) { return(null); } TopLevelElement topLevelWindow = uiMap.TopLevelWindows.Where(e => e.Id == pathElements[1]).FirstOrDefault(); if (topLevelWindow == null) { return(null); } UIObject currentObject = topLevelWindow; Collection <UIObject> descendants = topLevelWindow.Descendants; // now recursively find element for (int i = 2; i < pathElements.Length; i++) { string pathElement = pathElements[i]; if (String.IsNullOrWhiteSpace(pathElement)) { continue; } currentObject = descendants.Where(e => e.Id == pathElement).FirstOrDefault(); if (currentObject == null) { return(null); } descendants = currentObject.Descendants; } return(currentObject); }
/// <summary> /// Move UI object from other UIMap into current UIMap /// </summary> /// <param name="srcUIMapFile">The source UIMap file.</param> /// <param name="srcElementPath">The source element path.</param> /// <param name="destParentPath">Path of destination parent.</param> public void MoveUIObject(UIMapFile srcUIMapFile, string srcElementPath, string destParentPath) { // source element UIObject srcElement = srcUIMapFile.FindUIObject(srcElementPath); if (srcElement == null) { throw new NullReferenceException(String.Format("Could not find source UIObject with path '{0}'", srcElementPath)); } // source element parent string srcParentPath = srcElementPath.Substring(0, srcElementPath.LastIndexOf('.')); // not supported to move actions between different files if (srcUIMapFile != this) { // find references in actions foreach (UITestAction action in srcUIMapFile.UITest.ExecuteActions.Actions) { if ((action.UIObjectName != null) && (action.UIObjectName.StartsWith(srcElementPath))) { throw new NotSupportedException("Your source UIMap file contains actions referencing elements that are moved. This is not supported"); } } } if (srcElement is TopLevelElement) { // can only be moved to root if (destParentPath.Contains('.')) { throw new ArgumentException(String.Format("A top level element ({0}) can only be moved to UIMap root", srcElementPath)); } TopLevelElement srcTopLevelElement = (TopLevelElement)srcElement; UIMap destUIMap = this.UITest.Maps.Where(m => m.Id == destParentPath).FirstOrDefault(); if (destUIMap == null) { throw new NullReferenceException(String.Format("Could not find UIMap '{0}' in destination UIMap file", destParentPath)); } UIMap srcUIMap = srcUIMapFile.UITest.Maps.Where(m => m.Id == srcParentPath).FirstOrDefault(); if (srcUIMap == null) { throw new NullReferenceException(String.Format("Could not find UIMap '{0}' in source UIMap file", srcParentPath)); } TopLevelElement destTopLevelElement = destUIMap.TopLevelWindows.Where(t => t.Id == srcTopLevelElement.Id).FirstOrDefault(); if (destTopLevelElement == null) { // doesn't exist in destination, so simply move it destUIMap.TopLevelWindows.Add(srcTopLevelElement); srcUIMap.TopLevelWindows.Remove(srcTopLevelElement); } else { // a top level element with same id already exists in destination, so we need to merge RecursivelyMergeElements(srcTopLevelElement, destTopLevelElement); srcUIMapFile.DeleteUIObject(srcElementPath); } this.IsModified = true; srcUIMapFile.IsModified = true; } else { // just a "normal" UIObject (not top-level) UIObject srcParent = srcUIMapFile.FindUIObject(srcParentPath); if (srcParent == null) { throw new NullReferenceException(String.Format("Could not find source parent UIObject with path '{0}'", srcParentPath)); } // destination element parent UIObject destParent = this.FindUIObject(destParentPath); if (destParent == null) { throw new NullReferenceException(String.Format("Could not find destination parent UIObject with path '{0}'", destParentPath)); } // find references in actions and move them string destElementPath = String.Format("{0}.{1}", destParentPath, srcElement.Id); foreach (UITestAction action in this.UITest.ExecuteActions.Actions) { if ((action.UIObjectName != null) && (action.UIObjectName.StartsWith(srcElementPath))) { action.UIObjectName = action.UIObjectName.Replace(srcElementPath, destElementPath); } } // see if element already exists in destination (if so, we need to merge) UIObject destObject = destParent.Descendants.Where(d => d.Id == srcElement.Id).FirstOrDefault(); if (destObject == null) { // just move it destParent.Descendants.Add(srcElement); srcParent.Descendants.Remove(srcElement); } else { // we need to recursively move elements RecursivelyMergeElements(srcElement, destObject); srcUIMapFile.DeleteUIObject(srcElementPath); } this.IsModified = true; srcUIMapFile.IsModified = true; } }