NunitWebTest.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // Authors:
  3. // Rafael Mizrahi <[email protected]>
  4. // Erez Lotan <[email protected]>
  5. // Vladimir Krasnov <[email protected]>
  6. //
  7. //
  8. // Copyright (c) 2002-2005 Mainsoft Corporation.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.IO;
  31. using System.Xml;
  32. using System.Net;
  33. using System.Text;
  34. using System.Collections;
  35. using System.Reflection;
  36. using NUnit.Framework;
  37. namespace MonoTests.stand_alone.WebHarness
  38. {
  39. public class HtmlDiff
  40. {
  41. public const string BEGIN_TAG = "begint";
  42. public const string END_TAG = "endt";
  43. private XmlDocument _xmlIgnoreList = null;
  44. private string _compareStatus = "";
  45. private static string _compareActual = "";
  46. private static string _compareExpect = "";
  47. private string _ignoreListFile = "";
  48. public HtmlDiff()
  49. {
  50. }
  51. public string IgnoreListFile
  52. {
  53. get {return _ignoreListFile;}
  54. set {_ignoreListFile = value;}
  55. }
  56. public string CompareStatus
  57. {
  58. get {return _compareStatus.ToString();}
  59. }
  60. public static string GetControlFromPageHtml (string str)
  61. {
  62. if (str == null || str == string.Empty)
  63. throw new ArgumentException ("internal error: str is null or empty");
  64. int beginPos = str.IndexOf (BEGIN_TAG);
  65. int endPos = str.IndexOf (END_TAG);
  66. if (beginPos == -1)
  67. throw new Exception ("internal error: BEGIN_TAG is missing. Full source: "+str);
  68. if (endPos == -1)
  69. throw new Exception ("internal error: END_TAG is missing. Full source: "+str);
  70. StringBuilder sb = new StringBuilder ();
  71. sb.Append (str.Substring (beginPos + BEGIN_TAG.Length, endPos - beginPos - BEGIN_TAG.Length));
  72. return sb.ToString ();
  73. }
  74. public static void AssertAreEqual (string origin, string derived, string msg)
  75. {
  76. bool test = false;
  77. try {
  78. test = HtmlComparer (origin, derived);
  79. }
  80. catch (Exception e) {
  81. //swallow e when there is XML error and fallback
  82. //to the text comparison
  83. Assert.AreEqual (origin, derived, msg);
  84. }
  85. if (!test) {
  86. Assert.AreEqual (_compareExpect, _compareActual, msg);
  87. }
  88. }
  89. private static bool HtmlComparer (string origin, string derived)
  90. {
  91. XmlDocument or = new XmlDocument ();
  92. MonoTests.stand_alone.WebHarness.HtmlDiff helper = new MonoTests.stand_alone.WebHarness.HtmlDiff ();
  93. or.LoadXml (helper.HtmltoXml (origin));
  94. XmlDocument dr = new XmlDocument ();
  95. dr.LoadXml (helper.HtmltoXml (derived));
  96. return helper.XmlCompare (or, dr, false);
  97. }
  98. private bool XmlCompare(XmlDocument expected, XmlDocument actual, bool ignoreAlmost)
  99. {
  100. XmlComparer comparer = new XmlComparer();
  101. if (ignoreAlmost == false)
  102. {
  103. DoAlmost(expected);
  104. DoAlmost(actual);
  105. }
  106. bool c = comparer.AreEqual(expected, actual);
  107. _compareStatus = comparer.LastCompare;
  108. _compareActual = comparer.Actual;
  109. _compareExpect = comparer.Expected;
  110. return c;
  111. }
  112. public string HtmltoXml (string html) //throws XmlException
  113. {
  114. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument ();
  115. doc.LoadHtml (html.Trim (new char[] { '\r', '\n', ' ' })); // bug in HtmlAgilityPack
  116. StringBuilder fixedxml = new StringBuilder ();
  117. StringWriter sw = new StringWriter (fixedxml);
  118. StringBuilder tempxml = new StringBuilder ();
  119. StringWriter tsw = new StringWriter (tempxml);
  120. doc.OptionOutputAsXml = true;
  121. doc.Save (tsw);
  122. // fix style attribute
  123. // the reason is that style attribute name-value pairs come in different order
  124. // in .NET and GH
  125. // Here I will sort the values of style attribute
  126. XmlDocument tempDoc = new XmlDocument ();
  127. tempDoc.LoadXml (tempxml.ToString ());
  128. XmlNodeList allNodes = tempDoc.SelectNodes ("//*");
  129. foreach (XmlNode n in allNodes) {
  130. if (n.Attributes["style"] != null) {
  131. string att = n.Attributes["style"].Value;
  132. string[] style = att.Trim (new char[] { ' ', ';' }).Split (';');
  133. for (int styleIndex = 0; styleIndex < style.Length; styleIndex++) {
  134. style[styleIndex] = FixStyleNameValue (style[styleIndex]);
  135. }
  136. Array.Sort (style);
  137. n.Attributes["style"].Value = string.Join (";", style);
  138. }
  139. }
  140. tempDoc.Save (sw);
  141. return fixedxml.ToString ();
  142. }
  143. private string FixStyleNameValue(string nameValue)
  144. {
  145. string [] nv = nameValue.Split(':');
  146. // value may contain spaces in case of
  147. // multiple values for one key
  148. string [] nvalue = nv[1].Trim().Split(' ');
  149. Array.Sort(nvalue);
  150. nv[1] = string.Join(" ", nvalue);
  151. return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
  152. }
  153. private void DoAlmost(XmlDocument xmlDocument)
  154. {
  155. XmlNode XmlIgnoreNode;
  156. IEnumerator xmlIgnoreEnum;
  157. if (_xmlIgnoreList == null)
  158. {
  159. _xmlIgnoreList = new XmlDocument();
  160. string xml;
  161. Stream source = Assembly.GetExecutingAssembly ()
  162. .GetManifestResourceStream ("HtmlCompare.nunitweb_config.xml");
  163. if (source == null) {
  164. source = Assembly.GetExecutingAssembly ()
  165. .GetManifestResourceStream ("nunitweb_config.xml");
  166. }
  167. try {
  168. using (StreamReader sr = new StreamReader (source))
  169. xml = sr.ReadToEnd ();
  170. }
  171. finally {
  172. source.Close ();
  173. }
  174. _xmlIgnoreList.LoadXml (xml);
  175. }
  176. // Remove by Id or Name
  177. // search by tag and if id or name match, remove all attributes
  178. // must be the first almost since the following almost delete the id and name
  179. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
  180. while (xmlIgnoreEnum.MoveNext())
  181. {
  182. XmlNodeList DocNodeList;
  183. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  184. DocNodeList = xmlDocument.GetElementsByTagName("*");
  185. if (DocNodeList != null)
  186. {
  187. foreach (XmlElement tmpXmlElement in DocNodeList)
  188. {
  189. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  190. {
  191. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  192. {
  193. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  194. {
  195. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
  196. {
  197. tmpXmlElement.RemoveAllAttributes();
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. // remove ignored attributes
  206. // search for tag and remove it's attributes
  207. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
  208. while (xmlIgnoreEnum.MoveNext())
  209. {
  210. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  211. XmlNodeList DocNodeList;
  212. //clean specific element
  213. DocNodeList = xmlDocument.GetElementsByTagName("*");
  214. if (DocNodeList != null)
  215. {
  216. foreach (XmlElement tmpXmlElement in DocNodeList)
  217. {
  218. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  219. {
  220. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  221. {
  222. tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
  223. }
  224. }
  225. }
  226. }
  227. }
  228. // clean javascript attribute value
  229. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
  230. while (xmlIgnoreEnum.MoveNext())
  231. {
  232. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  233. XmlNodeList DocNodeList;
  234. //clean Java Script attribute values
  235. DocNodeList = xmlDocument.GetElementsByTagName("*");
  236. if (DocNodeList != null)
  237. {
  238. foreach (XmlElement tmpXmlElement in DocNodeList)
  239. {
  240. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  241. {
  242. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  243. {
  244. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  245. {
  246. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower().IndexOf("javascript") >= 0 )
  247. {
  248. tmpXmlElement.SetAttribute(tmpIgnoreAttr.Name, "");
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. }