QUnitExBrowserTestResultItem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.Collections.Generic;
  14. using System.Text.RegularExpressions;
  15. namespace CassiniDev
  16. {
  17. public abstract class CassiniDevBrowserTestFixture<T> where T : BrowserTestResultItem, new()
  18. {
  19. private string url;
  20. private TimeSpan _timeOut = TimeSpan.FromMinutes(1);
  21. private Dictionary<string, BrowserTestResultItem> _results;
  22. private string _postKey = "log.axd";
  23. public abstract WebBrowser Browser { get; }
  24. public abstract string Path { get; }
  25. public abstract string Url { get; }
  26. public TimeSpan TimeOut
  27. {
  28. get { return _timeOut; }
  29. set { _timeOut = value; }
  30. }
  31. public Dictionary<string, BrowserTestResultItem> Results
  32. {
  33. get { return _results; }
  34. }
  35. public string PostKey
  36. {
  37. get { return _postKey; }
  38. set { _postKey = value; }
  39. }
  40. public void RunTest()
  41. {
  42. //ContentLocator locator = new ContentLocator(@"RESTWebServices\RESTWebServices");
  43. var test = new CassiniDevBrowserTest(PostKey);
  44. test.StartServer(Path);
  45. url = test.NormalizeUrl(Url);
  46. var testResults = new BrowserTestResults(test.RunTest(url, Browser, TimeOut));
  47. var results = new T();
  48. results.Parse(testResults.Log);
  49. _results = results.Items;
  50. test.StopServer();
  51. }
  52. }
  53. /// <summary>
  54. /// NOTE: there seems to be a 7k limit on data posted from the test so
  55. /// be concious of the data you log
  56. /// </summary>
  57. [Serializable]
  58. public class QUnitExBrowserTestResultItem : BrowserTestResultItem
  59. {
  60. private static readonly Regex rx = new Regex(
  61. @"failures\s*=\s*(?<failures>\d+)\s*;\s*total\s*=\s*(?<total>\d+)",
  62. RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
  63. public override void Parse(string log)
  64. {
  65. // parse it line by line
  66. var lines = log.Split(new[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
  67. Log.AddRange(lines);
  68. BrowserTestResultItem currentItem = this;
  69. BrowserTestResultItem lastItem = null;
  70. int index = 0;
  71. while (index < lines.Length - 1)
  72. {
  73. var line = lines[index];
  74. if (line.StartsWith("Module Started:") || line.StartsWith(" Test Started:"))
  75. {
  76. lastItem = currentItem;
  77. currentItem = new BrowserTestResultItem
  78. {
  79. Name = line.Substring("Module Started:".Length + 1)
  80. };
  81. if (lastItem == null)
  82. {
  83. throw new Exception("lst item is null?");
  84. }
  85. lastItem.Items.Add(currentItem.Name, currentItem);
  86. }
  87. else if (line.StartsWith("Module Done:") || line.StartsWith(" Test Done:"))
  88. {
  89. SetCount(currentItem, line);
  90. currentItem = lastItem;
  91. }
  92. else
  93. {
  94. if (currentItem == null)
  95. {
  96. throw new Exception("log parse exception");
  97. }
  98. currentItem.Log.Add(line);
  99. }
  100. index++;
  101. }
  102. SetCount(this, lines[lines.Length - 1]);
  103. }
  104. private static void SetCount(BrowserTestResultItem item, string value)
  105. {
  106. int total, failures;
  107. ParseCount(value, out total, out failures);
  108. item.Total = total;
  109. item.Failures = failures;
  110. item.Success = failures == 0;
  111. }
  112. private static void ParseCount(string value, out int total, out int failures)
  113. {
  114. var match = rx.Match(value);
  115. total = Convert.ToInt32(match.Groups["total"].Value);
  116. failures = Convert.ToInt32(match.Groups["failures"].Value);
  117. }
  118. }
  119. }