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 @ 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;
        }
    }
}

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

  1. 

    Very useful.Thank you very much

    Like

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 […]

    Like

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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