Skip to content

Commit

Permalink
make WaitStable more general
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Jul 18, 2023
1 parent add0409 commit b77daf9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
16 changes: 8 additions & 8 deletions must.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,13 +416,7 @@ 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, []proto.NetworkResourceType{
proto.NetworkResourceTypeWebSocket,
proto.NetworkResourceTypeEventSource,
proto.NetworkResourceTypeMedia,
proto.NetworkResourceTypeImage,
proto.NetworkResourceTypeFont,
})
return p.WaitRequestIdle(300*time.Millisecond, nil, excludes, nil)
}

// MustWaitIdle is similar to Page.WaitIdle
Expand All @@ -431,9 +425,15 @@ func (p *Page) MustWaitIdle() *Page {
return p
}

// MustWaitDOMStable is similar to Page.WaitDOMStable
func (p *Page) MustWaitDOMStable() *Page {
p.e(p.WaitDOMStable(time.Second, 0))
return p
}

// MustWaitStable is similar to Page.WaitStable
func (p *Page) MustWaitStable() *Page {
p.e(p.WaitStable(time.Second, 0))
p.e(p.WaitStable(time.Second))
return p
}

Expand Down
36 changes: 33 additions & 3 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,16 @@ func (p *Page) WaitNavigation(name proto.PageLifecycleEventName) func() {
func (p *Page) WaitRequestIdle(d time.Duration, includes, excludes []string, excludeTypes []proto.NetworkResourceType) func() {
defer p.tryTrace(TraceTypeWait, "request-idle")()

if excludeTypes == nil {
excludeTypes = []proto.NetworkResourceType{
proto.NetworkResourceTypeWebSocket,
proto.NetworkResourceTypeEventSource,
proto.NetworkResourceTypeMedia,
proto.NetworkResourceTypeImage,
proto.NetworkResourceTypeFont,
}
}

if len(includes) == 0 {
includes = []string{""}
}
Expand Down Expand Up @@ -637,11 +647,11 @@ func (p *Page) WaitRequestIdle(d time.Duration, includes, excludes []string, exc
}
}

// WaitStable waits until the change of the DOM tree is less or equal than diff percent for d duration.
// WaitDOMStable waits until the change of the DOM tree is less or equal than diff percent for d duration.
// Be careful, d is not the max wait timeout, it's the least stable time.
// If you want to set a timeout you can use the "Page.Timeout" function.
func (p *Page) WaitStable(d time.Duration, diff float64) error {
defer p.tryTrace(TraceTypeWait, "stable")()
func (p *Page) WaitDOMStable(d time.Duration, diff float64) error {
defer p.tryTrace(TraceTypeWait, "dom-stable")()

domSnapshot, err := p.CaptureDOMSnapshot()
if err != nil {
Expand Down Expand Up @@ -677,6 +687,26 @@ func (p *Page) WaitStable(d time.Duration, diff float64) error {
return nil
}

// WaitStable waits until the page is stable for d duration.
func (p *Page) WaitStable(d time.Duration) error {
defer p.tryTrace(TraceTypeWait, "stable")()

var err error
setErr := sync.Once{}

utils.All(func() {
e := p.WaitLoad()
setErr.Do(func() { err = e })
}, func() {
p.WaitRequestIdle(d, nil, nil, nil)()
}, func() {
e := p.WaitDOMStable(d, 0)
setErr.Do(func() { err = e })
})()

return err
}

// WaitIdle waits until the next window.requestIdleCallback is called.
func (p *Page) WaitIdle(timeout time.Duration) (err error) {
_, err = p.Evaluate(evalHelper(js.WaitIdle, timeout.Milliseconds()).ByPromise())
Expand Down
17 changes: 12 additions & 5 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,37 +524,44 @@ func TestPageCaptureDOMSnapshot(t *testing.T) {
g.Nil(snapshot)
}

func TestPageWaitStable(t *testing.T) {
func TestPageWaitDOMStable(t *testing.T) {
g := setup(t)

{
p := g.page.MustNavigate(g.srcFile("fixtures/page-wait-stable.html"))
p.MustWaitStable()
p.MustWaitDOMStable()
}

{
p := g.page.MustNavigate(g.srcFile("fixtures/page-wait-stable.html"))
err := p.Timeout(time.Second).WaitStable(time.Second, 0)
err := p.Timeout(time.Second).WaitDOMStable(time.Second, 0)
g.Is(err, context.DeadlineExceeded)
}

{
g.Panic(func() {
p := g.page.MustNavigate(g.srcFile("fixtures/page-wait-stable.html"))
g.mc.stubErr(1, proto.DOMSnapshotCaptureSnapshot{})
p.MustWaitStable()
p.MustWaitDOMStable()
})
}

{
g.Panic(func() {
p := g.page.MustNavigate(g.srcFile("fixtures/page-wait-stable.html"))
g.mc.stubErr(2, proto.DOMSnapshotCaptureSnapshot{})
p.MustWaitStable()
p.MustWaitDOMStable()
})
}
}

func TestPageWaitStable(t *testing.T) {
g := setup(t)

p := g.page.MustNavigate(g.srcFile("fixtures/page-wait-stable.html"))
p.MustWaitStable()
}

func TestPageWaitIdle(t *testing.T) {
g := setup(t)

Expand Down

0 comments on commit b77daf9

Please sign in to comment.