NunitWebTest.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. }
  84. if (!test) {
  85. Assert.AreEqual (_compareExpect, _compareActual, msg);
  86. }
  87. }
  88. private static bool HtmlComparer (string origin, string derived)
  89. {
  90. XmlDocument or = new XmlDocument ();
  91. MonoTests.stand_alone.WebHarness.HtmlDiff helper = new MonoTests.stand_alone.WebHarness.HtmlDiff ();
  92. or.LoadXml (helper.HtmltoXml (origin));
  93. XmlDocument dr = new XmlDocument ();
  94. dr.LoadXml (helper.HtmltoXml (derived));
  95. return helper.XmlCompare (or, dr, false);
  96. }
  97. private bool XmlCompare(XmlDocument expected, XmlDocument actual, bool ignoreAlmost)
  98. {
  99. XmlComparer comparer = new XmlComparer();
  100. if (ignoreAlmost == false)
  101. {
  102. DoAlmost(expected);
  103. DoAlmost(actual);
  104. }
  105. bool c = comparer.AreEqual(expected, actual);
  106. _compareStatus = comparer.LastCompare;
  107. _compareActual = comparer.Actual;
  108. _compareExpect = comparer.Expected;
  109. return c;
  110. }
  111. public string HtmltoXml (string html) //throws XmlException
  112. {
  113. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument ();
  114. doc.LoadHtml (html.Trim (new char[] { '\r', '\n', ' ' })); // bug in HtmlAgilityPack
  115. StringBuilder fixedxml = new StringBuilder ();
  116. StringWriter sw = new StringWriter (fixedxml);
  117. StringBuilder tempxml = new StringBuilder ();
  118. StringWriter tsw = new StringWriter (tempxml);
  119. doc.OptionOutputAsXml = true;
  120. doc.Save (tsw);
  121. // fix style attribute
  122. // the reason is that style attribute name-value pairs come in different order
  123. // in .NET and GH
  124. // Here I will sort the values of style attribute
  125. XmlDocument tempDoc = new XmlDocument ();
  126. tempDoc.LoadXml (tempxml.ToString ());
  127. XmlNodeList allNodes = tempDoc.SelectNodes ("//*");
  128. foreach (XmlNode n in allNodes) {
  129. if (n.Attributes["style"] != null) {
  130. string att = n.Attributes["style"].Value;
  131. string[] style = att.Trim (new char[] { ' ', ';' }).Split (';');
  132. for (int styleIndex = 0; styleIndex < style.Length; styleIndex++) {
  133. style[styleIndex] = FixStyleNameValue (style[styleIndex]);
  134. }
  135. Array.Sort (style);
  136. n.Attributes["style"].Value = string.Join (";", style);
  137. }
  138. }
  139. tempDoc.Save (sw);
  140. return fixedxml.ToString ();
  141. }
  142. private string FixStyleNameValue(string nameValue)
  143. {
  144. string [] nv = nameValue.Split(':');
  145. // value may contain spaces in case of
  146. // multiple values for one key
  147. string [] nvalue = nv[1].Trim().Split(' ');
  148. Array.Sort(nvalue);
  149. nv[1] = string.Join(" ", nvalue);
  150. return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
  151. }
  152. private void DoAlmost(XmlDocument xmlDocument)
  153. {
  154. XmlNode XmlIgnoreNode;
  155. IEnumerator xmlIgnoreEnum;
  156. if (_xmlIgnoreList == null)
  157. {
  158. _xmlIgnoreList = new XmlDocument();
  159. string xml;
  160. Stream source = Assembly.GetExecutingAssembly ()
  161. .GetManifestResourceStream ("HtmlCompare.nunitweb_config.xml");
  162. if (source == null) {
  163. source = Assembly.GetExecutingAssembly ()
  164. .GetManifestResourceStream ("nunitweb_config.xml");
  165. }
  166. try {
  167. using (StreamReader sr = new StreamReader (source))
  168. xml = sr.ReadToEnd ();
  169. }
  170. finally {
  171. source.Close ();
  172. }
  173. _xmlIgnoreList.LoadXml (xml);
  174. }
  175. // Remove by Id or Name
  176. // search by tag and if id or name match, remove all attributes
  177. // must be the first almost since the following almost delete the id and name
  178. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
  179. while (xmlIgnoreEnum.MoveNext())
  180. {
  181. XmlNodeList DocNodeList;
  182. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  183. DocNodeList = xmlDocument.GetElementsByTagName("*");
  184. if (DocNodeList != null)
  185. {
  186. foreach (XmlElement tmpXmlElement in DocNodeList)
  187. {
  188. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  189. {
  190. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  191. {
  192. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  193. {
  194. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
  195. {
  196. tmpXmlElement.RemoveAllAttributes();
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203. }
  204. // remove ignored attributes
  205. // search for tag and remove it's attributes
  206. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
  207. while (xmlIgnoreEnum.MoveNext())
  208. {
  209. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  210. XmlNodeList DocNodeList;
  211. //clean specific element
  212. DocNodeList = xmlDocument.GetElementsByTagName("*");
  213. if (DocNodeList != null)
  214. {
  215. foreach (XmlElement tmpXmlElement in DocNodeList)
  216. {
  217. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  218. {
  219. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  220. {
  221. tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
  222. }
  223. }
  224. }
  225. }
  226. }
  227. // clean javascript attribute value
  228. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
  229. while (xmlIgnoreEnum.MoveNext())
  230. {
  231. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  232. XmlNodeList DocNodeList;
  233. //clean Java Script attribute values
  234. DocNodeList = xmlDocument.GetElementsByTagName("*");
  235. if (DocNodeList != null)
  236. {
  237. foreach (XmlElement tmpXmlElement in DocNodeList)
  238. {
  239. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  240. {
  241. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  242. {
  243. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  244. {
  245. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower().IndexOf("javascript") >= 0 )
  246. {
  247. tmpXmlElement.SetAttribute(tmpIgnoreAttr.Name, "");
  248. }
  249. }
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }