Skip to content

Commit 29bd861

Browse files
authored
fix: correctly handle failed navigation (#428)
1 parent 80fd809 commit 29bd861

File tree

2 files changed

+7
-47
lines changed

2 files changed

+7
-47
lines changed

frame.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,11 @@ func (f *frameImpl) ExpectNavigation(cb func() error, options ...FrameExpectNavi
209209
}
210210
predicate := func(events ...interface{}) bool {
211211
ev := events[0].(map[string]interface{})
212-
if ev["error"] != nil {
213-
print("error")
212+
err, ok := ev["error"]
213+
if ok {
214+
// Any failed navigation results in a rejection.
215+
logger.Printf("navigated to %s error: %v", ev["url"].(string), err)
216+
return true
214217
}
215218
return matcher == nil || matcher.Matches(ev["url"].(string))
216219
}

page.go

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"os"
88
"sync"
9-
"time"
109

1110
"golang.org/x/exp/slices"
1211
)
@@ -543,52 +542,10 @@ func (p *pageImpl) ExpectEvent(event string, cb func() error, options ...PageExp
543542
}
544543

545544
func (p *pageImpl) ExpectNavigation(cb func() error, options ...PageExpectNavigationOptions) (Response, error) {
546-
option := PageExpectNavigationOptions{}
547545
if len(options) == 1 {
548-
option = options[0]
549-
}
550-
if option.WaitUntil == nil {
551-
option.WaitUntil = WaitUntilStateLoad
552-
}
553-
if option.Timeout == nil {
554-
option.Timeout = Float(p.timeoutSettings.NavigationTimeout())
555-
}
556-
deadline := time.Now().Add(time.Duration(*option.Timeout) * time.Millisecond)
557-
var matcher *urlMatcher
558-
if option.URL != nil {
559-
matcher = newURLMatcher(option.URL, p.browserContext.options.BaseURL)
560-
}
561-
predicate := func(events ...interface{}) bool {
562-
ev := events[0].(map[string]interface{})
563-
if ev["error"] != nil {
564-
print("error")
565-
}
566-
return matcher == nil || matcher.Matches(ev["url"].(string))
567-
}
568-
waiter, err := p.mainFrame.(*frameImpl).setNavigationWaiter(option.Timeout)
569-
if err != nil {
570-
return nil, err
571-
}
572-
573-
eventData, err := waiter.WaitForEvent(p.mainFrame.(*frameImpl), "navigated", predicate).RunAndWait(cb)
574-
if err != nil || eventData == nil {
575-
return nil, err
576-
}
577-
578-
t := time.Until(deadline).Milliseconds()
579-
if t > 0 {
580-
err = p.mainFrame.(*frameImpl).waitForLoadStateImpl(string(*option.WaitUntil), Float(float64(t)), nil)
581-
if err != nil {
582-
return nil, err
583-
}
584-
}
585-
586-
event := eventData.(map[string]interface{})
587-
if event["newDocument"] != nil && event["newDocument"].(map[string]interface{})["request"] != nil {
588-
request := fromChannel(event["newDocument"].(map[string]interface{})["request"]).(*requestImpl)
589-
return request.Response()
546+
return p.mainFrame.ExpectNavigation(cb, FrameExpectNavigationOptions(options[0]))
590547
}
591-
return nil, nil
548+
return p.mainFrame.ExpectNavigation(cb)
592549
}
593550

594551
func (p *pageImpl) ExpectConsoleMessage(cb func() error, options ...PageExpectConsoleMessageOptions) (ConsoleMessage, error) {

0 commit comments

Comments
 (0)