Coded UI Tests (CUIT) With Multiple UIMaps Using A Container

September 21, 2012 — 2 Comments

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 @

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>();


        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.

public class NotePadCodedUITest
    private TestContext testContextInstance;
    private readonly UIMapContainer<NotePad> container;

    public NotePadCodedUITest()
        container = new UIMapContainer<NotePad>();
            .Configure<AboutWindow>(m => m.UINewTextDocumentNotepWindow ,
                                    r=> r.UINewTextDocumentNotepWindow);

    public void OpenNotePadAndViewAboutWindow()

        var aboutWindow = container.Get<AboutWindow>();


    #region Additional test attributes


    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    public TestContext TestContext
            return testContextInstance;
            testContextInstance = value;

2 responses to Coded UI Tests (CUIT) With Multiple UIMaps Using A Container


    Very useful.Thank you very much


Trackbacks and Pingbacks:

  1. Coded UI testing: What is the reasoning behind creating multiple individual UIMap classes instead of just multiple partial UIMap classes? - BlogoSfera - August 12, 2015

    […] Tests on, and I have gotten the impression that creating multiple UIMaps is the way to go! Sources: UIMap container, Using multiple UIMaps, More source and the list could go […]


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s