diff --git a/handler.go b/handler.go index 0f16cfc..7cd16f6 100644 --- a/handler.go +++ b/handler.go @@ -211,10 +211,7 @@ func (t *Target) pageEvent(ev interface{}) { id, op = e.FrameID, frameDetached case *page.EventFrameStartedLoading: - // TODO: this happens before EventFrameNavigated, so the frame - // isn't in t.frames yet. - //id, op = e.FrameID, frameStartedLoading - return + id, op = e.FrameID, frameStartedLoading case *page.EventFrameStoppedLoading: id, op = e.FrameID, frameStoppedLoading @@ -237,6 +234,13 @@ func (t *Target) pageEvent(ev interface{}) { } f := t.frames[id] + if f == nil { + // This can happen if a frame is attached or starts loading + // before it's ever navigated to. We won't have all the frame + // details just yet, but that's okay. + f = &cdp.Frame{ID: id} + t.frames[id] = f + } f.Lock() defer f.Unlock() diff --git a/nav_test.go b/nav_test.go index 3168919..b1a4a4f 100644 --- a/nav_test.go +++ b/nav_test.go @@ -9,6 +9,7 @@ import ( "net/http/httptest" "strings" "testing" + "time" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/cdproto/page" @@ -345,3 +346,19 @@ func TestTitle(t *testing.T) { t.Fatalf("expected title to be %s, got: %s", exptitle, title) } } + +func TestLoadIframe(t *testing.T) { + t.Parallel() + + ctx, cancel := testAllocate(t, "iframe.html") + defer cancel() + + if err := Run(ctx, Tasks{ + // TODO: remove the sleep once we have better support for + // iframes. + Sleep(10 * time.Millisecond), + //WaitVisible(`#form`, ByID), // for the nested form.html + }); err != nil { + t.Fatal(err) + } +} diff --git a/testdata/iframe.html b/testdata/iframe.html new file mode 100644 index 0000000..72a59e0 --- /dev/null +++ b/testdata/iframe.html @@ -0,0 +1,9 @@ + + + + page with an iframe + + + + + diff --git a/util.go b/util.go index 4e21d9c..51b5d7a 100644 --- a/util.go +++ b/util.go @@ -37,9 +37,9 @@ func frameDetached(f *cdp.Frame) { clearFrameState(f, cdp.FrameAttached) } -/*func frameStartedLoading(f *cdp.Frame) { +func frameStartedLoading(f *cdp.Frame) { setFrameState(f, cdp.FrameLoading) -}*/ +} func frameStoppedLoading(f *cdp.Frame) { clearFrameState(f, cdp.FrameLoading)