This repository has been archived by the owner on Dec 16, 2023. It is now read-only.
/
xpath_test.coffee
121 lines (92 loc) · 3.12 KB
/
xpath_test.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
{ assert, brains, Browser } = require("./helpers")
describe "XPath", ->
browser = new Browser()
before (done)->
brains.get "/xpath", (req, res)-> res.send """
<html>
<body>
<h1 id="title">My Blog</h2>
<ul class="navigation">
<li><a href="#">First anchor</a></li>
<li><a href="#">Second anchor</a></li>
<li><a href="#">Third anchor</a></li>
<li><a href="#">Fourth anchor</a></li>
<li><a href="#">Fifth anchor</a></li>
</ul>
<div id="posts">
<div class="post" id="post-1">
<h2>First post</h2>
<div class="meta">
<a href="#">First permalink</a>
<a href="#">First author</a>
<a href="#">First comments</a>
</div>
<div class="content">
<p>First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
</div>
</div>
<div class="post" id="post-2">
<h2>Second post</h2>
<div class="meta">
<a href="#">Second permalink</a>
<a href="#">Second author</a>
<a href="#">Second comments</a>
</div>
<div class="content">
<p>Fourth paragraph</p>
<p>Fifth paragraph</p>
<p>Sixth paragraph</p>
</div>
</div>
</div>
</body>
</html>
"""
brains.ready ->
browser.visit "http://localhost:3003/xpath", done
describe "evaluate nodes", ->
result = null
before ->
result = browser.xpath("//a")
it "should return result type node-set", ->
assert.equal result.type, "node-set"
it "should return eleven nodes", ->
assert.equal result.value.length, 11
it "should return first anchor", ->
assert.equal result.value[0].textContent, "First anchor"
it "should return third anchor", ->
assert.equal result.value[2].textContent, "Third anchor"
describe "evaluate with id", ->
result = null
before ->
result = browser.xpath('//*[@id="post-2"]/h2')
it "should return one node", ->
assert.equal result.value.length, 1
it "should return second post title", ->
assert.equal result.value[0].textContent, "Second post"
describe "evaluate number", ->
result = null
before ->
result = browser.xpath("count(//a)")
it "should return result type number", ->
assert.equal result.type, "number"
it "should return number of nodes", ->
assert.equal result.value, 11
describe "evaluate string", ->
result = null
before ->
result = browser.xpath("'foobar'")
it "should return result type string", ->
assert.equal result.type, "string"
it "should return number of nodes", ->
assert.equal result.value, "foobar"
describe "evaluate boolean", ->
result = null
before ->
result = browser.xpath("2 + 2 = 4")
it "should return result type boolean", ->
assert.equal result.type, "boolean"
it "should return number of nodes", ->
assert.equal result.value, true