Skip to content

Commit

Permalink
fix: correctly handle failed navigation (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
canstand committed Mar 21, 2024
1 parent 80fd809 commit 29bd861
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 47 deletions.
7 changes: 5 additions & 2 deletions frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,11 @@ func (f *frameImpl) ExpectNavigation(cb func() error, options ...FrameExpectNavi
}
predicate := func(events ...interface{}) bool {
ev := events[0].(map[string]interface{})
if ev["error"] != nil {
print("error")
err, ok := ev["error"]
if ok {
// Any failed navigation results in a rejection.
logger.Printf("navigated to %s error: %v", ev["url"].(string), err)
return true
}
return matcher == nil || matcher.Matches(ev["url"].(string))
}
Expand Down
47 changes: 2 additions & 45 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"os"
"sync"
"time"

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

func (p *pageImpl) ExpectNavigation(cb func() error, options ...PageExpectNavigationOptions) (Response, error) {
option := PageExpectNavigationOptions{}
if len(options) == 1 {
option = options[0]
}
if option.WaitUntil == nil {
option.WaitUntil = WaitUntilStateLoad
}
if option.Timeout == nil {
option.Timeout = Float(p.timeoutSettings.NavigationTimeout())
}
deadline := time.Now().Add(time.Duration(*option.Timeout) * time.Millisecond)
var matcher *urlMatcher
if option.URL != nil {
matcher = newURLMatcher(option.URL, p.browserContext.options.BaseURL)
}
predicate := func(events ...interface{}) bool {
ev := events[0].(map[string]interface{})
if ev["error"] != nil {
print("error")
}
return matcher == nil || matcher.Matches(ev["url"].(string))
}
waiter, err := p.mainFrame.(*frameImpl).setNavigationWaiter(option.Timeout)
if err != nil {
return nil, err
}

eventData, err := waiter.WaitForEvent(p.mainFrame.(*frameImpl), "navigated", predicate).RunAndWait(cb)
if err != nil || eventData == nil {
return nil, err
}

t := time.Until(deadline).Milliseconds()
if t > 0 {
err = p.mainFrame.(*frameImpl).waitForLoadStateImpl(string(*option.WaitUntil), Float(float64(t)), nil)
if err != nil {
return nil, err
}
}

event := eventData.(map[string]interface{})
if event["newDocument"] != nil && event["newDocument"].(map[string]interface{})["request"] != nil {
request := fromChannel(event["newDocument"].(map[string]interface{})["request"]).(*requestImpl)
return request.Response()
return p.mainFrame.ExpectNavigation(cb, FrameExpectNavigationOptions(options[0]))
}
return nil, nil
return p.mainFrame.ExpectNavigation(cb)
}

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

0 comments on commit 29bd861

Please sign in to comment.