Here is a simple test that asserts if the DropDownList1 control has its "False" option selected:
public void TestFalseSelected()
string page = "http://localhost/telerik/r.a.d.grid3.0_NET1/Grid/Examples/GeneralFeatures/Sorting/DefaultCS.aspx";
XmlDocument doc = GetPageDocument(page);
XmlNode selectedOption = doc.SelectSingleNode("//select[@name='DropDownList1']//option[@selected='selected']");
I am using the fact that drop-down lists are rendered as HTML <select> elements that contain a set of <option> ones. The XPath means "get me the <select> that has a name attribute of "DropDownList1", then look for a child <option> element that has its selected attribute set to "selected". The assertion is straight-forward -- does that <option>'s value attribute equal to "False".
Here is the GetPageDocument method. It has some gotchas: I remove the <!DOCTYPE> header, so that XmlDocument does not slow-down while trying to fetch the DTD off W3C's servers. Well, removing the doctype gets you some "unresolved entities" errors for , ©, and the likes, so I added a dummy definition for those entities -- just a simple <span> element:
public XmlDocument GetPageDocument(string pageAddress)
WebClient client = new WebClient();
using (Stream dataStream = client.OpenRead(pageAddress))
StreamReader reader = new StreamReader(dataStream);
string contents = reader.ReadToEnd();
contents = Regex.Replace(contents, "<!DOCTYPE[^>]+>", "", RegexOptions.IgnoreCase);
string customEntitiesDeclaration = @"<!DOCTYPE testdocument [
<!ENTITY nbsp ""<span />"">
<!ENTITY copy ""<span />"">
contents = customEntitiesDeclaration + contents;
XmlDocument document = new XmlDocument();
So, that wasn't hard. Get all your tags closed, write their names in lowercase, use the XhtmlPage class to force that for most of ASP.NET's broken controls, and get those tests rolling.
Subscribe to be the first to get our expert-written articles and tutorials for developers!