1+ using System ;
2+ using System . Collections . ObjectModel ;
3+ using System . Linq . Expressions ;
4+ using Machine . Specifications ;
5+ using Smallcode . Net . Exceptions ;
6+
7+ namespace Smallcode . Net . Test . Extensions
8+ {
9+ [ Tags ( "字符" , "扩展" ) ]
10+ public class Html解析扩展规范
11+ {
12+ public static Html EmptyHtml ;
13+ public static Html Html ;
14+ public Establish Content = ( ) =>
15+ {
16+ EmptyHtml = Html . Parse ( string . Empty ) ;
17+ Html = Html . Parse ( "<div class=\" class\" id=\" id\" name=\" name\" ><a href=\" http://www.test.com\" >click me</a></div><div class=\" class other\" id=\" other\" ><a href=\" http://www.test1.com\" >click me</a></div>" ) ;
18+ } ;
19+
20+ #region 单节点
21+ public It 无法解析时能抛出异常 = ( ) => Catch . Exception ( ( ) => EmptyHtml . FindElement ( By . CssSelector ( "div" ) ) ) . ShouldBeOfType < NotFoundException > ( ) ;
22+
23+ public It 必须能用TagName解析 = ( ) => Html . FindElement ( By . TagName ( "div" ) ) . ShouldMatch ( ConditionId ( ) ) ;
24+
25+ public It 必须能用id解析 = ( ) => Html . FindElement ( By . Id ( "id" ) ) . ShouldMatch ( ConditionId ( ) ) ;
26+
27+ public It 必须能用ClassName解析 = ( ) => Html . FindElement ( By . ClassName ( "class" ) ) . ShouldMatch ( ConditionId ( ) ) ;
28+
29+ public It 必须能用xPath解析 = ( ) => Html . FindElement ( By . XPath ( "//div" ) ) . ShouldMatch ( ConditionId ( ) ) ;
30+
31+ public It 必须能用LinkText解析 = ( ) => Html . FindElement ( By . LinkText ( "click me" ) ) . ShouldMatch ( ConditionHref ( ) ) ;
32+
33+ public It 必须能用PartialLinkText解析 = ( ) => Html . FindElement ( By . PartialLinkText ( "click" ) ) . ShouldMatch ( ConditionHref ( ) ) ;
34+
35+ public It 必须能用Css解析 = ( ) =>
36+ {
37+ Html . FindElement ( By . CssSelector ( ".class" ) ) . ShouldMatch ( ConditionId ( ) ) ;
38+ Html . FindElement ( By . CssSelector ( "#id" ) ) . ShouldMatch ( ConditionId ( ) ) ;
39+ Html . FindElement ( By . CssSelector ( "div" ) ) . ShouldMatch ( ConditionId ( ) ) ;
40+ Html . FindElement ( By . CssSelector ( "div.class" ) ) . ShouldMatch ( ConditionId ( ) ) ;
41+ Html . FindElement ( By . CssSelector ( "div#id" ) ) . ShouldMatch ( ConditionId ( ) ) ;
42+ Html . FindElement ( By . CssSelector ( "div.class.other" ) ) . ShouldMatch ( h => h . GetAttribute ( "id" ) == "other" ) ; //必须同时匹配两个class
43+ Html . FindElement ( By . CssSelector ( "div#other.other" ) ) . ShouldMatch ( h => h . GetAttribute ( "id" ) == "other" ) ; //必须同时匹配id和class
44+ Html . FindElement ( By . CssSelector ( "div.class > a" ) ) . ShouldMatch ( ConditionHref ( ) ) ; //选择div.class的直接子标签a
45+ Html . FindElement ( By . CssSelector ( "div[name=name].class" ) ) . ShouldMatch ( ConditionId ( ) ) ; //必须同时匹配属性name和类class
46+ } ;
47+ #endregion
48+
49+ #region 多节点
50+ public It 无法解析集合时返回空集合 = ( ) => EmptyHtml . FindElements ( By . CssSelector ( "div" ) ) . Count . ShouldEqual ( 0 ) ;
51+
52+ public It 必须能用TagName解析集合 = ( ) => Html . FindElements ( By . TagName ( "div" ) ) . ShouldMatch ( ConditionIds ( ) ) ;
53+
54+ public It 必须能用ClassName解析集合 = ( ) => Html . FindElements ( By . ClassName ( "class" ) ) . ShouldMatch ( ConditionIds ( ) ) ;
55+
56+ public It 必须能用xPath解析集合 = ( ) => Html . FindElements ( By . XPath ( "//div" ) ) . ShouldMatch ( ConditionIds ( ) ) ;
57+
58+ public It 必须能用LinkText解析集合 = ( ) => Html . FindElements ( By . LinkText ( "click me" ) ) . ShouldMatch ( ConditionHrefs ( ) ) ;
59+
60+ public It 必须能用PartialLinkText解析集合 = ( ) => Html . FindElements ( By . PartialLinkText ( "click" ) ) . ShouldMatch ( ConditionHrefs ( ) ) ;
61+
62+ public It 必须能用Css解析集合 = ( ) =>
63+ {
64+ Html . FindElements ( By . CssSelector ( "div" ) ) . ShouldMatch ( ConditionIds ( ) ) ;
65+ Html . FindElements ( By . CssSelector ( "div.class" ) ) . ShouldMatch ( ConditionIds ( ) ) ;
66+ } ;
67+ #endregion
68+
69+ private static Expression < Func < Html , bool > > ConditionId ( )
70+ {
71+ return h => h [ "class" ] == "class" && h [ "id" ] == "id" && h [ "name" ] == "name" ; ;
72+ }
73+
74+ private static Expression < Func < ReadOnlyCollection < Html > , bool > > ConditionIds ( )
75+ {
76+ return list => list . Count == 2 && list [ 0 ] . GetAttribute ( "id" ) == "id" && list [ 1 ] . GetAttribute ( "id" ) == "other" ;
77+ }
78+
79+ private static Expression < Func < Html , bool > > ConditionHref ( )
80+ {
81+ return h => h . GetAttribute ( "href" ) == "http://www.test.com" ;
82+ }
83+
84+ private static Expression < Func < ReadOnlyCollection < Html > , bool > > ConditionHrefs ( )
85+ {
86+ return list => list . Count == 2 && list [ 0 ] . GetAttribute ( "href" ) == "http://www.test.com" && list [ 1 ] . GetAttribute ( "href" ) == "http://www.test1.com" ;
87+ }
88+
89+ }
90+ }
0 commit comments