forked from chromedp/chromedp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expose_test.go
178 lines (151 loc) · 4.81 KB
/
expose_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
package chromedp
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"fmt"
"testing"
"github.com/chromedp/cdproto/runtime"
)
func md5SumFunc(args string) (string, error) {
h := md5.New()
h.Write([]byte(args))
return hex.EncodeToString(h.Sum(nil)), nil
}
func base64EncodeFunc(args string) (string, error) {
return base64.StdEncoding.EncodeToString([]byte(testString)), nil
}
func echoFunc(str string) (string, error) {
return str, nil
}
const testString = "chromedp expose test"
const testStringMd5 = "a93d69002a286b46c8aa114362afb7ac"
const testStringBase64 = "Y2hyb21lZHAgZXhwb3NlIHRlc3Q="
const testIFrameHTMLTitle = "page with an iframe"
const testFormHTMLTitle = "this is form title"
func TestExposeToAllFrames(t *testing.T) {
// allocate browser
ctx, cancel := testAllocate(t, "iframe.html")
defer cancel()
// expose echoFunc function as to browser current page and every frame
if err := Run(ctx, Expose("echo", echoFunc)); err != nil {
t.Fatal(err)
}
c := FromContext(ctx)
c.Target.frameMu.RLock()
executionContextIDs := make([]runtime.ExecutionContextID, 0, len(c.Target.execContexts))
for _, executionContextID := range c.Target.execContexts {
executionContextIDs = append(executionContextIDs, executionContextID)
}
c.Target.frameMu.RUnlock()
var res1 string
var res2 string
callEchoFunc := fmt.Sprintf(`%s(document.title);`, "echo")
for _, executionContextID := range executionContextIDs {
id := executionContextID
var res string
if err := Run(ctx, Evaluate(callEchoFunc, &res, func(p *runtime.EvaluateParams) *runtime.EvaluateParams {
return p.WithContextID(id).WithAwaitPromise(true)
})); err != nil {
t.Fatal(err)
}
if len(res1) == 0 {
res1 = res
} else {
res2 = res
}
}
// we expect res1 or res2 = testIFrameHTMLTitle or testFormHTMLTitle
if res1 == testIFrameHTMLTitle && res2 == testFormHTMLTitle || res1 == testFormHTMLTitle && res2 == testIFrameHTMLTitle {
// pass
} else {
t.Fatalf("res1: %s, res2: %s", res1, res2)
}
}
func TestExpose(t *testing.T) {
// allocate browser
ctx, cancel := testAllocate(t, "")
defer cancel()
// expose md5SumFunc function as md5 to browser current page and every frame
if err := Run(ctx, Expose("md5", md5SumFunc)); err != nil {
t.Fatal(err)
}
// expose base64EncodeFunc function as base64 to browser current page and every frame
if err := Run(ctx, Expose("base64", base64EncodeFunc)); err != nil {
t.Fatal(err)
}
// 1. When on the current page
var res string
callMd5 := fmt.Sprintf(`%s("%s");`, "md5", testString)
if err := Run(ctx, Evaluate(callMd5, &res, func(p *runtime.EvaluateParams) *runtime.EvaluateParams {
return p.WithAwaitPromise(true)
})); err != nil {
t.Fatal(err)
}
if res != testStringMd5 {
t.Fatalf("want: %s, got: %s", testStringMd5, res)
}
var res2 string
callBase64 := fmt.Sprintf(`%s("%s");`, "base64", testString)
if err := Run(ctx, Evaluate(callBase64, &res2, func(p *runtime.EvaluateParams) *runtime.EvaluateParams {
return p.WithAwaitPromise(true)
})); err != nil {
t.Fatal(err)
}
if res2 != testStringBase64 {
t.Fatalf("want: %s, got: %s", testStringBase64, res)
}
// 2. Navigate another page
if err := Run(ctx,
Navigate(testdataDir+"/child1.html"),
); err != nil {
t.Fatal(err)
}
// we expect md5 can work properly.
if err := Run(ctx, Evaluate(callMd5, &res, func(p *runtime.EvaluateParams) *runtime.EvaluateParams {
return p.WithAwaitPromise(true)
})); err != nil {
t.Fatal(err)
}
if res != testStringMd5 {
t.Fatalf("want: %s, got: %s", testStringMd5, res)
}
// we expect base64 can work properly.
if err := Run(ctx, Evaluate(callBase64, &res2, func(p *runtime.EvaluateParams) *runtime.EvaluateParams {
return p.WithAwaitPromise(true)
})); err != nil {
t.Fatal(err)
}
if res2 != testStringBase64 {
t.Fatalf("want: %s, got: %s", testStringBase64, res)
}
}
func TestExposeMulti(t *testing.T) {
// allocate browser
ctx, cancel := testAllocate(t, "")
defer cancel()
// creates a new page. about:blank
Run(ctx)
// expose md5SumFunc function as sameFunc to browser current page and every frame
if err := Run(ctx, Expose("sameFunc", md5SumFunc)); err != nil {
t.Fatal(err)
}
// expose base64EncodeFunc function as sameFunc to browser current page and every frame
if err := Run(ctx, Expose("sameFunc", base64EncodeFunc)); err != nil {
t.Fatal(err)
}
// we expect first expose function to handle
var res string
sameFunc := fmt.Sprintf(`%s("%s");`, "sameFunc", testString)
if err := Run(ctx, Evaluate(sameFunc, &res, func(p *runtime.EvaluateParams) *runtime.EvaluateParams {
return p.WithAwaitPromise(true)
})); err != nil {
t.Fatal(err)
}
if res != testStringMd5 {
t.Fatalf("want md5SumFunc res:%s, got:%s", testStringMd5, res)
}
if res == testStringBase64 {
t.Fatalf("want md5SumFunc res:%s, got base64EncodeFunc res :%s", testStringMd5, res)
}
}