2019-03-05 13:14:50 +00:00
|
|
|
package chromedp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestExecAllocator(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2019-03-22 17:19:30 +00:00
|
|
|
poolCtx, poolCancel := NewAllocator(context.Background(), WithExecAllocator(allocOpts...))
|
|
|
|
defer poolCancel()
|
2019-03-05 13:14:50 +00:00
|
|
|
|
|
|
|
// TODO: test that multiple child contexts are run in different
|
|
|
|
// processes and browsers.
|
|
|
|
|
|
|
|
taskCtx, cancel := NewContext(poolCtx)
|
|
|
|
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()
|
|
|
|
|
|
|
|
poolCtx, poolCancel := NewAllocator(context.Background(), WithExecAllocator(allocOpts...))
|
|
|
|
defer poolCancel()
|
|
|
|
|
|
|
|
// TODO: test that multiple child contexts are run in different
|
|
|
|
// processes and browsers.
|
|
|
|
|
|
|
|
taskCtx, _ := NewContext(poolCtx)
|
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.
|
|
|
|
poolCancel()
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|