forked from chromedp/chromedp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallocate_test.go
274 lines (235 loc) · 6.75 KB
/
allocate_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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package chromedp
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
)
func TestExecAllocator(t *testing.T) {
t.Parallel()
allocCtx, cancel := NewExecAllocator(context.Background(), allocOpts...)
defer cancel()
// TODO: test that multiple child contexts are run in different
// processes and browsers.
taskCtx, cancel := NewContext(allocCtx)
defer cancel()
want := "insert"
var got string
if err := Run(taskCtx,
Navigate(testdataDir+"/form.html"),
Text("#foo", &got, ByID),
); err != nil {
t.Fatal(err)
}
if got != want {
t.Fatalf("want %q, got %q", want, got)
}
cancel()
tempDir := FromContext(taskCtx).Browser.userDataDir
if _, err := os.Lstat(tempDir); !os.IsNotExist(err) {
t.Fatalf("temporary user data dir %q not deleted", tempDir)
}
}
func TestExecAllocatorCancelParent(t *testing.T) {
t.Parallel()
allocCtx, allocCancel := NewExecAllocator(context.Background(), allocOpts...)
defer allocCancel()
// TODO: test that multiple child contexts are run in different
// processes and browsers.
taskCtx, _ := NewContext(allocCtx)
if err := Run(taskCtx); err != nil {
t.Fatal(err)
}
// Canceling the pool context should stop all browsers too.
allocCancel()
tempDir := FromContext(taskCtx).Browser.userDataDir
if _, err := os.Lstat(tempDir); !os.IsNotExist(err) {
t.Fatalf("temporary user data dir %q not deleted", tempDir)
}
}
func TestExecAllocatorKillBrowser(t *testing.T) {
t.Parallel()
// Simulate a scenario where we navigate to a page that never responds,
// and the browser is closed while it's loading.
ctx, cancel := testAllocateSeparate(t)
defer cancel()
if err := Run(ctx); err != nil {
t.Fatal(err)
}
kill := make(chan bool, 1)
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
kill <- true
<-ctx.Done() // block until the end of the test
}))
defer s.Close()
go func() {
<-kill
b := FromContext(ctx).Browser
if err := b.process.Signal(os.Kill); err != nil {
t.Error(err)
}
}()
// Run should error with something other than "deadline exceeded" in
// much less than 5s.
ctx2, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
switch err := Run(ctx2, Navigate(s.URL)); err {
case nil:
// TODO: figure out why this happens sometimes on Travis
// t.Fatal("did not expect a nil error")
case context.DeadlineExceeded:
t.Fatalf("did not expect a standard context error: %v", err)
}
}
func TestSkipNewContext(t *testing.T) {
ctx, cancel := NewExecAllocator(context.Background(), allocOpts...)
defer cancel()
// Using the allocator context directly (without calling NewContext)
// should be an immediate error.
err := Run(ctx, Navigate(testdataDir+"/form.html"))
want := ErrInvalidContext
if err != want {
t.Fatalf("want error to be %q, got %q", want, err)
}
}
func TestRemoteAllocator(t *testing.T) {
t.Parallel()
tempDir, err := ioutil.TempDir("", "chromedp-runner")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tempDir)
procCtx, cancel := context.WithCancel(context.Background())
defer cancel()
cmd := exec.CommandContext(procCtx, execPath,
// TODO: deduplicate these with allocOpts in chromedp_test.go
"--no-first-run",
"--no-default-browser-check",
"--headless",
"--disable-gpu",
"--no-sandbox",
// TODO: perhaps deduplicate this code with ExecAllocator
"--user-data-dir="+tempDir,
"--remote-debugging-port=0",
"about:blank",
)
stderr, err := cmd.StderrPipe()
if err != nil {
t.Fatal(err)
}
defer stderr.Close()
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
wsURL, err := addrFromStderr(stderr)
if err != nil {
t.Fatal(err)
}
allocCtx, allocCancel := NewRemoteAllocator(context.Background(), wsURL)
defer allocCancel()
taskCtx, taskCancel := NewContext(allocCtx)
defer taskCancel()
want := "insert"
var got string
if err := Run(taskCtx,
Navigate(testdataDir+"/form.html"),
Text("#foo", &got, ByID),
); err != nil {
t.Fatal(err)
}
if got != want {
t.Fatalf("want %q, got %q", want, got)
}
targetID := FromContext(taskCtx).Target.TargetID
if err := Cancel(taskCtx); err != nil {
t.Fatal(err)
}
// Check that cancel closed the tabs. Don't just count the
// number of targets, as perhaps the initial blank tab hasn't
// come up yet.
targetsCtx, targetsCancel := NewContext(allocCtx)
defer targetsCancel()
infos, err := Targets(targetsCtx)
if err != nil {
t.Fatal(err)
}
for _, info := range infos {
if info.TargetID == targetID {
t.Fatalf("target from previous iteration wasn't closed: %v", targetID)
}
}
targetsCancel()
// Finally, if we kill the browser and the websocket connection drops,
// Run should error way before the 5s timeout.
// TODO: a "defer cancel()" here adds a 1s timeout, since we try to
// close the target twice. Fix that.
ctx, _ := NewContext(allocCtx)
ctx, cancel = context.WithTimeout(ctx, 5*time.Second)
defer cancel()
// Connect to the browser, then kill it.
if err := Run(ctx); err != nil {
t.Fatal(err)
}
if err := cmd.Process.Signal(os.Kill); err != nil {
t.Error(err)
}
switch err := Run(ctx, Navigate(testdataDir+"/form.html")); err {
case nil:
// TODO: figure out why this happens sometimes on Travis
// t.Fatal("did not expect a nil error")
case context.DeadlineExceeded:
t.Fatalf("did not expect a standard context error: %v", err)
}
}
func TestExecAllocatorMissingWebsocketAddr(t *testing.T) {
t.Parallel()
allocCtx, cancel := NewExecAllocator(context.Background(),
// Use a bad listen addres, so both Chrome and headless-shell
// exit almost immediately.
append([]ExecAllocatorOption{Flag("remote-debugging-address", "_")},
allocOpts...)...)
defer cancel()
ctx, cancel := NewContext(allocCtx)
defer cancel()
want := "stopped too early"
got := fmt.Sprintf("%v", Run(ctx))
if !strings.Contains(got, want) {
t.Fatalf("want error to contain %q, got %q", want, got)
}
}
func TestCombinedOutput(t *testing.T) {
t.Parallel()
buf := new(bytes.Buffer)
allocCtx, cancel := NewExecAllocator(context.Background(),
append([]ExecAllocatorOption{
CombinedOutput(buf),
Flag("enable-logging", true),
}, allocOpts...)...)
defer cancel()
taskCtx, cancel := NewContext(allocCtx)
defer cancel()
if err := Run(taskCtx); err != nil {
t.Fatal(err)
}
if !strings.Contains(buf.String(), "DevTools listening on") {
t.Fatalf("failed to find websocket string in browser output test")
}
if want, got := 0, strings.Count(buf.String(), `"spam"`); want != got {
t.Fatalf("want %d spam console logs, got %d", want, got)
}
if err := Run(taskCtx,
Navigate(testdataDir+"/consolespam.html"),
); err != nil {
t.Fatal(err)
}
if want, got := 2000, strings.Count(buf.String(), `"spam"`); want != got {
t.Fatalf("want %d spam console logs, got %d", want, got)
}
}