From 7c1a9fbf3e136af1a90ab7bfcfc165df3a03cc1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 20 Mar 2019 16:56:03 +0000 Subject: [PATCH] get rid of all exceptions We hadn't noticed a few uncaught exceptions being received from the browser, because the events were ignored. Start printing them via the error logger. The ones we were getting were caused by testAllocate running Navigate actions when the path argument was empty. Navigating to "testdata/" causes JS exceptions, as it's not a valid page. Instead, leave the new target pointing at a blank document. --- browser.go | 9 +++++++++ chromedp_test.go | 7 +++++-- nav_test.go | 12 ++++++------ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/browser.go b/browser.go index b8c1bdc..7b38d0b 100644 --- a/browser.go +++ b/browser.go @@ -14,6 +14,7 @@ import ( "github.com/chromedp/cdproto" "github.com/chromedp/cdproto/cdp" + "github.com/chromedp/cdproto/runtime" "github.com/chromedp/cdproto/target" "github.com/mailru/easyjson" ) @@ -203,6 +204,14 @@ func (b *Browser) run(ctx context.Context) { // these events? continue } + if msg.Method == cdproto.EventRuntimeExceptionThrown { + ev := new(runtime.EventExceptionThrown) + if err := json.Unmarshal(msg.Params, ev); err != nil { + b.errf("%s", err) + continue + } + b.errf("%+v\n", ev.ExceptionDetails) + } page, ok := b.pages[sessionID] if !ok { diff --git a/chromedp_test.go b/chromedp_test.go index 018ad26..76e9877 100644 --- a/chromedp_test.go +++ b/chromedp_test.go @@ -23,8 +23,11 @@ var ( func testAllocate(t *testing.T, path string) (_ context.Context, cancel func()) { ctx, cancel := NewContext(allocCtx) - if err := Run(ctx, Navigate(testdataDir+"/"+path)); err != nil { - t.Fatal(err) + // Only navigate if we want a path, otherwise leave the blank page. + if path != "" { + if err := Run(ctx, Navigate(testdataDir+"/"+path)); err != nil { + t.Fatal(err) + } } //if err := WithLogf(t.Logf)(c.c); err != nil { diff --git a/nav_test.go b/nav_test.go index 9a457c9..4b73dcd 100644 --- a/nav_test.go +++ b/nav_test.go @@ -56,14 +56,14 @@ func TestNavigationEntries(t *testing.T) { t.Fatal(err) } - if len(entries) != 2 { - t.Errorf("expected to have 2 navigation entry: got %d", len(entries)) + if len(entries) != 1 { + t.Errorf("expected to have 1 navigation entry: got %d", len(entries)) } - if index != 1 { - t.Errorf("expected navigation index is 1, got: %d", index) + if index != 0 { + t.Errorf("expected navigation index is 0, got: %d", index) } - expIdx, expEntries := 2, 3 + expIdx, expEntries := 1, 2 for i, url := range tests { if err := Run(ctx, Navigate(testdataDir+"/"+url)); err != nil { t.Fatal(err) @@ -77,7 +77,7 @@ func TestNavigationEntries(t *testing.T) { if len(entries) != expEntries { t.Errorf("test %d expected to have %d navigation entry: got %d", i, expEntries, len(entries)) } - if want := int64(i + 2); index != want { + if want := int64(i + 1); index != want { t.Errorf("test %d expected navigation index is %d, got: %d", i, want, index) }