diff --git a/allocate.go b/allocate.go index e0ed302..a0afb62 100644 --- a/allocate.go +++ b/allocate.go @@ -43,6 +43,14 @@ func setupExecAllocator(opts ...ExecAllocatorOption) *ExecAllocator { return ep } +// DefaultExecAllocatorOptions are the ExecAllocator options used by NewContext +// if the given parent context doesn't have an allocator set up. +var DefaultExecAllocatorOptions = []ExecAllocatorOption{ + NoFirstRun, + NoDefaultBrowserCheck, + Headless, +} + // NewExecAllocator creates a new context set up with an ExecAllocator, suitable // for use with NewContext or Run. func NewExecAllocator(parent context.Context, opts ...ExecAllocatorOption) (context.Context, context.CancelFunc) { diff --git a/chromedp.go b/chromedp.go index 05a81eb..c8c9081 100644 --- a/chromedp.go +++ b/chromedp.go @@ -80,11 +80,7 @@ func NewContext(parent context.Context, opts ...ContextOption) (context.Context, o(c) } if c.Allocator == nil { - c.Allocator = setupExecAllocator( - NoFirstRun, - NoDefaultBrowserCheck, - Headless, - ) + c.Allocator = setupExecAllocator(DefaultExecAllocatorOptions...) } ctx = context.WithValue(ctx, contextKey{}, c) diff --git a/chromedp_test.go b/chromedp_test.go index 5eddc4b..a3d7d08 100644 --- a/chromedp_test.go +++ b/chromedp_test.go @@ -17,12 +17,8 @@ var ( browserCtx context.Context - allocOpts = []ExecAllocatorOption{ - NoFirstRun, - NoDefaultBrowserCheck, - Headless, - DisableGPU, - } + // allocOpts is filled in TestMain + allocOpts []ExecAllocatorOption ) func testAllocate(t *testing.T, path string) (_ context.Context, cancel func()) { @@ -52,6 +48,13 @@ func TestMain(m *testing.M) { } testdataDir = "file://" + path.Join(wd, "testdata") + // build on top of the default options + allocOpts = append(allocOpts, DefaultExecAllocatorOptions...) + + // disabling the GPU helps portability with some systems like Travis, + // and can slightly speed up the tests on other systems + allocOpts = append(allocOpts, DisableGPU) + // it's worth noting that newer versions of chrome (64+) run much faster // than older ones -- same for headless_shell ... if execPath := os.Getenv("CHROMEDP_TEST_RUNNER"); execPath != "" {