2019-03-05 13:14:50 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExecAllocator(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-04-14 10:56:09 +00:00
|
|
|
allocCtx, cancel := NewExecAllocator(context.Background(), allocOpts...)
|
|
|
|
defer cancel()
|
2019-03-05 13:14:50 +00:00
|
|
|
|
|
|
|
// TODO: test that multiple child contexts are run in different
|
|
|
|
// processes and browsers.
|
|
|
|
|
2019-04-14 10:56:09 +00:00
|
|
|
taskCtx, cancel := NewContext(allocCtx)
|
2019-03-05 13:14:50 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
want := "insert"
|
|
|
|
var got string
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(taskCtx,
|
|
|
|
Navigate(testdataDir+"/form.html"),
|
2019-03-05 13:14:50 +00:00
|
|
|
Text("#foo", &got, ByID),
|
2019-04-01 15:57:22 +00:00
|
|
|
); err != nil {
|
2019-03-05 13:14:50 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Fatalf("wanted %q, got %q", want, got)
|
|
|
|
}
|
|
|
|
|
2019-04-07 21:40:29 +00:00
|
|
|
cancel()
|
2019-03-05 13:14:50 +00:00
|
|
|
|
2019-04-06 19:58:54 +00:00
|
|
|
tempDir := FromContext(taskCtx).Browser.userDataDir
|
|
|
|
if _, err := os.Lstat(tempDir); !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("temporary user data dir %q not deleted", tempDir)
|
2019-03-05 13:14:50 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-22 17:19:30 +00:00
|
|
|
|
|
|
|
func TestExecAllocatorCancelParent(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-04-14 10:56:09 +00:00
|
|
|
allocCtx, allocCancel := NewExecAllocator(context.Background(), allocOpts...)
|
|
|
|
defer allocCancel()
|
2019-03-22 17:19:30 +00:00
|
|
|
|
|
|
|
// TODO: test that multiple child contexts are run in different
|
|
|
|
// processes and browsers.
|
|
|
|
|
2019-04-14 10:56:09 +00:00
|
|
|
taskCtx, _ := NewContext(allocCtx)
|
2019-04-01 15:57:22 +00:00
|
|
|
if err := Run(taskCtx); err != nil {
|
2019-03-22 17:19:30 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Canceling the pool context should stop all browsers too.
|
2019-04-14 10:56:09 +00:00
|
|
|
allocCancel()
|
2019-03-22 17:19:30 +00:00
|
|
|
|
2019-04-06 19:58:54 +00:00
|
|
|
tempDir := FromContext(taskCtx).Browser.userDataDir
|
|
|
|
if _, err := os.Lstat(tempDir); !os.IsNotExist(err) {
|
|
|
|
t.Fatalf("temporary user data dir %q not deleted", tempDir)
|
2019-03-22 17:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-14 10:56:09 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|