NunitWebTest.cs 8.0 KB

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