diff --git a/must.go b/must.go index ba24cc0e..fbdfaf97 100644 --- a/must.go +++ b/must.go @@ -416,7 +416,13 @@ func (p *Page) MustWaitNavigation() func() { // MustWaitRequestIdle is similar to Page.WaitRequestIdle func (p *Page) MustWaitRequestIdle(excludes ...string) (wait func()) { - return p.WaitRequestIdle(300*time.Millisecond, nil, excludes) + return p.WaitRequestIdle(300*time.Millisecond, nil, excludes, []proto.NetworkResourceType{ + proto.NetworkResourceTypeWebSocket, + proto.NetworkResourceTypeEventSource, + proto.NetworkResourceTypeMedia, + proto.NetworkResourceTypeImage, + proto.NetworkResourceTypeFont, + }) } // MustWaitIdle is similar to Page.WaitIdle diff --git a/page.go b/page.go index cc347f88..c5b811e0 100644 --- a/page.go +++ b/page.go @@ -584,7 +584,9 @@ func (p *Page) WaitNavigation(name proto.PageLifecycleEventName) func() { // Be careful, d is not the max wait timeout, it's the least idle time. // If you want to set a timeout you can use the "Page.Timeout" function. // Use the includes and excludes regexp list to filter the requests by their url. -func (p *Page) WaitRequestIdle(d time.Duration, includes, excludes []string) func() { +func (p *Page) WaitRequestIdle(d time.Duration, includes, excludes []string, excludeTypes []proto.NetworkResourceType) func() { + defer p.tryTrace(TraceTypeWait, "request-idle")() + if len(includes) == 0 { includes = []string{""} } @@ -605,6 +607,12 @@ func (p *Page) WaitRequestIdle(d time.Duration, includes, excludes []string) fun } wait := p.EachEvent(func(sent *proto.NetworkRequestWillBeSent) { + for _, t := range excludeTypes { + if sent.Type == t { + return + } + } + if match(sent.Request.URL) { // Redirect will send multiple NetworkRequestWillBeSent events with the same RequestID, // we should filter them out. diff --git a/page_test.go b/page_test.go index 51096ad2..c8ae13d2 100644 --- a/page_test.go +++ b/page_test.go @@ -465,7 +465,8 @@ func TestPageWaitRequestIdle(t *testing.T) { rw.WriteHeader(http.StatusFound) }) s.Route("/r4", "") - s.Route("/", ".html", ``) + s.Route("/img.jpg", ".jpg", "img") + s.Route("/", ".html", ``) page := g.newPage(s.URL()).MustWaitLoad() @@ -473,6 +474,7 @@ func TestPageWaitRequestIdle(t *testing.T) { fetch('/r2').then(r => r.text()) fetch('/r1') fetch('/r3') + document.body.innerHTML = '' }` waitReq := ""