使用HtmlAgilityPack更好的HTML分析和验证
2012-01-15 04:42
417 查看
======================================================
注:本文源代码点此下载
======================================================
让我们面对它,有时候,当您正在编写自定义的提取和验证规则时microsoft.visualstudio.testtools.webtesting.htmldocument 类不会剪切它。htmldocument最初设计是作为一个内部类非常有效地为html响应正文之外的从属请求(比如图像)分析urls。vs 2005 rtm 之前,我们将htmldocument 作为公有webtestframework
api的一部分,但是时间安排和资源约束阻止我们为其添加更多的常规目的 dom 功能如innerhtml、innertext 和getelementbyid。您可以自己分析html字符串,不过
幸运的是还有一个更好的选择:htmlagilitypack。
htmlagilitypack 是codeplex 上的一个开源项目。它提供了标准的dom api 和xpath 导航--即使 html 不是适当的格式!
下面是使用htmlagilitypack.htmldocument代替webtestframework中的web测试示例。它简单验证微软主页在导航工具条上将windows列为第一项。下载htmlagilitypack 并从您的测试项目添加对它的引用来尝试此编码 web 测试。
using system;
using system.collections.generic;
using system.text;
using microsoft.visualstudio.testtools.webtesting;
using htmlagilitypack;
public class webtest1coded : webtest
{
public override ienumeratorwebtestrequest> getrequestenumerator()
{
webtestrequest request1 = new webtestrequest("http://www.microsoft.com/");
request1.validateresponse += new eventhandlervalidationeventargs>(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
element
htmlnode firstnavitemnode = navnode.selectsinglenode(".//li");
//validate the first list item in the nav element says "windows"
e.isvalid = firstnavitemnode.innertext == "windows";
}
}
joshch发布于星期天,2006年12月10日下午9点56分
原文地址:http://blogs.msdn.com/joshch/archive/2006/12/10/be...
绿色通道:好文要顶关注我收藏该文与我联系
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定
这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
注:本文源代码点此下载
======================================================
让我们面对它,有时候,当您正在编写自定义的提取和验证规则时microsoft.visualstudio.testtools.webtesting.htmldocument 类不会剪切它。htmldocument最初设计是作为一个内部类非常有效地为html响应正文之外的从属请求(比如图像)分析urls。vs 2005 rtm 之前,我们将htmldocument 作为公有webtestframework
api的一部分,但是时间安排和资源约束阻止我们为其添加更多的常规目的 dom 功能如innerhtml、innertext 和getelementbyid。您可以自己分析html字符串,不过
幸运的是还有一个更好的选择:htmlagilitypack。
htmlagilitypack 是codeplex 上的一个开源项目。它提供了标准的dom api 和xpath 导航--即使 html 不是适当的格式!
下面是使用htmlagilitypack.htmldocument代替webtestframework中的web测试示例。它简单验证微软主页在导航工具条上将windows列为第一项。下载htmlagilitypack 并从您的测试项目添加对它的引用来尝试此编码 web 测试。
using system;
using system.collections.generic;
using system.text;
using microsoft.visualstudio.testtools.webtesting;
using htmlagilitypack;
public class webtest1coded : webtest
{
public override ienumeratorwebtestrequest> getrequestenumerator()
{
webtestrequest request1 = new webtestrequest("http://www.microsoft.com/");
request1.validateresponse += new eventhandlervalidationeventargs>(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
element
htmlnode firstnavitemnode = navnode.selectsinglenode(".//li");
//validate the first list item in the nav element says "windows"
e.isvalid = firstnavitemnode.innertext == "windows";
}
}
joshch发布于星期天,2006年12月10日下午9点56分
原文地址:http://blogs.msdn.com/joshch/archive/2006/12/10/be...
绿色通道:好文要顶关注我收藏该文与我联系
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定
这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
相关文章推荐
- 使用HtmlAgilityPack更好的HTML分析和验证
- 使用HtmlAgilityPack更好的HTML分析和验证
- [翻译]使用HtmlAgilityPack更好的HTML分析和验证
- 使用HtmlAgilityPack更好的HTML分析和验证
- 使用HtmlAgilityPack更好的HTML分析和验证
- Win10通用程序 UWP版HtmlAgilityPack UWP应用使用示例
- 使用HtmlAgilityPack和ScrapySharp抓取网页数据遇到的几个问题解决方法——格式编码问题
- 使用HtmlAgilityPack解析html
- C# HTML解析工具HtmlAgilityPack使用实例(一)
- NET 使用HtmlAgilityPack抓取网页数据
- HtmlAgilityPack使用
- C# 使用HtmlAgilityPack抓取网页信息
- Html Agility Pack ── 一个分析HTML的工具
- 使用HtmlAgilityPack抓取网页数据
- C#使用HtmlAgilityPack抓取糗事百科内容实例
- HtmlAgilityPack使用
- HtmlAgilityPack的简单使用
- WP8 中使用HTML Agility Pack与友盟分享SDK遇到的 System.Xml.XPath加载问题
- c#使用htmlagilitypack解析html格式字符串
- C#中使用HtmlAgilityPack对html进行解析