Better HTML analysis and verification using htmlagilitypack

Source: Internet
Author: User
Let's face it. Sometimes, the Microsoft. visualstudio. testtools. webtesting. htmldocument class does not cut it when you are writing custom extraction and verification rules. Htmldocument was originally designed as an internal class to effectively analyze URLs for subordinate requests (ratios) outside the HTML response body. Before VS 2005 RTM, we use htmldocument as a part of the public webtestframework API, but schedules and resource constraints prevent us from adding more common Dom functions such as innerhtml, innertext, and getelementbyid for it. You can analyze the HTML string by yourself,

Fortunately, there is another better option: htmlagilitypack.

Htmlagilitypack is an open-source project on codeplex. It provides standard DOM APIs and XPath navigation-even if HTML is not in the proper format!

The following is a web test example using htmlagilitypack. htmldocument to replace webtestframework. It simply verifies that the Microsoft homepage lists Windows as the first item in the navigation toolbar. Download htmlagilitypack and add a reference to it from your test project to try this encoding web test.

Using system;

Using system. Collections. Generic;

Using system. text;

Using Microsoft. visualstudio. testtools. webtesting;

Using htmlagilitypack;

Public class webtest1coded: webtest

{

Public override ienumerator <webtestrequest> getrequestenumerator ()

{

Webtestrequest request1 = new webtestrequest ("http://www.microsoft.com /");

Request1.validateresponse + = new eventhandler <validationeventargs> (request1_validateresponse );

Yield return request1;

}

Void request1_validateresponse (Object sender, validationeventargs E)

{

// Load the response body string as an htmlagilitypack. htmldocument

Htmlagilitypack. htmldocument Doc = new htmlagilitypack. htmldocument ();

Doc. loadhtml (E. Response. bodystring );

// Locate the "nav" element

Htmlnode navnode = Doc. getelementbyid ("nav ");

// Pick the first <li> element

Htmlnode firstnavitemnode = navnode. selectsinglenode (". // li ");

// Validate the first list item in the nav element says "Windows"

E. isvalid = firstnavitemnode. innertext = "Windows ";

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.