From e4c16681d0a4862634055b8040794d57d5eed696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 17 Apr 2019 13:17:01 +0900 Subject: [PATCH] expose the default allocator options For example, this can be useful if a user wants to simply add one flag of their own, without otherwise messing with the default flags. In the old codebase, they'd either have to build their own list from scratch, or copy ours from source, needing to keep it in sync. --- allocate.go | 8 ++++++++ chromedp.go | 6 +----- chromedp_test.go | 15 +++++++++------ 3 files changed, 18 insertions(+), 11 deletions(-) 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 != "" {