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-03-21 15:44:28 +00:00
|
|
|
tempDir := FromContext(taskCtx).Browser.userDataDir
|
2019-03-05 13:14:50 +00:00
|
|
|
pool := FromContext(taskCtx).Allocator
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
pool.Wait()
|
|
|
|
|
|
|
|
if _, err := os.Lstat(tempDir); os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Fatalf("temporary user data dir %q not deleted", tempDir)
|
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
tempDir := FromContext(taskCtx).Browser.userDataDir
|
|
|
|
pool := FromContext(taskCtx).Allocator
|
|
|
|
|
|
|
|
// Canceling the pool context should stop all browsers too.
|
|
|
|
poolCancel()
|
|
|
|
pool.Wait()
|
|
|
|
|
|
|
|
if _, err := os.Lstat(tempDir); os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Fatalf("temporary user data dir %q not deleted", tempDir)
|
|
|
|
}
|