forked from chromedp/chromedp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
212 lines (186 loc) · 4.72 KB
/
util_test.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package chromedp
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/runtime"
)
// dumpJS is a javascript snippet that dumps the passed element node in the
// same text format as cdproto/cdp/Node.Dump.
//
// Used to verify that the DOM tree built by chromedp is the same as what
// chrome itself sees.
//
// Note: written to be "line-by-line equivalent" with Node.Dump's
// implementation.
const dumpJS = `(function dump(n, prefix, indent, nodeIDs) {
if (n === null || typeof n !== 'object') {
return prefix + '<nil>';
}
var s = '';
if (typeof n.localName !== 'undefined') {
s = n.localName;
}
if (s === '' && typeof n.nodeName !== 'undefined') {
s = n.nodeName;
}
if (s === '') {
throw 'invalid node element';
}
if (typeof n.attributes !== 'undefined') {
for (var i = 0; i < n.attributes.length; i++) {
if (n.attributes[i].name.toLowerCase() === 'id') {
s += '#' + n.attributes[i].value;
break;
}
}
}
if (n.nodeType !== 1 && n.nodeType !== 3) {
s += ' <' + [
'Element',
'Attribute',
'Text',
'CDATA',
'EntityReference',
'Entity',
'ProcessingInstruction',
'Comment',
'Document',
'DocumentType',
'DocumentFragment',
'Notation'
][n.nodeType - 1] + '>';
}
if (n.nodeType === 3) {
var v = n.nodeValue;
if (v.length > 15) {
v = v.substring(0, 15) + '...';
}
s += ' ' + JSON.stringify(v);
}
if (n.nodeType === 1 && typeof n.attributes !== 'undefined' && n.attributes.length > 0) {
var attrs = '';
for (var i = 0; i < n.attributes.length; i++) {
if (n.attributes[i].name.toLowerCase() === 'id') {
continue;
}
if (attrs !== '') {
attrs += ' ';
}
attrs += n.attributes[i].name + '=' + JSON.stringify(n.attributes[i].value);
}
if (attrs != '') {
s += ' [' + attrs + ']';
}
}
if (nodeIDs) {
throw 'cannot read element node ID from scripts';
}
if (typeof n.childNodes !== 'undefined' && n.childNodes.length > 0) {
for (var i = 0; i < n.childNodes.length; i++) {
// skip empty #text nodes
if (n.childNodes[i].nodeType === 3 && n.childNodes[i].nodeValue.trim() === '') {
continue;
}
s += '\n' + dump(n.childNodes[i], prefix+indent, indent, nodeIDs);
}
}
return prefix + s;
})(%s, %q, %q, %t);`
const insertJS = `(function(n, typ, id, text) {
var el = document.createElement(typ);
el.id = id;
el.innerText = text;
%s;
})(document.querySelector(%q), %q, %q, %q)`
// test:
// insertBefore
// removeChild
// appendChild
// replaceChild
// prepend
// append
// insertAdjacentElement
func TestNodeOp(t *testing.T) {
t.Parallel()
s := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
fmt.Fprintf(res, `<!doctype html>
<html>
<head>
<title>empty test page</title>
</head>
<body>
<div id="div1">div1 content</div>
</body>
<html>`)
}))
defer s.Close()
ctx, cancel := testAllocate(t, "")
defer cancel()
// get document root
var nodes []*cdp.Node
if err := Run(ctx,
Navigate(s.URL),
Nodes(`//*`, &nodes),
Nodes(`document`, &nodes, ByJSPath),
); err != nil {
t.Fatal(err)
}
tests := []struct {
q, expr string
}{
{`body`, `n.insertBefore(el, n.childNodes[0])`},
{`body`, `n.insertBefore(el, n.childNodes[1])`},
{`#div3`, `n.prepend(el)`},
{`#div2`, `n.append(el)`},
{`#div2`, `n.appendChild(el)`},
{`#div3`, `n.removeChild(n.childNodes[0])`},
{`#div2`, `n.insertAdjacentElement('afterend', el)`},
{`body`, `n.insertBefore(el, n.childNodes[0])`},
{`body`, `n.insertBefore(el, n.childNodes[1])`},
{`#div2`, `n.replaceChild(el, n.childNodes[1])`},
}
prev := nodes[0].Dump("", " ", false)
for i, test := range tests {
// modify tree
if err := Run(ctx,
ActionFunc(func(ctx context.Context) error {
id := strconv.Itoa(i + 2)
expr := fmt.Sprintf(insertJS, test.expr, test.q, `div`, `div`+id, `div`+id+` content`)
_, exp, err := runtime.Evaluate(expr).Do(ctx)
if err != nil {
return err
}
if exp != nil {
return exp
}
return nil
}),
); err != nil {
t.Fatal(err)
}
// wait for events to propagate
time.Sleep(5 * time.Millisecond)
tree := nodes[0].Dump("", " ", false)
if prev == tree {
t.Fatalf("test %d expected tree to change (prev == tree)\n-- PREV:\n%s\n-- TREE:\n%s\n--\n", i, prev, tree)
}
// retrieve browser's tree view
var exp string
if err := Run(ctx,
EvaluateAsDevTools(fmt.Sprintf(dumpJS, `document`, "", " ", false), &exp),
); err != nil {
t.Fatal(err)
}
if exp != tree {
t.Errorf("test %d expected tree and node tree do not match:\n-- EXPECTED:\n%s\n-- GOT:\n%s\n--\n", i, exp, tree)
}
prev = tree
// t.Logf("test %d:\n%s\n--\n", i, tree)
}
}