CassiniDevBrowserTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // /* **********************************************************************************
  2. // *
  3. // * Copyright (c) Sky Sanders. All rights reserved.
  4. // *
  5. // * This source code is subject to terms and conditions of the Microsoft Public
  6. // * License (Ms-PL). A copy of the license can be found in the license.htm file
  7. // * included in this distribution.
  8. // *
  9. // * You must not remove this notice, or any other, from this software.
  10. // *
  11. // * **********************************************************************************/
  12. using System;
  13. using System.Diagnostics;
  14. using System.Threading;
  15. using CassiniDev.ServerLog;
  16. namespace CassiniDev
  17. {
  18. /// <summary>
  19. /// A web test executor base on an idea from Nikhil Kothari's Script#
  20. /// http://projects.nikhilk.net/ScriptSharp
  21. ///
  22. /// TODO: finer grained control over browser instances.
  23. /// TODO: create parser/abstraction for RequestEventArgs
  24. /// </summary>
  25. public class CassiniDevBrowserTest : CassiniDevServer
  26. {
  27. private readonly string _postKey = "testresults.axd";
  28. public string PostKey
  29. {
  30. get
  31. {
  32. return _postKey;
  33. }
  34. }
  35. public CassiniDevBrowserTest(string postKey)
  36. {
  37. _postKey = postKey;
  38. }
  39. public CassiniDevBrowserTest()
  40. {
  41. }
  42. public RequestEventArgs RunTest(string url)
  43. {
  44. return RunTest(url, WebBrowser.InternetExplorer, TimeSpan.FromMinutes(1.0));
  45. }
  46. public RequestEventArgs RunTest(string url, WebBrowser browser)
  47. {
  48. return RunTest(url, browser, TimeSpan.FromMinutes(1.0));
  49. }
  50. public RequestEventArgs RunTest(string url, WebBrowser browser, TimeSpan timeout)
  51. {
  52. if (browser == null)
  53. {
  54. throw new ArgumentNullException("browser");
  55. }
  56. if (string.IsNullOrEmpty(browser.ExecutablePath))
  57. {
  58. throw new InvalidOperationException("The specified browser could not be located.");
  59. }
  60. if (timeout.TotalMilliseconds == 0.0)
  61. {
  62. timeout = TimeSpan.FromMinutes(1.0);
  63. }
  64. var waitHandle = new AutoResetEvent(false);
  65. RequestEventArgs result = null;
  66. Process process;
  67. EventHandler<RequestEventArgs> logEventHandler = null;
  68. EventHandler<RequestEventArgs> handler = logEventHandler;
  69. logEventHandler = delegate(object sender, RequestEventArgs e)
  70. {
  71. if (e.RequestLog.Url.ToLower().Contains(_postKey))
  72. {
  73. Server.RequestComplete -= handler;
  74. result = e;
  75. waitHandle.Set();
  76. }
  77. };
  78. try
  79. {
  80. var startInfo = new ProcessStartInfo(browser.ExecutablePath, url)
  81. {
  82. UseShellExecute = true,
  83. WindowStyle = ProcessWindowStyle.Minimized
  84. };
  85. Server.RequestComplete += logEventHandler;
  86. process = Process.Start(startInfo);
  87. }
  88. catch (Exception ex)
  89. {
  90. Server.RequestComplete -= logEventHandler;
  91. return new RequestEventArgs(Guid.Empty, new LogInfo { StatusCode = -1, Exception = ex.ToString() },
  92. new LogInfo());
  93. }
  94. bool flag = waitHandle.WaitOne(timeout);
  95. try
  96. {
  97. if (!process.CloseMainWindow())
  98. {
  99. process.Kill();
  100. }
  101. }
  102. catch
  103. {
  104. }
  105. return flag ? result : new RequestEventArgs(Guid.Empty, new LogInfo { StatusCode = -2 }, new LogInfo());
  106. }
  107. }
  108. }