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 if (string.IsNullOrEmpty (str))
76 throw new ArgumentException ("internal error: str is null or empty");
77 int beginPos = str.IndexOf (BEGIN_TAG);
78 int endPos = str.IndexOf (END_TAG);
80 throw new Exception ("internal error: BEGIN_TAG is missing. Full source: "+str);
82 throw new Exception ("internal error: END_TAG is missing. Full source: "+str);
84 StringBuilder sb = new StringBuilder ();
85 sb.Append (str.Substring (beginPos + BEGIN_TAG.Length, endPos - beginPos - BEGIN_TAG.Length));
86 return sb.ToString ();
89 public static void AssertAreEqual (string origin, string derived, string msg)
91 bool test = HtmlComparer (origin, derived);
93 Assert.AreEqual (_compareActual, _compareExpect, msg);
98 public static bool HtmlComparer (string origin, string derived)
100 XmlDocument or = new XmlDocument ();
101 MonoTests.stand_alone.WebHarness.WebTest helper = new MonoTests.stand_alone.WebHarness.WebTest ();
102 or.LoadXml (helper.HtmltoXml (origin));
103 XmlDocument dr = new XmlDocument ();
104 dr.LoadXml (helper.HtmltoXml (derived));
105 return helper.XmlCompare (or, dr, false);
108 public override bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost)
110 XmlComparer comparer = new XmlComparer();
111 if (ignoreAlmost == false)
116 bool c = comparer.AreEqual(d1, d2);
117 _compareStatus = comparer.LastCompare;
118 _compareActual = comparer.Actual;
119 _compareExpect = comparer.Expected;
123 public string HtmltoXml(string html)
125 HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
128 StringBuilder fixedxml = new StringBuilder();
129 StringWriter sw = new StringWriter(fixedxml);
133 StringBuilder tempxml = new StringBuilder();
134 StringWriter tsw = new StringWriter(tempxml);
136 doc.OptionOutputAsXml = true;
139 // fix style attribute
140 // the reason is that style attribute name-value pairs come in different order
142 // Here I will sort the values of style attribute
143 XmlDocument tempDoc = new XmlDocument();
144 tempDoc.LoadXml(tempxml.ToString());
146 XmlNodeList allNodes = tempDoc.SelectNodes("//*");
147 foreach (XmlNode n in allNodes)
149 if (n.Attributes["style"] != null)
151 string att = n.Attributes["style"].Value;
152 string [] style = att.Trim(new char[]{' ', ';'}).Split(';');
154 for (int styleIndex=0; styleIndex<style.Length; styleIndex++)
156 style[styleIndex] = FixStyleNameValue(style[styleIndex]);
159 n.Attributes["style"].Value = string.Join(";", style);
166 Console.WriteLine("Error parsing html response...");
167 Console.WriteLine("Test case aborted");
168 return "<TestCaseAborted></TestCaseAborted>";
170 return fixedxml.ToString();
173 private string FixStyleNameValue(string nameValue)
175 string [] nv = nameValue.Split(':');
176 // value may contain spaces in case of
177 // multiple values for one key
178 string [] nvalue = nv[1].Trim().Split(' ');
180 nv[1] = string.Join(" ", nvalue);
181 return nv[0].Trim().ToLower() + ":" + nv[1].Trim().ToLower();
184 private void DoAlmost(XmlDocument xmlDocument)
186 XmlNode XmlIgnoreNode;
187 IEnumerator xmlIgnoreEnum;
189 if (_xmlIgnoreList == null)
191 _xmlIgnoreList = new XmlDocument();
193 using (Stream source = Assembly.GetExecutingAssembly()
194 .GetManifestResourceStream ("nunitweb_config.xml")) {
195 using (StreamReader sr = new StreamReader (source))
196 xml = sr.ReadToEnd ();
198 _xmlIgnoreList.LoadXml (xml);
201 // Remove by Id or Name
202 // search by tag and if id or name match, remove all attributes
203 // must be the first almost since the following almost delete the id and name
204 xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/RemoveById").GetEnumerator();
205 while (xmlIgnoreEnum.MoveNext())
207 XmlNodeList DocNodeList;
208 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
209 DocNodeList = xmlDocument.GetElementsByTagName("*");
210 if (DocNodeList != null)
212 foreach (XmlElement tmpXmlElement in DocNodeList)
214 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
216 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
218 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
220 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower() == tmpIgnoreAttr.Value.ToLower())
222 tmpXmlElement.RemoveAllAttributes();
230 // remove ignored attributes
231 // search for tag and remove it's attributes
232 xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/IgnoreList").GetEnumerator(); //FirstChild.GetEnumerator
233 while (xmlIgnoreEnum.MoveNext())
235 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
236 XmlNodeList DocNodeList;
237 //clean specific element
239 DocNodeList = xmlDocument.GetElementsByTagName("*");
240 if (DocNodeList != null)
242 foreach (XmlElement tmpXmlElement in DocNodeList)
244 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
246 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
248 tmpXmlElement.RemoveAttribute(tmpIgnoreAttr.Name);
255 // clean javascript attribute value
256 xmlIgnoreEnum = _xmlIgnoreList.SelectSingleNode("Almost/CleanJavaScriptValueList").GetEnumerator(); //FirstChild.GetEnumerator
257 while (xmlIgnoreEnum.MoveNext())
259 XmlIgnoreNode = (XmlNode)xmlIgnoreEnum.Current;
260 XmlNodeList DocNodeList;
261 //clean Java Script attribute values
262 DocNodeList = xmlDocument.GetElementsByTagName("*");
263 if (DocNodeList != null)
265 foreach (XmlElement tmpXmlElement in DocNodeList)
267 if (tmpXmlElement.Name.ToLower() == XmlIgnoreNode.Name.ToLower())
269 foreach (XmlAttribute tmpIgnoreAttr in XmlIgnoreNode.Attributes)
271 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name] != null )
273 if (tmpXmlElement.Attributes[tmpIgnoreAttr.Name].Value.ToLower().IndexOf("javascript") >= 0 )
275 tmpXmlElement.SetAttribute(tmpIgnoreAttr.Name, "");