WebTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 XmlDocument GetTestXml(TestInfo testInfo);
  40. public abstract bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost);
  41. }
  42. public class HtmlDiff : XmlComparableTest
  43. {
  44. private string _testsBaseUrl = "";
  45. private string _ignoreListFile = "";
  46. private XmlDocument _xmlIgnoreList = null;
  47. private string _compareStatus = "";
  48. public HtmlDiff()
  49. {
  50. }
  51. public string TestsBaseUrl
  52. {
  53. get {return _testsBaseUrl;}
  54. set {_testsBaseUrl = value;}
  55. }
  56. public string IgnoreListFile
  57. {
  58. get {return _ignoreListFile;}
  59. set {_ignoreListFile = value;}
  60. }
  61. public string CompareStatus
  62. {
  63. get {return _compareStatus.ToString();}
  64. }
  65. public override XmlDocument GetTestXml(TestInfo testInfo)
  66. {
  67. return BuildXml( GetSubTests( GetUrl(testInfo.Url) ), testInfo );
  68. }
  69. public override bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost)
  70. {
  71. XmlComparer comparer = new XmlComparer();
  72. if (ignoreAlmost == false)
  73. {
  74. DoAlmost(d1);
  75. DoAlmost(d2);
  76. }
  77. bool c = comparer.AreEqual(d1, d2);
  78. _compareStatus = comparer.LastCompare;
  79. return c;
  80. }
  81. //============================================================================
  82. //
  83. private string GetUrl(string url)
  84. {
  85. try
  86. {
  87. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_testsBaseUrl + url);
  88. request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
  89. request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
  90. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  91. Stream responseStream = response.GetResponseStream();
  92. StreamReader sr = new StreamReader(responseStream);
  93. string s = sr.ReadToEnd();
  94. sr.Close();
  95. return s;
  96. }
  97. catch(Exception e)
  98. {
  99. Console.WriteLine("Cannot retrieve document from url " + _testsBaseUrl + url);
  100. Console.WriteLine(e.Message);
  101. }
  102. return "";
  103. }
  104. private ArrayList GetSubTests(string s)
  105. {
  106. ArrayList subTestList = new ArrayList();
  107. int startIndex = 0;
  108. string subTest = FindSubTest(s, startIndex);
  109. while (subTest != "")
  110. {
  111. if (subTest.ToLower().IndexOf("ghtsubtest") != -1)
  112. subTestList.Add(subTest);
  113. subTest = FindSubTest(s, s.IndexOf(subTest) + subTest.Length);
  114. }
  115. return subTestList;
  116. }
  117. #region "Sub Test extraction routines"
  118. private string FindSubTest(string s, int startIndex)
  119. {
  120. int tagBeginCount = 0;
  121. int stringPosition = startIndex;
  122. int tagPosition = 0;
  123. tagPosition = GetNextDivPosition(s, stringPosition);
  124. if (tagPosition == -1) return "";
  125. if (isBeginTag(s, tagPosition))
  126. tagBeginCount++;
  127. else
  128. return "";
  129. startIndex = tagPosition;
  130. while (tagBeginCount > 0)
  131. {
  132. stringPosition = tagPosition + 1;
  133. tagPosition = GetNextDivPosition(s, stringPosition);
  134. if (tagPosition == -1) return "";
  135. if (isBeginTag(s, tagPosition))
  136. tagBeginCount++;
  137. else
  138. tagBeginCount--;
  139. }
  140. return s.Substring(startIndex, tagPosition - startIndex + 6);
  141. }
  142. private int GetNextDivPosition(string s, int startIndex)
  143. {
  144. int tagBeginPos = GetBeginDivPosition(s, startIndex);
  145. int tagEndPos = GetEndDivPosition(s, startIndex);
  146. if ((tagBeginPos == -1) && (tagEndPos == -1)) return -1;
  147. if ((tagBeginPos > 0) && (tagEndPos > 0))
  148. {
  149. if (tagBeginPos < tagEndPos)
  150. return tagBeginPos;
  151. else
  152. return tagEndPos;
  153. }
  154. else
  155. {
  156. if (tagBeginPos > tagEndPos)
  157. return tagBeginPos;
  158. else
  159. return tagEndPos;
  160. }
  161. }
  162. private int GetBeginDivPosition(string s, int startIndex)
  163. {
  164. return s.IndexOf("<div id", startIndex);
  165. }
  166. private int GetEndDivPosition(string s, int startIndex)
  167. {
  168. return s.IndexOf("</div", startIndex);
  169. }
  170. private bool isBeginTag(string tag, int pos)
  171. {
  172. return tag.Substring(pos).StartsWith("<div");
  173. }
  174. private bool isEndTag(string tag, int pos)
  175. {
  176. return tag.Substring(pos).StartsWith("</div");
  177. }
  178. #endregion
  179. private XmlDocument BuildXml(ArrayList subTests, TestInfo ti)
  180. {
  181. StringBuilder xmltext = new StringBuilder();
  182. xmltext.Append("<TestResults name=\"" + ti.Name + "\">");
  183. foreach(string st in subTests)
  184. {
  185. xmltext.Append(st);
  186. }
  187. xmltext.Append("</TestResults>");
  188. XmlDocument r = new XmlDocument();
  189. r.LoadXml(HtmltoXml(xmltext.ToString()));
  190. return r;
  191. }
  192. private string HtmltoXml(string html)
  193. {
  194. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
  195. doc.LoadHtml(html);
  196. StringBuilder fixedxml = new StringBuilder();
  197. StringWriter sw = new StringWriter(fixedxml);
  198. try
  199. {
  200. StringBuilder tempxml = new StringBuilder();
  201. StringWriter tsw = new StringWriter(tempxml);
  202. doc.OptionOutputAsXml = true;
  203. doc.Save(tsw);
  204. // fix style attribute
  205. // the reason is that style attribute name-value pairs come in different order
  206. // in .NET and GH
  207. // Here I will sort the values of style attribute
  208. XmlDocument tempDoc = new XmlDocument();
  209. tempDoc.LoadXml(tempxml.ToString());
  210. XmlNodeList allNodes = tempDoc.SelectNodes("//*");
  211. foreach (XmlNode n in allNodes)
  212. {
  213. if (n.Attributes["style"] != null)
  214. {
  215. string att = n.Attributes["style"].Value;
  216. string [] style = att.Trim(new char[]{' ', ';'}).Split(';');
  217. for (int styleIndex=0; styleIndex<style.Length; styleIndex++)
  218. {
  219. style[styleIndex] = FixStyleNameValue(style[styleIndex]);
  220. }
  221. Array.Sort(style);
  222. n.Attributes["style"].Value = string.Join(";", style);
  223. }
  224. }
  225. tempDoc.Save(sw);
  226. }
  227. catch (Exception)
  228. {
  229. Console.WriteLine("Error parsing html response...");
  230. Console.WriteLine("Test case aborted");
  231. return "<TestCaseAborted></TestCaseAborted>";
  232. }
  233. return fixedxml.ToString();
  234. }
  235. private string FixStyleNameValue(string nameValue)
  236. {
  237. string [] nv = nameValue.Split(':');
  238. // value may contain spaces in case of
  239. // multiple values for one key
  240. string [] nvalue = nv[1].Trim().Split(' ');
  241. Array.Sort(nvalue);
  242. nv[1] = string.Join(" ", nvalue);
  243. return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
  244. }
  245. private void DoAlmost(XmlDocument xmlDocument)
  246. {
  247. XmlNode XmlIgnoreNode;
  248. IEnumerator xmlIgnoreEnum;
  249. if (_xmlIgnoreList == null)
  250. {
  251. _xmlIgnoreList = new XmlDocument();
  252. _xmlIgnoreList.Load(_ignoreListFile);
  253. }
  254. // Remove by Id or Name
  255. // search by tag and if id or name match, remove all attributes
  256. // must be the first almost since the following almost delete the id and name
  257. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
  258. while (xmlIgnoreEnum.MoveNext())
  259. {
  260. XmlNodeList DocNodeList;
  261. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  262. DocNodeList = xmlDocument.GetElementsByTagName("*");
  263. if (DocNodeList != null)
  264. {
  265. foreach (XmlElement tmpXmlElement in DocNodeList)
  266. {
  267. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  268. {
  269. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  270. {
  271. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  272. {
  273. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
  274. {
  275. tmpXmlElement.RemoveAllAttributes();
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. }
  283. // remove ignored attributes
  284. // search for tag and remove it's attributes
  285. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
  286. while (xmlIgnoreEnum.MoveNext())
  287. {
  288. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  289. XmlNodeList DocNodeList;
  290. //clean specific element
  291. DocNodeList = xmlDocument.GetElementsByTagName("*");
  292. if (DocNodeList != null)
  293. {
  294. foreach (XmlElement tmpXmlElement in DocNodeList)
  295. {
  296. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  297. {
  298. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  299. {
  300. tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
  301. }
  302. }
  303. }
  304. }
  305. }
  306. // clean javascript attribute value
  307. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
  308. while (xmlIgnoreEnum.MoveNext())
  309. {
  310. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  311. XmlNodeList DocNodeList;
  312. //clean Java Script attribute values
  313. DocNodeList = xmlDocument.GetElementsByTagName("*");
  314. if (DocNodeList != null)
  315. {
  316. foreach (XmlElement tmpXmlElement in DocNodeList)
  317. {
  318. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  319. {
  320. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  321. {
  322. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  323. {
  324. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower().IndexOf("javascript") >= 0 )
  325. {
  326. tmpXmlElement.SetAttribute(tmpIgnoreAttr.Name, "");
  327. }
  328. }
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }
  335. }
  336. }