Skip to content

Commit

Permalink
doc to resolve go-rod#229
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Oct 9, 2020
1 parent 21bf123 commit 140b0d0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 31 deletions.
52 changes: 49 additions & 3 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/rand"
"sync"
"time"

"github.com/go-rod/rod"
Expand Down Expand Up @@ -416,8 +417,7 @@ func Example_handle_events() {

// Listen for all events of console output.
go page.EachEvent(func(e *proto.RuntimeConsoleAPICalled) {
log := page.MustObjectsToJSON(e.Args).Join(" ")
fmt.Println(log)
fmt.Println(page.MustObjectsToJSON(e.Args))
close(done)
})()

Expand Down Expand Up @@ -458,7 +458,7 @@ func Example_handle_events() {
<-done

// Output:
// hello world
// [hello world]
}

// Shows how to intercept requests and modify
Expand Down Expand Up @@ -540,3 +540,49 @@ func ExamplePage_Event() {
fmt.Println(msg.Method, msg.Params)
}
}

// It's a common practice to concurrently use a pool of resources in Go, it's not special for rod.
func ExamplePage_pool() {
browser := rod.New().MustConnect()
defer browser.MustClose()

// we create a pool that will hold at most 3 pages
pool := make(chan *rod.Page, 3)
for i := 0; i < cap(pool); i++ {
pool <- nil
}

// a function to get an item from the pool
getOnePage := func() (*rod.Page, func()) {
page := <-pool
if page == nil {
page = browser.MustPage("")
}
return page, func() { pool <- page }
}

yourJob := func() {
page, payback := getOnePage()
defer payback()

page.MustNavigate("http://example.com").MustWaitLoad()
fmt.Println(page.MustInfo().Title)
}

// run jobs concurrently
wg := sync.WaitGroup{}
for range "...." {
wg.Add(1)
go func() {
defer wg.Done()
yourJob()
}()
}
wg.Wait()

// Output:
// Example Domain
// Example Domain
// Example Domain
// Example Domain
}
3 changes: 3 additions & 0 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ type Touch struct {

// Start a touch action
func (t *Touch) Start(points ...*proto.InputTouchPoint) error {
// TODO: https://crbug.com/613219
_, _ = t.page.Root().Eval(`new Promise(r => requestAnimationFrame(r)`)

return proto.InputDispatchTouchEvent{
Type: proto.InputDispatchTouchEventTypeTouchStart,
TouchPoints: points,
Expand Down
6 changes: 0 additions & 6 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,12 +462,6 @@ func (p *Page) WaitIdle(timeout time.Duration) (err error) {
// WaitLoad waits for the `window.onload` event, it returns immediately if the event is already fired.
func (p *Page) WaitLoad() error {
_, err := p.Evaluate(jsHelper(js.WaitLoad))
if err != nil {
return err
}

// TODO: https://crbug.com/613219
_, err = p.Root().Eval(`new Promise(r => requestAnimationFrame(() => requestAnimationFrame(r)))`)
return err
}

Expand Down
26 changes: 17 additions & 9 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,11 +566,11 @@ func (t T) NativeDrag(got.Skip) { // devtools doesn't support to use mouse event
}

func (t T) Touch() {
page := t.newPage("")
page := t.newPage("").MustEmulate(devices.IPad)

page.MustEmulate(devices.IPad).
MustNavigate(t.srcFile("fixtures/touch.html")).
MustWaitLoad()
wait := page.WaitNavigation(proto.PageLifecycleEventNameLoad)
page.MustNavigate(t.srcFile("fixtures/touch.html"))
wait()

touch := page.Touch

Expand Down Expand Up @@ -813,14 +813,22 @@ func (t T) PageWaitLoadErr() {
func (t T) PageGoBackGoForward() {
p := t.newPage("").MustReload()

p.
MustNavigate(t.srcFile("fixtures/click.html")).MustWaitLoad().
MustNavigate(t.srcFile("fixtures/selector.html")).MustWaitLoad()
wait := p.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)
p.MustNavigate(t.srcFile("fixtures/click.html"))
wait()

wait = p.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)
p.MustNavigate(t.srcFile("fixtures/selector.html"))
wait()

p.MustNavigateBack().MustWaitLoad()
wait = p.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)
p.MustNavigateBack()
wait()
t.Regex("fixtures/click.html$", p.MustInfo().URL)

p.MustNavigateForward().MustWaitLoad()
wait = p.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)
p.MustNavigateForward()
wait()
t.Regex("fixtures/selector.html$", p.MustInfo().URL)
}

Expand Down
23 changes: 10 additions & 13 deletions setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type T struct {
}

type TesterPool struct {
list chan T
list chan *T
logger *log.Logger
}

Expand All @@ -58,7 +58,7 @@ func newTesterPool(t *testing.T) TesterPool {
lf := got.New(t).Open(true, "tmp", logName)

cp := TesterPool{
list: make(chan T, parallel),
list: make(chan *T, parallel),
logger: log.New(lf, "", log.Ltime),
}

Expand All @@ -70,21 +70,15 @@ func newTesterPool(t *testing.T) TesterPool {
}()
})

wg := &sync.WaitGroup{}
wg.Add(parallel)
for i := 0; i < parallel; i++ {
go func() {
cp.list <- cp.new()
wg.Done()
}()
cp.list <- nil
}
wg.Wait()

return cp
}

// new tester
func (cp TesterPool) new() T {
func (cp TesterPool) new() *T {
u := launcher.New().MustLaunch()

mc := newMockClient(cdp.New(u).Logger(cp.logger))
Expand All @@ -97,7 +91,7 @@ func (cp TesterPool) new() T {

page := getOnePage(browser)

return T{
return &T{
mc: mc,
browser: browser,
page: page,
Expand All @@ -111,6 +105,9 @@ func (cp TesterPool) get(t *testing.T) T {
}

tester := <-cp.list
if tester == nil {
tester = cp.new()
}
t.Cleanup(func() { cp.list <- tester })

if !testing.Short() {
Expand All @@ -132,11 +129,11 @@ func (cp TesterPool) get(t *testing.T) T {

heartBeat(tester)

return tester
return *tester
}

// when concurrently run tests, indicate the busy ones
func heartBeat(t T) {
func heartBeat(t *T) {
if !testing.Short() {
return
}
Expand Down

0 comments on commit 140b0d0

Please sign in to comment.