Skip to content

Commit

Permalink
feat: add support for Page.ResetNavigationHistory (#1087)
Browse files Browse the repository at this point in the history
* feat: add support for Page.ResetNavigationHistory

* Update .gitignore

Co-authored-by: Yad Smood <ysmood@users.noreply.github.com>

* Update page.go

Co-authored-by: Yad Smood <ysmood@users.noreply.github.com>

---------

Co-authored-by: jackcipher <jackciper.vip@gmail.com>
Co-authored-by: Yad Smood <ysmood@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 11, 2024
1 parent c42b414 commit 6671261
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
6 changes: 6 additions & 0 deletions must.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ func (p *Page) MustNavigate(url string) *Page {
return p
}

// MustResetNavigationHistory is similar to [Page.ResetNavigationHistory]
func (p *Page) MustResetNavigationHistory() *Page {
p.e(p.ResetNavigationHistory())
return p
}

// MustReload is similar to [Page.Reload].
func (p *Page) MustReload() *Page {
p.e(p.Reload())
Expand Down
11 changes: 11 additions & 0 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ func (p *Page) NavigateBack() error {
return err
}

// ResetNavigationHistory reset history
func (p *Page) ResetNavigationHistory() error {
err := proto.PageResetNavigationHistory{}.Call(p)
return err
}

// GetNavigationHistory get navigation history
func (p *Page) GetNavigationHistory() (*proto.PageGetNavigationHistoryResult, error) {
return proto.PageGetNavigationHistory{}.Call(p)
}

// NavigateForward history.
func (p *Page) NavigateForward() error {
// Not using cdp API because it doesn't work for iframe
Expand Down
60 changes: 60 additions & 0 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,3 +1085,63 @@ func TestPageActionAfterClose(t *testing.T) {
g.Eq(err, context.Canceled)
}
}

func TestPageResetNavigationHistory(t *testing.T) {
const (
// After resetting history, the record should contain only the current page entry
expectedInitialHistoryLength = 1
clickHTMLPath = "fixtures/click.html"
inputHTMLPath = "fixtures/input.html"
)

g := setup(t)

// Helper function for navigation
navigateTo := func(p *rod.Page, url string) {
wait := p.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)
p.MustNavigate(url)
wait()
}

// Initialize page with blank content
page := g.page.MustNavigate(g.blank())

// Navigate to multiple pages
navigateTo(page, g.srcFile(clickHTMLPath))
navigateTo(page, g.srcFile(inputHTMLPath))

// Verify navigation back functionality
wait := page.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)
page.MustNavigateBack()
wait()

g.Regex(`/`+clickHTMLPath, page.MustInfo().URL)

// Verify history has multiple entries
initialHistory, err := page.GetNavigationHistory()
g.E(err)
g.NotNil(initialHistory)
g.Gt(len(initialHistory.Entries), expectedInitialHistoryLength)

// Test resetting navigation history
err = page.ResetNavigationHistory()
g.E(err)

// Verify history is reset to initial state
resetHistory, err := page.GetNavigationHistory()
g.E(err)
g.NotNil(resetHistory)
g.Eq(len(resetHistory.Entries), expectedInitialHistoryLength)

// Navigate to another page
navigateTo(page, g.srcFile(inputHTMLPath))

// Test resetting history again
page.MustResetNavigationHistory()

// Verify history is reset again
finalHistory, err := page.GetNavigationHistory()
g.E(err)
g.NotNil(finalHistory)
g.Eq(len(finalHistory.Entries), expectedInitialHistoryLength)
}

0 comments on commit 6671261

Please sign in to comment.