Skip to content

Commit

Permalink
add default type exclude for Page.WaitRequestIdle
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Jul 12, 2023
1 parent ba218fa commit add0409
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 7 additions & 1 deletion must.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{""}
}
Expand All @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,14 +465,16 @@ func TestPageWaitRequestIdle(t *testing.T) {
rw.WriteHeader(http.StatusFound)
})
s.Route("/r4", "")
s.Route("/", ".html", `<html></html>`)
s.Route("/img.jpg", ".jpg", "img")
s.Route("/", ".html", `<html><body></body></html>`)

page := g.newPage(s.URL()).MustWaitLoad()

code := ` () => {
fetch('/r2').then(r => r.text())
fetch('/r1')
fetch('/r3')
document.body.innerHTML = '<img src="/img.jpg" />'
}`

waitReq := ""
Expand Down

0 comments on commit add0409

Please sign in to comment.