Skip to content

Commit 6671261

Browse files
jackcipherjackcipherysmood
authored
feat: add support for Page.ResetNavigationHistory (#1087)
* feat: add support for Page.ResetNavigationHistory * Update .gitignore Co-authored-by: Yad Smood <[email protected]> * Update page.go Co-authored-by: Yad Smood <[email protected]> --------- Co-authored-by: jackcipher <[email protected]> Co-authored-by: Yad Smood <[email protected]>
1 parent c42b414 commit 6671261

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

must.go

+6
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,12 @@ func (p *Page) MustNavigate(url string) *Page {
233233
return p
234234
}
235235

236+
// MustResetNavigationHistory is similar to [Page.ResetNavigationHistory]
237+
func (p *Page) MustResetNavigationHistory() *Page {
238+
p.e(p.ResetNavigationHistory())
239+
return p
240+
}
241+
236242
// MustReload is similar to [Page.Reload].
237243
func (p *Page) MustReload() *Page {
238244
p.e(p.Reload())

page.go

+11
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ func (p *Page) NavigateBack() error {
197197
return err
198198
}
199199

200+
// ResetNavigationHistory reset history
201+
func (p *Page) ResetNavigationHistory() error {
202+
err := proto.PageResetNavigationHistory{}.Call(p)
203+
return err
204+
}
205+
206+
// GetNavigationHistory get navigation history
207+
func (p *Page) GetNavigationHistory() (*proto.PageGetNavigationHistoryResult, error) {
208+
return proto.PageGetNavigationHistory{}.Call(p)
209+
}
210+
200211
// NavigateForward history.
201212
func (p *Page) NavigateForward() error {
202213
// Not using cdp API because it doesn't work for iframe

page_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -1085,3 +1085,63 @@ func TestPageActionAfterClose(t *testing.T) {
10851085
g.Eq(err, context.Canceled)
10861086
}
10871087
}
1088+
1089+
func TestPageResetNavigationHistory(t *testing.T) {
1090+
const (
1091+
// After resetting history, the record should contain only the current page entry
1092+
expectedInitialHistoryLength = 1
1093+
clickHTMLPath = "fixtures/click.html"
1094+
inputHTMLPath = "fixtures/input.html"
1095+
)
1096+
1097+
g := setup(t)
1098+
1099+
// Helper function for navigation
1100+
navigateTo := func(p *rod.Page, url string) {
1101+
wait := p.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)
1102+
p.MustNavigate(url)
1103+
wait()
1104+
}
1105+
1106+
// Initialize page with blank content
1107+
page := g.page.MustNavigate(g.blank())
1108+
1109+
// Navigate to multiple pages
1110+
navigateTo(page, g.srcFile(clickHTMLPath))
1111+
navigateTo(page, g.srcFile(inputHTMLPath))
1112+
1113+
// Verify navigation back functionality
1114+
wait := page.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)
1115+
page.MustNavigateBack()
1116+
wait()
1117+
1118+
g.Regex(`/`+clickHTMLPath, page.MustInfo().URL)
1119+
1120+
// Verify history has multiple entries
1121+
initialHistory, err := page.GetNavigationHistory()
1122+
g.E(err)
1123+
g.NotNil(initialHistory)
1124+
g.Gt(len(initialHistory.Entries), expectedInitialHistoryLength)
1125+
1126+
// Test resetting navigation history
1127+
err = page.ResetNavigationHistory()
1128+
g.E(err)
1129+
1130+
// Verify history is reset to initial state
1131+
resetHistory, err := page.GetNavigationHistory()
1132+
g.E(err)
1133+
g.NotNil(resetHistory)
1134+
g.Eq(len(resetHistory.Entries), expectedInitialHistoryLength)
1135+
1136+
// Navigate to another page
1137+
navigateTo(page, g.srcFile(inputHTMLPath))
1138+
1139+
// Test resetting history again
1140+
page.MustResetNavigationHistory()
1141+
1142+
// Verify history is reset again
1143+
finalHistory, err := page.GetNavigationHistory()
1144+
g.E(err)
1145+
g.NotNil(finalHistory)
1146+
g.Eq(len(finalHistory.Entries), expectedInitialHistoryLength)
1147+
}

0 commit comments

Comments
 (0)