3 // Rafael Mizrahi <rafim@mainsoft.com>
4 // Erez Lotan <erezl@mainsoft.com>
5 // Vladimir Krasnov <vladimirk@mainsoft.com>
8 // Copyright (c) 2002-2005 Mainsoft Corporation.
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
37 using System.Collections;
38 using System.Reflection;
39 using NUnit.Framework;
41 namespace MonoTests.stand_alone.WebHarness
43 public abstract class XmlComparableTest
45 public abstract bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost);
48 public class HtmlDiff : XmlComparableTest
50 public const string BEGIN_TAG = "begint";
51 public const string END_TAG = "endt";
53 private XmlDocument _xmlIgnoreList = null;
54 private string _compareStatus = "";
55 private static string _compareActual = "";
56 private static string _compareExpect = "";
57 private string _ignoreListFile = "";
65 public string IgnoreListFile
67 get {return _ignoreListFile;}
68 set {_ignoreListFile = value;}
71 public string CompareStatus
73 get {return _compareStatus.ToString();}
76 public static string GetControlFromPageHtml (string str)
78 if (str == null || str == string.Empty)
79 throw new ArgumentException ("internal error: str is null or empty");
80 int beginPos = str.IndexOf (BEGIN_TAG);
81 int endPos = str.IndexOf (END_TAG);
83 throw new Exception ("internal error: BEGIN_TAG is missing. Full source: "+str);
85 throw new Exception ("internal error: END_TAG is missing. Full source: "+str);
87 StringBuilder sb = new StringBuilder ();
88 sb.Append (str.Substring (beginPos + BEGIN_TAG.Length, endPos - beginPos - BEGIN_TAG.Length));
89 return sb.ToString ();
92 public static void AssertAreEqual (string origin, string derived, string msg)
94 bool test = HtmlComparer (origin, derived);
96 Assert.AreEqual (_compareActual, _compareExpect, msg);
101 public static bool HtmlComparer (string origin, string derived)
103 XmlDocument or = new XmlDocument ();
104 MonoTests.stand_alone.WebHarness.HtmlDiff helper = new MonoTests.stand_alone.WebHarness.HtmlDiff ();
105 or.LoadXml (helper.HtmltoXml (origin));
106 XmlDocument dr = new XmlDocument ();
107 dr.LoadXml (helper.HtmltoXml (derived));
108 return helper.XmlCompare (or, dr, false);
111 public override bool XmlCompare(XmlDocument expected, XmlDocument actual, bool ignoreAlmost)
113 XmlComparer comparer = new XmlComparer();
114 if (ignoreAlmost == false)
119 bool c = comparer.AreEqual(expected, actual);
120 _compareStatus = comparer.LastCompare;
121 _compareActual = comparer.Actual;
122 _compareExpect = comparer.Expected;
126 public string HtmltoXml(string html)
128 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
131 StringBuilder fixedxml = new StringBuilder();
132 StringWriter sw = new StringWriter(fixedxml);
136 StringBuilder tempxml = new StringBuilder();
137 StringWriter tsw = new StringWriter(tempxml);
139 doc.OptionOutputAsXml = true;
142 // fix style attribute
143 // the reason is that style attribute name-value pairs come in different order
145 // Here I will sort the values of style attribute
146 XmlDocument tempDoc = new XmlDocument();
147 tempDoc.LoadXml(tempxml.ToString());
149 XmlNodeList allNodes = tempDoc.SelectNodes("//*");
150 foreach (XmlNode n in allNodes)
152 if (n.Attributes["style"] != null)
154 string att = n.Attributes["style"].Value;
155 string [] style = att.Trim(new char[]{' ', ';'}).Split(';');
157 for (int styleIndex=0; styleIndex<style.Length; styleIndex++)
159 style[styleIndex] = FixStyleNameValue(style[styleIndex]);
162 n.Attributes["style"].Value = string.Join(";", style);
169 Console.WriteLine("Error parsing html response...");
170 Console.WriteLine("Test case aborted");
171 return "<TestCaseAborted></TestCaseAborted>";
173 return fixedxml.ToString();
176 private string FixStyleNameValue(string nameValue)
178 string [] nv = nameValue.Split(':');
179 // value may contain spaces in case of
180 // multiple values for one key
181 string [] nvalue = nv[1].Trim().Split(' ');
183 nv[1] = string.Join(" ", nvalue);
184 return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
187 private void DoAlmost(XmlDocument xmlDocument)
189 XmlNode XmlIgnoreNode;
190 IEnumerator xmlIgnoreEnum;
193 if (_xmlIgnoreList == null)
195 _xmlIgnoreList = new XmlDocument();
198 Stream source = Assembly.GetExecutingAssembly ()
\r
199 .GetManifestResourceStream ("HtmlCompare.nunitweb_config.xml");
\r
200 if (source == null) {
\r
201 source = Assembly.GetExecutingAssembly ()
\r
202 .GetManifestResourceStream ("nunitweb_config.xml");
\r
206 using (StreamReader sr = new StreamReader (source))
\r
207 xml = sr.ReadToEnd ();
\r
213 _xmlIgnoreList.LoadXml (xml);
215 // Remove by Id or Name
216 // search by tag and if id or name match, remove all attributes
217 // must be the first almost since the following almost delete the id and name
219 xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
220 while (xmlIgnoreEnum.MoveNext())
222 XmlNodeList DocNodeList;
223 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
224 DocNodeList = xmlDocument.GetElementsByTagName("*");
225 if (DocNodeList != null)
227 foreach (XmlElement tmpXmlElement in DocNodeList)
229 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
231 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
233 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
235 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
237 tmpXmlElement.RemoveAllAttributes();
245 // remove ignored attributes
246 // search for tag and remove it's attributes
247 xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
248 while (xmlIgnoreEnum.MoveNext())
250 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
251 XmlNodeList DocNodeList;
252 //clean specific element
254 DocNodeList = xmlDocument.GetElementsByTagName("*");
255 if (DocNodeList != null)
257 foreach (XmlElement tmpXmlElement in DocNodeList)
259 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
261 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
263 tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
270 // clean javascript attribute value
271 xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
272 while (xmlIgnoreEnum.MoveNext())
274 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
275 XmlNodeList DocNodeList;
276 //clean Java Script attribute values
277 DocNodeList = xmlDocument.GetElementsByTagName("*");
278 if (DocNodeList != null)
280 foreach (XmlElement tmpXmlElement in DocNodeList)
282 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
284 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
286 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
288 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower().IndexOf("javascript") >= 0 )
290 tmpXmlElement.SetAttribute(tmpIgnoreAttr.Name, "");