2
0

NunitWebTest.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 abstract class XmlComparableTest
  40. {
  41. public abstract bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost);
  42. }
  43. public class HtmlDiff : XmlComparableTest
  44. {
  45. public const string BEGIN_TAG = "begint";
  46. public const string END_TAG = "endt";
  47. private XmlDocument _xmlIgnoreList = null;
  48. private string _compareStatus = "";
  49. private static string _compareActual = "";
  50. private static string _compareExpect = "";
  51. private string _ignoreListFile = "";
  52. public HtmlDiff()
  53. {
  54. }
  55. public string IgnoreListFile
  56. {
  57. get {return _ignoreListFile;}
  58. set {_ignoreListFile = value;}
  59. }
  60. public string CompareStatus
  61. {
  62. get {return _compareStatus.ToString();}
  63. }
  64. public static string GetControlFromPageHtml (string str)
  65. {
  66. if (str == null || str == string.Empty)
  67. throw new ArgumentException ("internal error: str is null or empty");
  68. int beginPos = str.IndexOf (BEGIN_TAG);
  69. int endPos = str.IndexOf (END_TAG);
  70. if (beginPos == -1)
  71. throw new Exception ("internal error: BEGIN_TAG is missing. Full source: "+str);
  72. if (endPos == -1)
  73. throw new Exception ("internal error: END_TAG is missing. Full source: "+str);
  74. StringBuilder sb = new StringBuilder ();
  75. sb.Append (str.Substring (beginPos + BEGIN_TAG.Length, endPos - beginPos - BEGIN_TAG.Length));
  76. return sb.ToString ();
  77. }
  78. public static void AssertAreEqual (string origin, string derived, string msg)
  79. {
  80. bool test = HtmlComparer (origin, derived);
  81. if (!test) {
  82. Assert.AreEqual (_compareExpect, _compareActual, msg);
  83. }
  84. }
  85. public static bool HtmlComparer (string origin, string derived)
  86. {
  87. XmlDocument or = new XmlDocument ();
  88. MonoTests.stand_alone.WebHarness.HtmlDiff helper = new MonoTests.stand_alone.WebHarness.HtmlDiff ();
  89. or.LoadXml (helper.HtmltoXml (origin));
  90. XmlDocument dr = new XmlDocument ();
  91. dr.LoadXml (helper.HtmltoXml (derived));
  92. return helper.XmlCompare (or, dr, false);
  93. }
  94. public override bool XmlCompare(XmlDocument expected, XmlDocument actual, bool ignoreAlmost)
  95. {
  96. XmlComparer comparer = new XmlComparer();
  97. if (ignoreAlmost == false)
  98. {
  99. DoAlmost(expected);
  100. DoAlmost(actual);
  101. }
  102. bool c = comparer.AreEqual(expected, actual);
  103. _compareStatus = comparer.LastCompare;
  104. _compareActual = comparer.Actual;
  105. _compareExpect = comparer.Expected;
  106. return c;
  107. }
  108. public string HtmltoXml(string html)
  109. {
  110. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
  111. doc.LoadHtml (html.Trim (new char [] { '\r', '\n', ' ' })); // bug in HtmlAgilityPack
  112. StringBuilder fixedxml = new StringBuilder();
  113. StringWriter sw = new StringWriter(fixedxml);
  114. try
  115. {
  116. StringBuilder tempxml = new StringBuilder();
  117. StringWriter tsw = new StringWriter(tempxml);
  118. doc.OptionOutputAsXml = true;
  119. doc.Save(tsw);
  120. // fix style attribute
  121. // the reason is that style attribute name-value pairs come in different order
  122. // in .NET and GH
  123. // Here I will sort the values of style attribute
  124. XmlDocument tempDoc = new XmlDocument();
  125. tempDoc.LoadXml(tempxml.ToString());
  126. XmlNodeList allNodes = tempDoc.SelectNodes("//*");
  127. foreach (XmlNode n in allNodes)
  128. {
  129. if (n.Attributes["style"] != null)
  130. {
  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. {
  135. style[styleIndex] = FixStyleNameValue(style[styleIndex]);
  136. }
  137. Array.Sort(style);
  138. n.Attributes["style"].Value = string.Join(";", style);
  139. }
  140. }
  141. tempDoc.Save(sw);
  142. }
  143. catch (Exception)
  144. {
  145. Console.WriteLine("Error parsing html response...");
  146. Console.WriteLine("Test case aborted");
  147. return "<TestCaseAborted></TestCaseAborted>";
  148. }
  149. return fixedxml.ToString();
  150. }
  151. private string FixStyleNameValue(string nameValue)
  152. {
  153. string [] nv = nameValue.Split(':');
  154. // value may contain spaces in case of
  155. // multiple values for one key
  156. string [] nvalue = nv[1].Trim().Split(' ');
  157. Array.Sort(nvalue);
  158. nv[1] = string.Join(" ", nvalue);
  159. return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
  160. }
  161. private void DoAlmost(XmlDocument xmlDocument)
  162. {
  163. XmlNode XmlIgnoreNode;
  164. IEnumerator xmlIgnoreEnum;
  165. if (_xmlIgnoreList == null)
  166. {
  167. _xmlIgnoreList = new XmlDocument();
  168. string xml;
  169. Stream source = Assembly.GetExecutingAssembly ()
  170. .GetManifestResourceStream ("HtmlCompare.nunitweb_config.xml");
  171. if (source == null) {
  172. source = Assembly.GetExecutingAssembly ()
  173. .GetManifestResourceStream ("nunitweb_config.xml");
  174. }
  175. try {
  176. using (StreamReader sr = new StreamReader (source))
  177. xml = sr.ReadToEnd ();
  178. }
  179. finally {
  180. source.Close ();
  181. }
  182. _xmlIgnoreList.LoadXml (xml);
  183. }
  184. // Remove by Id or Name
  185. // search by tag and if id or name match, remove all attributes
  186. // must be the first almost since the following almost delete the id and name
  187. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
  188. while (xmlIgnoreEnum.MoveNext())
  189. {
  190. XmlNodeList DocNodeList;
  191. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  192. DocNodeList = xmlDocument.GetElementsByTagName("*");
  193. if (DocNodeList != null)
  194. {
  195. foreach (XmlElement tmpXmlElement in DocNodeList)
  196. {
  197. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  198. {
  199. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  200. {
  201. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  202. {
  203. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
  204. {
  205. tmpXmlElement.RemoveAllAttributes();
  206. }
  207. }
  208. }
  209. }
  210. }
  211. }
  212. }
  213. // remove ignored attributes
  214. // search for tag and remove it's attributes
  215. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
  216. while (xmlIgnoreEnum.MoveNext())
  217. {
  218. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  219. XmlNodeList DocNodeList;
  220. //clean specific element
  221. DocNodeList = xmlDocument.GetElementsByTagName("*");
  222. if (DocNodeList != null)
  223. {
  224. foreach (XmlElement tmpXmlElement in DocNodeList)
  225. {
  226. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  227. {
  228. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  229. {
  230. tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
  231. }
  232. }
  233. }
  234. }
  235. }
  236. // clean javascript attribute value
  237. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
  238. while (xmlIgnoreEnum.MoveNext())
  239. {
  240. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  241. XmlNodeList DocNodeList;
  242. //clean Java Script attribute values
  243. DocNodeList = xmlDocument.GetElementsByTagName("*");
  244. if (DocNodeList != null)
  245. {
  246. foreach (XmlElement tmpXmlElement in DocNodeList)
  247. {
  248. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  249. {
  250. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  251. {
  252. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  253. {
  254. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower().IndexOf("javascript") >= 0 )
  255. {
  256. tmpXmlElement.SetAttribute(tmpIgnoreAttr.Name, "");
  257. }
  258. }
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. }