When using Coded UI Tests (CUIT) to test large applications, it’s recommended to create individual UIMaps for each screen.
I used the following container to configure multiple UIMaps to point to the same root element. I can use the container to fetch the UIMaps when I need them.
Using this container can help build maintainable Coded UI Tests.
Get the code @ https://github.com/brisebois/Coded.UI.Test.Demoware
public class UIMapContainer<TRootUIMap> { private readonly TRootUIMap uiMap; private readonly Dictionary<Type, object> uiMaps; public UIMapContainer() { uiMaps = new Dictionary<Type, object>(); uiMap = Activator.CreateInstance<TRootUIMap>(); } public UIMapContainer<TRootUIMap> Configure<TUIMap>( Func<TRootUIMap, UITestControl> rootUiControl, Func<TUIMap, UITestControl> targetUiControl) { var type = typeof(TUIMap); var requestedUIMap = Activator.CreateInstance<TUIMap>(); targetUiControl(requestedUIMap).CopyFrom(rootUiControl(uiMap)); uiMaps.Add(type, requestedUIMap); return this; } public TRootUIMap UIMap { get { return uiMap; } } public TUIMap Get<TUIMap>() { var type = typeof(TUIMap); if (uiMaps.ContainsKey(type)) return (TUIMap)uiMaps[type]; throw new UIMapNotFound(type.FullName); } public class UIMapNotFound : Exception { public UIMapNotFound(string fullName) : base("UIMap is not configured : " + fullName) { } } }
The following example demonstrates how to use the container and how to configure the UIMaps.
[CodedUITest] public class NotePadCodedUITest { private TestContext testContextInstance; private readonly UIMapContainer<NotePad> container; public NotePadCodedUITest() { container = new UIMapContainer<NotePad>(); container .Configure<AboutWindow>(m => m.UINewTextDocumentNotepWindow , r=> r.UINewTextDocumentNotepWindow); } [TestMethod] public void OpenNotePadAndViewAboutWindow() { container.UIMap.OpenNotePad(); container.UIMap.OpenAboutWindow(); var aboutWindow = container.Get<AboutWindow>(); aboutWindow.AssertNotePadVersion(); aboutWindow.CloseAboutWindow(); } #region Additional test attributes #endregion /// <summary> ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///</summary> public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } }
Very useful.Thank you very much
LikeLike