WebTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 = SkipViewstate (s);
  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 SkipViewstate (string s)
  163. {
  164. int start = s.IndexOf ("<div id");
  165. int startVS = s.IndexOf ("<div>");
  166. int vs = s.IndexOf ("VIEWSTATE");
  167. int endVS = s.IndexOf ("</div>");
  168. if (startVS > 0 && startVS < vs && vs < endVS && startVS < start)
  169. return endVS + 7;
  170. return 0;
  171. }
  172. private int GetBeginDivPosition(string s, int startIndex)
  173. {
  174. return s.IndexOf("<div id", startIndex);
  175. }
  176. private int GetEndDivPosition(string s, int startIndex)
  177. {
  178. return s.IndexOf("</div", startIndex);
  179. }
  180. private bool isBeginTag(string tag, int pos)
  181. {
  182. return tag.Substring(pos).StartsWith("<div");
  183. }
  184. private bool isEndTag(string tag, int pos)
  185. {
  186. return tag.Substring(pos).StartsWith("</div");
  187. }
  188. #endregion
  189. private XmlDocument BuildXml(ArrayList subTests, TestInfo ti)
  190. {
  191. StringBuilder xmltext = new StringBuilder();
  192. xmltext.Append("<TestResults name=\"" + ti.Name + "\">");
  193. foreach(string st in subTests)
  194. {
  195. xmltext.Append(st);
  196. }
  197. xmltext.Append("</TestResults>");
  198. XmlDocument r = new XmlDocument();
  199. r.LoadXml(HtmltoXml(xmltext.ToString()));
  200. return r;
  201. }
  202. private string HtmltoXml(string html)
  203. {
  204. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
  205. doc.LoadHtml(html);
  206. StringBuilder fixedxml = new StringBuilder();
  207. StringWriter sw = new StringWriter(fixedxml);
  208. try
  209. {
  210. StringBuilder tempxml = new StringBuilder();
  211. StringWriter tsw = new StringWriter(tempxml);
  212. doc.OptionOutputAsXml = true;
  213. doc.Save(tsw);
  214. // fix style attribute
  215. // the reason is that style attribute name-value pairs come in different order
  216. // in .NET and GH
  217. // Here I will sort the values of style attribute
  218. XmlDocument tempDoc = new XmlDocument();
  219. tempDoc.LoadXml(tempxml.ToString());
  220. XmlNodeList allNodes = tempDoc.SelectNodes("//*");
  221. foreach (XmlNode n in allNodes)
  222. {
  223. if (n.Attributes["style"] != null)
  224. {
  225. string att = n.Attributes["style"].Value;
  226. string [] style = att.Trim(new char[]{' ', ';'}).Split(';');
  227. for (int styleIndex=0; styleIndex<style.Length; styleIndex++)
  228. {
  229. style[styleIndex] = FixStyleNameValue(style[styleIndex]);
  230. }
  231. Array.Sort(style);
  232. n.Attributes["style"].Value = string.Join(";", style);
  233. }
  234. }
  235. tempDoc.Save(sw);
  236. }
  237. catch (Exception)
  238. {
  239. Console.WriteLine("Error parsing html response...");
  240. Console.WriteLine("Test case aborted");
  241. return "<TestCaseAborted></TestCaseAborted>";
  242. }
  243. return fixedxml.ToString();
  244. }
  245. private string FixStyleNameValue(string nameValue)
  246. {
  247. string [] nv = nameValue.Split(':');
  248. // value may contain spaces in case of
  249. // multiple values for one key
  250. string [] nvalue = nv[1].Trim().Split(' ');
  251. Array.Sort(nvalue);
  252. nv[1] = string.Join(" ", nvalue);
  253. return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
  254. }
  255. private void DoAlmost(XmlDocument xmlDocument)
  256. {
  257. XmlNode XmlIgnoreNode;
  258. IEnumerator xmlIgnoreEnum;
  259. if (_xmlIgnoreList == null)
  260. {
  261. _xmlIgnoreList = new XmlDocument();
  262. _xmlIgnoreList.Load(_ignoreListFile);
  263. }
  264. // Remove by Id or Name
  265. // search by tag and if id or name match, remove all attributes
  266. // must be the first almost since the following almost delete the id and name
  267. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
  268. while (xmlIgnoreEnum.MoveNext())
  269. {
  270. XmlNodeList DocNodeList;
  271. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  272. DocNodeList = xmlDocument.GetElementsByTagName("*");
  273. if (DocNodeList != null)
  274. {
  275. foreach (XmlElement tmpXmlElement in DocNodeList)
  276. {
  277. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  278. {
  279. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  280. {
  281. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  282. {
  283. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
  284. {
  285. tmpXmlElement.RemoveAllAttributes();
  286. }
  287. }
  288. }
  289. }
  290. }
  291. }
  292. }
  293. // remove ignored attributes
  294. // search for tag and remove it's attributes
  295. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
  296. while (xmlIgnoreEnum.MoveNext())
  297. {
  298. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  299. XmlNodeList DocNodeList;
  300. //clean specific element
  301. DocNodeList = xmlDocument.GetElementsByTagName("*");
  302. if (DocNodeList != null)
  303. {
  304. foreach (XmlElement tmpXmlElement in DocNodeList)
  305. {
  306. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  307. {
  308. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  309. {
  310. tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
  311. }
  312. }
  313. }
  314. }
  315. }
  316. // clean javascript attribute value
  317. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
  318. while (xmlIgnoreEnum.MoveNext())
  319. {
  320. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  321. XmlNodeList DocNodeList;
  322. //clean Java Script attribute values
  323. DocNodeList = xmlDocument.GetElementsByTagName("*");
  324. if (DocNodeList != null)
  325. {
  326. foreach (XmlElement tmpXmlElement in DocNodeList)
  327. {
  328. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  329. {
  330. foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
  331. {
  332. if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
  333. {
  334. if ((tmpXmlElement.Attributes [tmpIgnoreAttr.Name].Value.ToLower ().IndexOf ("javascript") >= 0) ||
  335. (tmpXmlElement.Attributes [tmpIgnoreAttr.Name].Value.ToLower ().IndexOf ("dopostback") >= 0)) {
  336. tmpXmlElement.SetAttribute (tmpIgnoreAttr.Name, "");
  337. }
  338. }
  339. }
  340. }
  341. }
  342. }
  343. }
  344. // remove whole tags
  345. ArrayList tagsToRemove = new ArrayList ();
  346. xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode ("Almost/RemoveTags").GetEnumerator (); //FirstChild.GetEnumerator
  347. while (xmlIgnoreEnum.MoveNext())
  348. {
  349. XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
  350. XmlNodeList DocNodeList;
  351. //clean Java Script attribute values
  352. DocNodeList = xmlDocument.GetElementsByTagName("*");
  353. if (DocNodeList != null)
  354. {
  355. foreach (XmlElement tmpXmlElement in DocNodeList)
  356. {
  357. if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
  358. {
  359. tagsToRemove.Add (tmpXmlElement);
  360. //tmpXmlElement.ParentNode.RemoveChild (tmpXmlElement);
  361. }
  362. }
  363. }
  364. }
  365. if (tagsToRemove.Count > 0) {
  366. foreach (XmlElement el in tagsToRemove) {
  367. try {
  368. el.ParentNode.RemoveChild (el);
  369. }
  370. catch { }
  371. }
  372. }
  373. }
  374. }
  375. }