NunitWebTest.cs 7.9 KB

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