Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly handle failed navigation #428

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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