Skip to content

Commit

Permalink
add blank helper for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Oct 21, 2020
1 parent d879dbe commit 11977ae
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
13 changes: 6 additions & 7 deletions browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ import (
)

func (t T) Incognito() {
file := t.srcFile("fixtures/click.html")
k := t.Srand(16)

b := t.browser.MustIncognito().Sleeper(rod.DefaultSleeper)
defer b.MustClose()

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

t.True(t.page.MustNavigate(file).MustEval(`k => localStorage[k]`, k).Nil())
t.True(t.page.MustNavigate(t.blank()).MustEval(`k => localStorage[k]`, k).Nil())
t.Eq(page.MustEval(`k => localStorage[k]`, k).Str(), "1") // localStorage can only store string
}

Expand All @@ -56,7 +55,7 @@ func (t T) PageFromTarget() {
}

func (t T) BrowserPages() {
t.newPage(t.srcFile("fixtures/click.html")).MustWaitLoad()
t.newPage(t.blank()).MustWaitLoad()

pages := t.browser.MustPages()

Expand Down Expand Up @@ -128,13 +127,13 @@ func (t T) BrowserWaitEvent() {
t.NotNil(t.browser.Context(t.Context()).Event())

wait := t.page.WaitEvent(proto.PageFrameNavigated{})
t.page.MustNavigate(t.srcFile("fixtures/click.html"))
t.page.MustNavigate(t.blank())
wait()

wait = t.browser.EachEvent(func(e *proto.PageFrameNavigated, id proto.TargetSessionID) bool {
return true
})
t.page.MustNavigate(t.srcFile("fixtures/click.html"))
t.page.MustNavigate(t.blank())
wait()
}

Expand Down Expand Up @@ -168,7 +167,7 @@ func (t T) BrowserCall() {
func (t T) Monitor() {
b := rod.New().MustConnect()
defer b.MustClose()
p := b.MustPage(t.srcFile("fixtures/click.html")).MustWaitLoad()
p := b.MustPage(t.blank()).MustWaitLoad()

b, cancel := b.WithCancel()
defer cancel()
Expand Down
4 changes: 1 addition & 3 deletions fixtures/blank.html
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
<html>

</html>
<html></html>
2 changes: 1 addition & 1 deletion lib/utils/setup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func nodejsDeps() {
log.Fatalln("make sure Node.js is installed")
}

utils.Exec("npm", "i", "--no-audit", "--silent", "eslint-plugin-html")
utils.Exec("npm", "i", "--no-audit", "--no-fund", "--silent", "eslint-plugin-html")
}

func golangDeps() {
Expand Down
26 changes: 13 additions & 13 deletions page_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (t T) PageEvalOnNewDocument() {
p.MustEvalOnNewDocument(`window.rod = 'ok'`)

// to activate the script
p.MustNavigate(t.srcFile("fixtures/click.html"))
p.MustNavigate(t.blank())

t.Eq(p.MustEval("rod").String(), "ok")

Expand All @@ -27,7 +27,7 @@ func (t T) PageEvalOnNewDocument() {
}

func (t T) PageEval() {
page := t.page.MustNavigate(t.srcFile("fixtures/click.html"))
page := t.page.MustNavigate(t.blank())

t.Eq(3, page.MustEval(`
(a, b) => a + b
Expand All @@ -45,7 +45,7 @@ func (t T) PageEval() {
}

func (t T) PageEvalNilContext() {
page := t.page.MustNavigate(t.srcFile("fixtures/click.html"))
page := t.page.MustNavigate(t.blank())

t.mc.stub(1, proto.RuntimeCallFunctionOn{}, func(send StubSend) (gson.JSON, error) {
return gson.New(nil), cdp.ErrCtxNotFound
Expand All @@ -54,7 +54,7 @@ func (t T) PageEvalNilContext() {
}

func (t T) PageExposeJSHelper() {
page := t.page.MustNavigate(t.srcFile("fixtures/click.html"))
page := t.page.MustNavigate(t.blank())

t.Eq("undefined", page.MustEval("typeof(rod)").Str())
page.ExposeJSHelper()
Expand All @@ -64,7 +64,7 @@ func (t T) PageExposeJSHelper() {
func (t T) PageExpose() {
cb, stop := t.page.MustExpose("exposedFunc")

t.page.MustNavigate(t.srcFile("fixtures/click.html")).MustWaitLoad()
t.page.MustNavigate(t.blank()).MustWaitLoad()

t.page.MustEval(`exposedFunc({a: 'ok'})`)
t.Eq("ok", (<-cb)[0].Get("a").Str())
Expand Down Expand Up @@ -100,14 +100,14 @@ func (t T) PromiseLeak() {
we can see the slow operation will still be executed.
*/

p := t.page.MustNavigate(t.srcFile("fixtures/click.html"))
p := t.page.MustNavigate(t.blank())

utils.All(func() {
_, err := p.Eval(`new Promise(r => setTimeout(() => r(location.href), 300))`)
t.Is(err, cdp.ErrCtxDestroyed)
}, func() {
utils.Sleep(0.1)
p.MustNavigate(t.srcFile("fixtures/input.html"))
p.MustNavigate(t.blank())
})()
}

Expand All @@ -116,12 +116,12 @@ func (t T) ObjectLeak() {
Seems like it won't leak
*/

p := t.page.MustNavigate(t.srcFile("fixtures/click.html"))
p := t.page.MustNavigate(t.blank())

el := p.MustElement("button")
p.MustNavigate(t.srcFile("fixtures/input.html")).MustWaitLoad()
obj := p.MustEvaluate(rod.Eval("{a:1}").ByObject())
p.MustReload().MustWaitLoad()
t.Panic(func() {
el.MustDescribe()
p.MustEvaluate(rod.Eval(`obj => obj`, obj))
})
}

Expand All @@ -135,7 +135,7 @@ func (t T) PageObjectErr() {
t.page.MustElementFromNode(-1)
})
t.Panic(func() {
id := t.page.MustNavigate(t.srcFile("fixtures/click.html")).MustElement(`body`).MustNodeID()
id := t.page.MustNavigate(t.blank()).MustElement(`body`).MustNodeID()
t.mc.stubErr(1, proto.DOMResolveNode{})
t.page.MustElementFromNode(id)
})
Expand All @@ -151,7 +151,7 @@ func (t T) GetJSHelperRetry() {
}

func (t T) ConcurrentEval() {
p := t.page.MustNavigate(t.srcFile("fixtures/click.html"))
p := t.page.MustNavigate(t.blank())
list := make(chan int, 2)

start := time.Now()
Expand Down
22 changes: 11 additions & 11 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (t T) PageContext() {
}

func (t T) Window() {
page := t.newPage(t.srcFile("fixtures/click.html"))
page := t.newPage(t.blank())

t.E(page.SetViewport(nil))

Expand Down Expand Up @@ -177,19 +177,19 @@ func (t T) Window() {
}

func (t T) SetViewport() {
page := t.newPage(t.srcFile("fixtures/click.html"))
page := t.newPage(t.blank())
page.MustSetViewport(317, 419, 0, false)
res := page.MustEval(`[window.innerWidth, window.innerHeight]`)
t.Eq(317, res.Get("0").Int())
t.Eq(419, res.Get("1").Int())

page2 := t.newPage(t.srcFile("fixtures/click.html"))
page2 := t.newPage(t.blank())
res = page2.MustEval(`[window.innerWidth, window.innerHeight]`)
t.Neq(int(317), res.Get("0").Int())
}

func (t T) EmulateDevice() {
page := t.newPage(t.srcFile("fixtures/click.html"))
page := t.newPage(t.blank())
page.MustEmulate(devices.IPhone6or7or8Plus)
res := page.MustEval(`[window.innerWidth, window.innerHeight, navigator.userAgent]`)
t.Eq(980, res.Get("0").Int())
Expand All @@ -209,15 +209,15 @@ func (t T) EmulateDevice() {
}

func (t T) PageCloseErr() {
page := t.newPage(t.srcFile("fixtures/click.html"))
page := t.newPage(t.blank())
t.Panic(func() {
t.mc.stubErr(1, proto.PageClose{})
page.MustClose()
})
}

func (t T) PageAddScriptTag() {
p := t.page.MustNavigate(t.srcFile("fixtures/click.html")).MustWaitLoad()
p := t.page.MustNavigate(t.blank()).MustWaitLoad()

res := p.MustAddScriptTag(t.srcFile("fixtures/add-script-tag.js")).MustEval(`count()`)
t.Eq(0, res.Int())
Expand Down Expand Up @@ -392,7 +392,7 @@ func (t T) PageEventSession() {

func (t T) PageWaitEvent() {
wait := t.page.WaitEvent(&proto.PageFrameNavigated{})
t.page.MustNavigate(t.srcFile("fixtures/click.html"))
t.page.MustNavigate(t.blank())
wait()
}

Expand Down Expand Up @@ -560,7 +560,7 @@ func (t T) ScreenshotFullPage() {

p.MustScreenshotFullPage("")

noEmulation := t.newPage(t.srcFile("fixtures/click.html"))
noEmulation := t.newPage(t.blank())
t.E(noEmulation.SetViewport(nil))
noEmulation.MustScreenshotFullPage()

Expand Down Expand Up @@ -622,7 +622,7 @@ func (t T) PageScroll() {
}

func (t T) PageConsoleLog() {
p := t.newPage(t.srcFile("fixtures/click.html")).MustWaitLoad()
p := t.newPage(t.blank()).MustWaitLoad()
e := &proto.RuntimeConsoleAPICalled{}
wait := p.WaitEvent(e)
p.MustEval(`console.log(1, {b: ['test']})`)
Expand Down Expand Up @@ -681,11 +681,11 @@ func (t T) PageNavigateErr() {

t.Panic(func() {
t.mc.stubErr(1, proto.PageStopLoading{})
t.page.MustNavigate(t.srcFile("fixtures/click.html"))
t.page.MustNavigate(t.blank())
})
t.Panic(func() {
t.mc.stubErr(1, proto.PageNavigate{})
t.page.MustNavigate(t.srcFile("fixtures/click.html"))
t.page.MustNavigate(t.blank())
})
}

Expand Down
4 changes: 4 additions & 0 deletions setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ func (t T) dump(args ...interface{}) {
t.Log(utils.Dump(args))
}

func (t T) blank() string {
return t.srcFile("./fixtures/blank.html")
}

// get abs file path from fixtures folder, return sample "file:///a/b/click.html"
func (t T) srcFile(path string) string {
t.Helper()
Expand Down

0 comments on commit 11977ae

Please sign in to comment.