Advertisement
Dennisaa

Page.cs

May 3rd, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.59 KB | None | 0 0
  1. namespace Microsoft.Services.TestTools.UITesting.Html
  2. {
  3.     using System;
  4.     using System.ComponentModel.Composition;
  5.     using System.ComponentModel.Composition.Hosting;
  6.     using System.Diagnostics.CodeAnalysis;
  7.     using System.Linq;
  8.     using Microsoft.VisualStudio.TestTools.UITesting;
  9.  
  10.     /// <summary>
  11.     /// Page Object base class
  12.     /// </summary>
  13.     /// <remarks>
  14.     /// Use this base class to simplify the implementation of the Page Object pattern.
  15.     /// </remarks>
  16.     public abstract class Page
  17.     {
  18.  
  19.         #region launcher
  20.  
  21.         /// <summary>
  22.         /// Launches the specified page.
  23.         /// </summary>
  24.         /// <typeparam name="T">The Page to launch</typeparam>
  25.         /// <returns>The launched page</returns>
  26.         /// <example>
  27.         /// Launch the home page.
  28.         /// <code>
  29.         /// <![CDATA[
  30.         /// var home = Page.Launch<Home>();
  31.         /// ]]>
  32.         /// </code>
  33.         /// </example>
  34.         /// <seealso cref="EntryUriAttribute"/>
  35.         /// <seealso cref="BaseUriAttribute"/>
  36.         /// <seealso cref="ClearCookiesAttribute"/>
  37.         /// <seealso cref="ClearCacheAttribute"/>
  38.         public static T Launch<T>() where T : Page, new()
  39.         {
  40.             return Launch<T>(
  41.                 ShouldClearCookies<T>(),
  42.                 ShouldClearCache<T>());
  43.         }
  44.  
  45.  
  46.         public static T LaunchWindow<T>() where T : Page, new()
  47.         {
  48.             var page = new T();
  49.  
  50.             string application = null;
  51.             string applicationPath = null;
  52.  
  53.             if (HasAttribute<T, ApplicationAttribute>())
  54.             {
  55.                 application = GetAttribute<T, ApplicationAttribute>().Application;
  56.             }
  57.  
  58.             LoadConfiguration(page);
  59.             applicationPath = page.TestConfiguration.GetApplicationPath(application);
  60.  
  61.             ApplicationUnderTest window = ApplicationUnderTest.Launch(applicationPath);
  62.             page.Window = window;
  63.             return page;
  64.         }
  65.  
  66.         public static T LaunchWindow<T>(string applicationPath) where T : Page, new()
  67.         {
  68.             var page = new T();
  69.  
  70.             // ApplicationUnderTest window = ApplicationUnderTest.Launch(applicationPath);
  71.  
  72.             var window = ApplicationUnderTest.Launch("c:\\downloads\\calc.exe");
  73.  
  74.             Playback.Wait(5000);
  75.             page.Window = window;
  76.             return page;
  77.         }
  78.  
  79.  
  80.         /// <summary>
  81.         /// Launches the specified page.
  82.         /// </summary>
  83.         /// <typeparam name="T">The Page to launch</typeparam>
  84.         /// <param name="clearCookies">if set to <c>true</c> [clear cookies].</param>
  85.         /// <param name="clearCache">if set to <c>true</c> [clear cache].</param>
  86.         /// <returns>
  87.         /// The launched page
  88.         /// </returns>
  89.         /// <example>
  90.         ///  Launch the home page and override any
  91.         /// <see cref="ClearCacheAttribute"/> and <see cref="ClearCookiesAttribute"/>
  92.         /// <code>
  93.         /// <![CDATA[
  94.         /// var home = Page.Launch<Home>(false,true);
  95.         /// ]]>
  96.         /// </code>
  97.         /// </example>
  98.         /// <seealso cref="EntryUriAttribute"/>
  99.         /// <seealso cref="BaseUriAttribute"/>
  100.         /// <seealso cref="ClearCookiesAttribute"/>
  101.         /// <seealso cref="ClearCacheAttribute"/>
  102.         /// <remarks>
  103.         /// Ignores the <see cref="ClearCookiesAttribute"/> and <see cref="ClearCacheAttribute"/>
  104.         /// </remarks>
  105.         public static T Launch<T>(bool clearCookies, bool clearCache) where T : Page, new()
  106.         {
  107.             if (clearCookies)
  108.             {
  109.                 BrowserWindow.ClearCookies();
  110.             }
  111.  
  112.             if (clearCache)
  113.             {
  114.                 BrowserWindow.ClearCache();
  115.             }
  116.  
  117.             var page = new T();
  118.             LoadConfiguration(page);
  119.             Uri launchUri = GetLaunchUri(page);
  120.             var browser = BrowserWindow.Launch(launchUri);
  121.             page.Browser = browser;
  122.             return page;
  123.         }
  124.  
  125.         public static T Launch<T>(string url) where T : Page, new()
  126.         {
  127.  
  128.             var page = new T();
  129.             Uri launchUri = new Uri(url);
  130.             var browser = BrowserWindow.Launch(launchUri);
  131.             page.Browser = browser;
  132.             return page;
  133.         }
  134.  
  135.  
  136.         /// <summary>
  137.         /// Checks if the current page is decorated with the ClearCacheAttribute.
  138.         /// </summary>
  139.         /// <typeparam name="T">The type of the page</typeparam>
  140.         /// <returns>True if the browser cache should be cleared</returns>
  141.         private static bool ShouldClearCache<T>()
  142.         {
  143.             return HasAttribute<T, ClearCookiesAttribute>();
  144.         }
  145.  
  146.         /// <summary>
  147.         /// Checks if the current page is decorated with the ClearCookiesAttribute.
  148.         /// </summary>
  149.         /// <typeparam name="T">The type of the page</typeparam>
  150.         /// <returns>True if the browser cookies should be cleared</returns>
  151.         private static bool ShouldClearCookies<T>()
  152.         {
  153.             return HasAttribute<T, ClearCookiesAttribute>();
  154.         }
  155.  
  156.         /// <summary>
  157.         /// Determines whether the page has the specified attribute.
  158.         /// </summary>
  159.         /// <typeparam name="T">The page type</typeparam>
  160.         /// <typeparam name="A">The attribute type</typeparam>
  161.         /// <returns>
  162.         ///   <c>true</c> if this instance has attribute; otherwise, <c>false</c>.
  163.         /// </returns>
  164.         private static bool HasAttribute<T, A>()
  165.         {
  166.             return typeof(T).GetCustomAttributes(false).Any(x => x.GetType() == typeof(A));
  167.         }
  168.  
  169.         /// <summary>
  170.         /// Gets the attribute.
  171.         /// </summary>
  172.         /// <typeparam name="T">The page type</typeparam>
  173.         /// <typeparam name="A">The attribute type</typeparam>
  174.         /// <returns>The attribute</returns>
  175.         private static A GetAttribute<T, A>()
  176.         {
  177.             return (A)typeof(T).GetCustomAttributes(false).SingleOrDefault(x => x.GetType() == typeof(A));
  178.         }
  179.  
  180.         /// <summary>
  181.         /// Gets the launch Uri for the specified page.
  182.         /// </summary>
  183.         /// <typeparam name="T">The page type</typeparam>
  184.         /// <param name="page">The page.</param>
  185.         /// <returns>The launch Uri</returns>
  186.         private static Uri GetLaunchUri<T>(T page) where T : Page
  187.         {
  188.             string baseUriName = null;
  189.             if (HasAttribute<T, BaseUriAttribute>())
  190.             {
  191.                 baseUriName = GetAttribute<T, BaseUriAttribute>().BaseUriName;
  192.             }
  193.  
  194.             Uri baseUri = page.TestConfiguration.GetBaseUri(baseUriName);
  195.  
  196.             if (HasAttribute<T, EntryUriAttribute>())
  197.             {
  198.                 Uri relativeUri = GetAttribute<T, EntryUriAttribute>().RelativeUri;
  199.                 return new Uri(baseUri, relativeUri);
  200.             }
  201.  
  202.             return baseUri;
  203.         }
  204.  
  205.         /// <summary>
  206.         /// MEF composition container.
  207.         /// </summary>
  208.         [SuppressMessage(
  209.             "StyleCop.CSharp.OrderingRules",
  210.             "SA1201:ElementsMustAppearInTheCorrectOrder",
  211.             Justification = "Reviewed. Suppression is OK here.")]
  212.         private static CompositionContainer _container;
  213.  
  214.         /// <summary>
  215.         /// Initializes the composition container and loads the configuration
  216.         /// </summary>
  217.         /// <param name="page">
  218.         /// The page.
  219.         /// </param>
  220.         /// <typeparam name="T">The page type</typeparam>
  221.         private static void LoadConfiguration<T>(T page)
  222.         {
  223.             if (_container == null)
  224.             {
  225.                 _container = new CompositionContainer(new AssemblyCatalog(typeof(T).Assembly));
  226.             }
  227.  
  228.             _container.ComposeParts(page);
  229.         }
  230.  
  231.         #endregion
  232.  
  233.         #region instance
  234.  
  235.         /// <summary>
  236.         /// Gets or sets the test configuration.
  237.         /// </summary>
  238.         /// <value>
  239.         /// The test configuration.
  240.         /// </value>
  241.         /// <seealso cref="IWebUITestConfiguration"/>
  242.         [SuppressMessage(
  243.             "StyleCop.CSharp.OrderingRules",
  244.             "SA1201:ElementsMustAppearInTheCorrectOrder",
  245.             Justification = "Reviewed. Suppression is OK here. Logical grouping"),
  246.          Import(typeof(IWebUITestConfiguration))]
  247.         protected IWebUITestConfiguration TestConfiguration { get; set; }
  248.  
  249.         /// <summary>
  250.         /// Returns a new page instance and forwards the browser instance to the new page.
  251.         /// </summary>
  252.         /// <typeparam name="T">The <see cref="Page"/> that the parent <see cref="Page"/> navigated to</typeparam>
  253.         /// <returns>The page that was navigated to</returns>
  254.         /// <example>
  255.         /// Clicking on the Home button should havigate back to home.
  256.         /// Instead of launching Home, use NavigatedTo.
  257.         /// <code>
  258.         /// <![CDATA[
  259.         /// Browser.ClickOnButton("Home");
  260.         /// return NavigatedTo<Home>();
  261.         /// ]]>
  262.         /// </code>
  263.         /// </example>
  264.         [SuppressMessage(
  265.             "StyleCop.CSharp.OrderingRules",
  266.             "SA1202:ElementsMustBeOrderedByAccess",
  267.             Justification = "Reviewed. Suppression is OK here. Logical grouping")]
  268.         protected T NavigatedTo<T>() where T : Page, new()
  269.         {
  270.             return new T() { Browser = Browser };
  271.         }
  272.  
  273.         /// <summary>
  274.         /// Gets or sets the browser.
  275.         /// </summary>
  276.         /// <value>
  277.         /// The browser.
  278.         /// </value>
  279.         [SuppressMessage(
  280.             "StyleCop.CSharp.OrderingRules",
  281.             "SA1201:ElementsMustAppearInTheCorrectOrder",
  282.             Justification = "Reviewed. Suppression is OK here. Logical grouping")]
  283.         protected BrowserWindow Browser { get; set; }
  284.  
  285.         protected ApplicationUnderTest Window { get; set; }
  286.  
  287.         /// <summary>
  288.         /// Refreshes this instance.
  289.         /// </summary>
  290.         [SuppressMessage("StyleCop.CSharp.OrderingRules",
  291.             "SA1202:ElementsMustBeOrderedByAccess",
  292.             Justification = "Reviewed. Suppression is OK here.")]
  293.         public void Refresh()
  294.         {
  295.             Browser.Refresh();
  296.         }
  297.  
  298.         /// <summary>
  299.         /// Closes this instance.
  300.         /// </summary>
  301.         [SuppressMessage(
  302.             "StyleCop.CSharp.OrderingRules",
  303.             "SA1202:ElementsMustBeOrderedByAccess",
  304.             Justification = "Reviewed. Suppression is OK here.")]
  305.         public void Close()
  306.         {
  307.             Browser.Close();
  308.         }
  309.  
  310.         #endregion
  311.     }
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement