Standardizing NavigateBack and NavigateForward API

This commit is contained in:
Kenneth Shaw 2017-02-21 11:25:36 +07:00
parent 786e242a24
commit bb2a503425
2 changed files with 25 additions and 29 deletions

40
nav.go
View File

@ -41,31 +41,35 @@ func NavigateToHistoryEntry(entryID int64) Action {
} }
// NavigateBack navigates the current frame backwards in its history. // NavigateBack navigates the current frame backwards in its history.
func NavigateBack(ctxt context.Context, h cdp.Handler) error { func NavigateBack() Action {
cur, entries, err := page.GetNavigationHistory().Do(ctxt, h) return ActionFunc(func(ctxt context.Context, h cdp.Handler) error {
if err != nil { cur, entries, err := page.GetNavigationHistory().Do(ctxt, h)
return err if err != nil {
} return err
}
if cur <= 0 || cur > int64(len(entries)-1) { if cur == 0 {
return errors.New("invalid navigation entry") return errors.New("already on oldest navigation entry")
} }
return page.NavigateToHistoryEntry(entries[cur-1].ID).Do(ctxt, h) return page.NavigateToHistoryEntry(entries[cur-1].ID).Do(ctxt, h)
})
} }
// NavigateForward navigates the current frame forwards in its history. // NavigateForward navigates the current frame forwards in its history.
func NavigateForward(ctxt context.Context, h cdp.Handler) error { func NavigateForward() Action {
cur, entries, err := page.GetNavigationHistory().Do(ctxt, h) return ActionFunc(func(ctxt context.Context, h cdp.Handler) error {
if err != nil { cur, entries, err := page.GetNavigationHistory().Do(ctxt, h)
return err if err != nil {
} return err
}
if cur < 0 || cur >= int64(len(entries)-1) { if cur == int64(len(entries)-1) {
return errors.New("invalid navigation entry") return errors.New("already on newest navigation entry")
} }
return page.NavigateToHistoryEntry(entries[cur+1].ID).Do(ctxt, h) return page.NavigateToHistoryEntry(entries[cur+1].ID).Do(ctxt, h)
})
} }
// Stop stops all navigation and pending resource retrieval. // Stop stops all navigation and pending resource retrieval.

View File

@ -1,12 +1,10 @@
package chromedp package chromedp
import ( import (
"context"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/knq/chromedp/cdp"
"github.com/knq/chromedp/cdp/page" "github.com/knq/chromedp/cdp/page"
) )
@ -193,9 +191,7 @@ func TestNavigateBack(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = c.Run(defaultContext, ActionFunc(func(c context.Context, h cdp.Handler) error { err = c.Run(defaultContext, NavigateBack())
return NavigateBack(c, h)
}))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -249,9 +245,7 @@ func TestNavigateForward(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = c.Run(defaultContext, ActionFunc(func(c context.Context, h cdp.Handler) error { err = c.Run(defaultContext, NavigateBack())
return NavigateBack(c, h)
}))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -261,9 +255,7 @@ func TestNavigateForward(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
err = c.Run(defaultContext, ActionFunc(func(c context.Context, h cdp.Handler) error { err = c.Run(defaultContext, NavigateForward())
return NavigateForward(c, h)
}))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }