Skip to content

Commit

Permalink
do not start a browser in headless mode by default
Browse files Browse the repository at this point in the history
Fixes #845
  • Loading branch information
ZekeLu committed Jul 13, 2021
1 parent e43a192 commit a2ec9e1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 25 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ full page screenshots.

## Frequently Asked Questions

> I can't see any Chrome browser window
By default, Chrome is run in headless mode. See `DefaultExecAllocatorOptions`, and
[an example][goref-chromedp-exec-allocator] to override the default options.

> I'm seeing "context canceled" errors
When the connection to the browser is lost, `chromedp` cancels the context, and
Expand Down Expand Up @@ -64,7 +59,17 @@ chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {
The simplest way is to run the Go program that uses chromedp inside the
[chromedp/headless-shell][docker-headless-shell] image. That image contains
`headless-shell`, a smaller headless build of Chrome, which `chromedp` is able
to find out of the box.
to find out of the box. If other browser is used in the headless environment,
don't forget to set the `--headless` option like this:

```go
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
allocCtx, allocCancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer allocCancel()
ctx, cancel := chromedp.NewContext(allocCtx)
defer cancel()
// use the ctx
```

## Resources

Expand Down
1 change: 0 additions & 1 deletion allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func setupExecAllocator(opts ...ExecAllocatorOption) *ExecAllocator {
var DefaultExecAllocatorOptions = [...]ExecAllocatorOption{
NoFirstRun,
NoDefaultBrowserCheck,
Headless,

// After Puppeteer's default behavior.
Flag("disable-background-networking", true),
Expand Down
17 changes: 13 additions & 4 deletions chromedp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func init() {
// and can slightly speed up the tests on other systems.
allocOpts = append(allocOpts, DisableGPU)

if noHeadless := os.Getenv("CHROMEDP_NO_HEADLESS"); noHeadless != "" && noHeadless != "false" {
allocOpts = append(allocOpts, Flag("headless", false))
if noHeadless := os.Getenv("CHROMEDP_NO_HEADLESS"); noHeadless == "" || noHeadless == "false" {
allocOpts = append(allocOpts, Headless)
}

// Find the exec path once at startup.
Expand Down Expand Up @@ -310,6 +310,7 @@ func TestPrematureCancelAllocator(t *testing.T) {

// To ensure we don't actually fire any Chrome processes.
allocCtx, cancel := NewExecAllocator(context.Background(),
Headless,
ExecPath("/do-not-run-chrome"))
// Cancel before the browser is allocated.
cancel()
Expand All @@ -326,6 +327,7 @@ func TestConcurrentCancel(t *testing.T) {

// To ensure we don't actually fire any Chrome processes.
allocCtx, cancel := NewExecAllocator(context.Background(),
Headless,
ExecPath("/do-not-run-chrome"))
defer cancel()

Expand Down Expand Up @@ -555,7 +557,10 @@ func TestLogOptions(t *testing.T) {
bufMu.Unlock()
}

ctx, cancel := NewContext(context.Background(),
opts := append(DefaultExecAllocatorOptions[:], Headless)
ctx, cancel := NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = NewContext(ctx,
WithErrorf(fn),
WithLogf(fn),
WithDebugf(fn),
Expand Down Expand Up @@ -728,6 +733,8 @@ func TestGracefulBrowserShutdown(t *testing.T) {
func TestAttachingToWorkers(t *testing.T) {
t.Parallel()

opts := append(DefaultExecAllocatorOptions[:], Headless)

for _, tc := range []struct {
desc, pageJS, wantSelf string
}{
Expand All @@ -754,7 +761,9 @@ func TestAttachingToWorkers(t *testing.T) {
ts := httptest.NewServer(mux)
defer ts.Close()

ctx, cancel := NewContext(context.Background())
ctx, cancel := NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = NewContext(ctx)
defer cancel()

ch := make(chan target.ID, 1)
Expand Down
71 changes: 57 additions & 14 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func writeHTML(content string) http.Handler {
}

func ExampleTitle() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

ts := httptest.NewServer(writeHTML(`
Expand All @@ -58,7 +61,10 @@ func ExampleTitle() {
}

func ExampleRunResponse() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

// This server simply shows the URL path as the page title, and contains
Expand Down Expand Up @@ -123,6 +129,7 @@ func ExampleExecAllocator() {
defer os.RemoveAll(dir)

opts := append(chromedp.DefaultExecAllocatorOptions[:],
chromedp.Headless,
chromedp.DisableGPU,
chromedp.UserDataDir(dir),
)
Expand Down Expand Up @@ -169,7 +176,10 @@ func ExampleNewContext_reuseBrowser() {
defer ts.Close()

// create a new browser
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

// start the browser without a timeout
Expand Down Expand Up @@ -200,8 +210,11 @@ func ExampleNewContext_reuseBrowser() {
}

func ExampleNewContext_manyTabs() {
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
// new browser, first tab
ctx1, cancel := chromedp.NewContext(context.Background())
ctx1, cancel := chromedp.NewContext(ctx)
defer cancel()

// ensure the first tab is created
Expand Down Expand Up @@ -229,7 +242,10 @@ func ExampleNewContext_manyTabs() {
}

func ExampleListenTarget_consoleLog() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

ts := httptest.NewServer(writeHTML(`
Expand Down Expand Up @@ -276,7 +292,10 @@ func ExampleListenTarget_consoleLog() {
}

func ExampleWaitNewTarget() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

mux := http.NewServeMux()
Expand Down Expand Up @@ -311,7 +330,10 @@ func ExampleWaitNewTarget() {
}

func ExampleListenTarget_acceptAlert() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

mux := http.NewServeMux()
Expand Down Expand Up @@ -346,7 +368,10 @@ func ExampleListenTarget_acceptAlert() {
}

func Example_retrieveHTML() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

ts := httptest.NewServer(writeHTML(`
Expand Down Expand Up @@ -383,7 +408,10 @@ function changeText() {
}

func ExampleEmulate() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

var buf []byte
Expand All @@ -405,7 +433,10 @@ func ExampleEmulate() {
}

func ExamplePrintToPDF() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

var buf []byte
Expand All @@ -431,7 +462,10 @@ func ExamplePrintToPDF() {
}

func ExampleByJSPath() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

ts := httptest.NewServer(writeHTML(`
Expand Down Expand Up @@ -466,7 +500,10 @@ func ExampleByJSPath() {
}

func ExampleFromNode() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

ts := httptest.NewServer(writeHTML(`
Expand Down Expand Up @@ -514,7 +551,10 @@ func ExampleFromNode() {
}

func Example_documentDump() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

ts := httptest.NewServer(writeHTML(`<!doctype html>
Expand Down Expand Up @@ -571,7 +611,10 @@ func Example_documentDump() {
}

func ExampleFullScreenshot() {
ctx, cancel := chromedp.NewContext(context.Background())
opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.Headless)
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx)
defer cancel()

var buf []byte
Expand Down

0 comments on commit a2ec9e1

Please sign in to comment.