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.
34 using System.Collections;
35 using System.Reflection;
36 using NUnit.Framework;
38 namespace MonoTests.stand_alone.WebHarness
40 public abstract class XmlComparableTest
42 public abstract bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost);
45 public class WebTest : XmlComparableTest
47 public const string BEGIN_TAG = "begint";
48 public const string END_TAG = "endt";
50 private XmlDocument _xmlIgnoreList = null;
51 private string _compareStatus = "";
52 private static string _compareActual = "";
53 private static string _compareExpect = "";
54 private string _ignoreListFile = "";
62 public string IgnoreListFile
64 get {return _ignoreListFile;}
65 set {_ignoreListFile = value;}
68 public string CompareStatus
70 get {return _compareStatus.ToString();}
73 public static string GetControlFromPageHtml (string str)
75 StringBuilder sb = new StringBuilder ();
76 sb.Append (str.Substring (str.IndexOf (BEGIN_TAG) + BEGIN_TAG.Length, str.IndexOf (END_TAG) - str.IndexOf (BEGIN_TAG) - BEGIN_TAG.Length));
77 return sb.ToString ();
80 public static void AssertAreEqual (string origin, string derived, string msg)
82 bool test = HtmlComparer (origin, derived);
84 Assert.AreEqual (_compareActual, _compareExpect, msg);
89 public static bool HtmlComparer (string origin, string derived)
91 XmlDocument or = new XmlDocument ();
92 MonoTests.stand_alone.WebHarness.WebTest helper = new MonoTests.stand_alone.WebHarness.WebTest ();
93 or.LoadXml (helper.HtmltoXml (origin));
94 XmlDocument dr = new XmlDocument ();
95 dr.LoadXml (helper.HtmltoXml (derived));
96 return helper.XmlCompare (or, dr, false);
99 public override bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost)
101 XmlComparer comparer = new XmlComparer();
102 if (ignoreAlmost == false)
107 bool c = comparer.AreEqual(d1, d2);
108 _compareStatus = comparer.LastCompare;
109 _compareActual = comparer.Actual;
110 _compareExpect = comparer.Expected;
114 public string HtmltoXml(string html)
116 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
119 StringBuilder fixedxml = new StringBuilder();
120 StringWriter sw = new StringWriter(fixedxml);
124 StringBuilder tempxml = new StringBuilder();
125 StringWriter tsw = new StringWriter(tempxml);
127 doc.OptionOutputAsXml = true;
130 // fix style attribute
131 // the reason is that style attribute name-value pairs come in different order
133 // Here I will sort the values of style attribute
134 XmlDocument tempDoc = new XmlDocument();
135 tempDoc.LoadXml(tempxml.ToString());
137 XmlNodeList allNodes = tempDoc.SelectNodes("//*");
138 foreach (XmlNode n in allNodes)
140 if (n.Attributes["style"] != null)
142 string att = n.Attributes["style"].Value;
143 string [] style = att.Trim(new char[]{' ', ';'}).Split(';');
145 for (int styleIndex=0; styleIndex<style.Length; styleIndex++)
147 style[styleIndex] = FixStyleNameValue(style[styleIndex]);
150 n.Attributes["style"].Value = string.Join(";", style);
157 Console.WriteLine("Error parsing html response...");
158 Console.WriteLine("Test case aborted");
159 return "<TestCaseAborted></TestCaseAborted>";
161 return fixedxml.ToString();
164 private string FixStyleNameValue(string nameValue)
166 string [] nv = nameValue.Split(':');
167 // value may contain spaces in case of
168 // multiple values for one key
169 string [] nvalue = nv[1].Trim().Split(' ');
171 nv[1] = string.Join(" ", nvalue);
172 return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
175 private void DoAlmost(XmlDocument xmlDocument)
177 XmlNode XmlIgnoreNode;
178 IEnumerator xmlIgnoreEnum;
180 if (_xmlIgnoreList == null)
182 _xmlIgnoreList = new XmlDocument();
184 using (Stream source = Assembly.GetExecutingAssembly()
185 .GetManifestResourceStream ("HTMLComparer.nunitweb_config.xml")) {
186 using (StreamReader sr = new StreamReader (source))
187 xml = sr.ReadToEnd ();
189 _xmlIgnoreList.LoadXml (xml);
192 // Remove by Id or Name
193 // search by tag and if id or name match, remove all attributes
194 // must be the first almost since the following almost delete the id and name
195 xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
196 while (xmlIgnoreEnum.MoveNext())
198 XmlNodeList DocNodeList;
199 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
200 DocNodeList = xmlDocument.GetElementsByTagName("*");
201 if (DocNodeList != null)
203 foreach (XmlElement tmpXmlElement in DocNodeList)
205 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
207 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
209 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
211 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
213 tmpXmlElement.RemoveAllAttributes();
221 // remove ignored attributes
222 // search for tag and remove it's attributes
223 xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
224 while (xmlIgnoreEnum.MoveNext())
226 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
227 XmlNodeList DocNodeList;
228 //clean specific element
230 DocNodeList = xmlDocument.GetElementsByTagName("*");
231 if (DocNodeList != null)
233 foreach (XmlElement tmpXmlElement in DocNodeList)
235 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
237 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
239 tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
246 // clean javascript attribute value
247 xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
248 while (xmlIgnoreEnum.MoveNext())
250 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
251 XmlNodeList DocNodeList;
252 //clean Java Script attribute values
253 DocNodeList = xmlDocument.GetElementsByTagName("*");
254 if (DocNodeList != null)
256 foreach (XmlElement tmpXmlElement in DocNodeList)
258 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
260 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
262 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
264 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower().IndexOf("javascript") >= 0 )
266 tmpXmlElement.SetAttribute(tmpIgnoreAttr.Name, "");