From bb2a503425cb92703a12cc85db33b420d138cdfc Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Tue, 21 Feb 2017 11:25:36 +0700 Subject: [PATCH] Standardizing NavigateBack and NavigateForward API --- nav.go | 40 ++++++++++++++++++++++------------------ nav_test.go | 14 +++----------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/nav.go b/nav.go index 13f7c5a..6258547 100644 --- a/nav.go +++ b/nav.go @@ -41,31 +41,35 @@ func NavigateToHistoryEntry(entryID int64) Action { } // NavigateBack navigates the current frame backwards in its history. -func NavigateBack(ctxt context.Context, h cdp.Handler) error { - cur, entries, err := page.GetNavigationHistory().Do(ctxt, h) - if err != nil { - return err - } +func NavigateBack() Action { + return ActionFunc(func(ctxt context.Context, h cdp.Handler) error { + cur, entries, err := page.GetNavigationHistory().Do(ctxt, h) + if err != nil { + return err + } - if cur <= 0 || cur > int64(len(entries)-1) { - return errors.New("invalid navigation entry") - } + if cur == 0 { + 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. -func NavigateForward(ctxt context.Context, h cdp.Handler) error { - cur, entries, err := page.GetNavigationHistory().Do(ctxt, h) - if err != nil { - return err - } +func NavigateForward() Action { + return ActionFunc(func(ctxt context.Context, h cdp.Handler) error { + cur, entries, err := page.GetNavigationHistory().Do(ctxt, h) + if err != nil { + return err + } - if cur < 0 || cur >= int64(len(entries)-1) { - return errors.New("invalid navigation entry") - } + if cur == int64(len(entries)-1) { + 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. diff --git a/nav_test.go b/nav_test.go index a6c1ac2..a3958aa 100644 --- a/nav_test.go +++ b/nav_test.go @@ -1,12 +1,10 @@ package chromedp import ( - "context" "strings" "testing" "time" - "github.com/knq/chromedp/cdp" "github.com/knq/chromedp/cdp/page" ) @@ -193,9 +191,7 @@ func TestNavigateBack(t *testing.T) { t.Fatal(err) } - err = c.Run(defaultContext, ActionFunc(func(c context.Context, h cdp.Handler) error { - return NavigateBack(c, h) - })) + err = c.Run(defaultContext, NavigateBack()) if err != nil { t.Fatal(err) } @@ -249,9 +245,7 @@ func TestNavigateForward(t *testing.T) { t.Fatal(err) } - err = c.Run(defaultContext, ActionFunc(func(c context.Context, h cdp.Handler) error { - return NavigateBack(c, h) - })) + err = c.Run(defaultContext, NavigateBack()) if err != nil { t.Fatal(err) } @@ -261,9 +255,7 @@ func TestNavigateForward(t *testing.T) { t.Fatal(err) } - err = c.Run(defaultContext, ActionFunc(func(c context.Context, h cdp.Handler) error { - return NavigateForward(c, h) - })) + err = c.Run(defaultContext, NavigateForward()) if err != nil { t.Fatal(err) }