Skip to content

Commit

Permalink
fix tests that are leaking pages
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 23, 2020
1 parent c308bfa commit 6fc4075
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
15 changes: 9 additions & 6 deletions browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (s *S) TestIncognito() {

b := s.browser.MustIncognito()
page := b.MustPage(file)
defer page.MustClose()
page.MustEval(`k => localStorage[k] = 1`, k)

s.Nil(s.page.MustNavigate(file).MustEval(`k => localStorage[k]`, k).Value())
Expand All @@ -43,6 +44,10 @@ func (s *S) TestPageFromTarget() {
s.Panics(func() {
res, err := proto.TargetCreateTarget{URL: "about:blank"}.Call(s.browser)
utils.E(err)
defer func() {
s.browser.MustPageFromTargetID(res.TargetID).MustClose()
}()

s.mc.stubErr(1, proto.EmulationSetDeviceMetricsOverride{})
s.browser.MustPageFromTargetID(res.TargetID)
})
Expand All @@ -54,19 +59,17 @@ func (s *S) TestBrowserPages() {

pages := s.browser.MustPages()

// TODO: I don't know why sometimes windows can miss one
if runtime.GOOS == "windows" {
s.GreaterOrEqual(len(pages), 2)
} else {
s.Len(pages, 3)
s.Len(pages, 2)

{
s.mc.stub(1, proto.TargetGetTargets{}, func(send func() ([]byte, error)) ([]byte, error) {
d, _ := send()
return sjson.SetBytes(d, "targetInfos.0.type", "iframe")
})
pages := s.browser.MustPages()
s.Len(pages, 2)
s.Len(pages, 1)
}

s.Panics(func() {
s.mc.stubErr(1, proto.TargetCreateTarget{})
s.browser.MustPage("")
Expand Down
6 changes: 5 additions & 1 deletion hijack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,14 @@ func (s *S) TestHandleAuth() {
page.MustElementR("p", "ok")

wait := s.browser.HandleAuth("a", "b")
wait2 := utils.All(func() { _, _ = s.browser.Page(url + "/err") })
var page2 *rod.Page
wait2 := utils.All(func() {
page2, _ = s.browser.Page(url + "/err")
})
s.mc.stubErr(1, proto.FetchContinueRequest{})
s.Error(wait())
wait2()
page2.MustClose()
}

func (s *S) TestGetDownloadFile() {
Expand Down
7 changes: 2 additions & 5 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,8 @@ func (p *Page) WaitOpen() func() (*Page, error) {

ctx, cancel := context.WithCancel(p.ctx)
wait := b.Context(ctx).EachEvent(func(e *proto.TargetTargetCreated) bool {
if e.TargetInfo.OpenerID == p.TargetID {
targetID = e.TargetInfo.TargetID
return true
}
return false
targetID = e.TargetInfo.TargetID
return e.TargetInfo.OpenerID == p.TargetID
})

return func() (*Page, error) {
Expand Down
4 changes: 2 additions & 2 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func (s *S) TestEmulateDevice() {

func (s *S) TestPageCloseErr() {
page := s.browser.MustPage(srcFile("fixtures/click.html"))
defer page.MustClose()
s.Panics(func() {
s.mc.stubErr(1, proto.PageStopLoading{})
page.MustClose()
Expand Down Expand Up @@ -310,8 +311,6 @@ func (s *S) TestPageWaitOpen() {

wait := page.MustWaitOpen()

s.browser.MustPage("").MustClose()

page.MustElement("a").MustClick()

newPage := wait()
Expand Down Expand Up @@ -626,6 +625,7 @@ func (s *S) TestScreenshotFullPage() {
s.Len(list, 1)

noEmulation := s.browser.MustPage(srcFile("fixtures/click.html"))
defer noEmulation.MustClose()
utils.E(noEmulation.SetViewport(nil))
noEmulation.MustScreenshotFullPage()

Expand Down
5 changes: 4 additions & 1 deletion setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func Test(t *testing.T) {

s.browser.MustIgnoreCertErrors(false)

s.page = s.browser.MustPages().First()
// the page may still under creation
for s.page = s.browser.MustPages().First(); s.page == nil; {
utils.Sleep(0.1)
}

s.goleakIgnore = goleak.IgnoreCurrent()

Expand Down

0 comments on commit 6fc4075

Please sign in to comment.