diff --git a/browser/mapping_test.go b/browser/mapping_test.go index 450fdc0ad..6918de38e 100644 --- a/browser/mapping_test.go +++ b/browser/mapping_test.go @@ -287,7 +287,7 @@ type browserContextAPI interface { SetDefaultTimeout(timeout int64) SetGeolocation(geolocation goja.Value) error SetHTTPCredentials(httpCredentials goja.Value) error - SetOffline(offline bool) + SetOffline(offline bool) error WaitForEvent(event string, optsOrPredicate goja.Value) (any, error) } diff --git a/common/browser_context.go b/common/browser_context.go index a92147af3..8a9aa20d5 100644 --- a/common/browser_context.go +++ b/common/browser_context.go @@ -305,13 +305,20 @@ func (b *BrowserContext) SetHTTPCredentials(httpCredentials goja.Value) error { } // SetOffline toggles the browser's connectivity on/off. -func (b *BrowserContext) SetOffline(offline bool) { +func (b *BrowserContext) SetOffline(offline bool) error { b.logger.Debugf("BrowserContext:SetOffline", "bctxid:%v offline:%t", b.id, offline) b.opts.Offline = offline for _, p := range b.browser.getPages() { - p.updateOffline() + if err := p.updateOffline(); err != nil { + return fmt.Errorf( + "setting offline status to %t for the browser context ID %s: %w", + offline, b.id, err, + ) + } } + + return nil } // Timeout will return the default timeout or the one set by the user. diff --git a/common/frame_session.go b/common/frame_session.go index 5c8f97b87..576751398 100644 --- a/common/frame_session.go +++ b/common/frame_session.go @@ -518,7 +518,9 @@ func (fs *FrameSession) initOptions() error { return err } - fs.updateOffline(true) + if err := fs.updateOffline(true); err != nil { + return err + } if err := fs.updateHTTPCredentials(true); err != nil { return err } @@ -1100,13 +1102,17 @@ func (fs *FrameSession) updateHTTPCredentials(initial bool) error { return nil } -func (fs *FrameSession) updateOffline(initial bool) { +func (fs *FrameSession) updateOffline(initial bool) error { fs.logger.Debugf("NewFrameSession:updateOffline", "sid:%v tid:%v", fs.session.ID(), fs.targetID) offline := fs.page.browserCtx.opts.Offline if !initial || offline { - fs.networkManager.SetOfflineMode(offline) + if err := fs.networkManager.SetOfflineMode(offline); err != nil { + return fmt.Errorf("updating offline mode for frame %v: %w", fs.targetID, err) + } } + + return nil } func (fs *FrameSession) throttleNetwork(networkProfile NetworkProfile) error { diff --git a/common/network_manager.go b/common/network_manager.go index 6f7fbb9fe..c5d5f902c 100644 --- a/common/network_manager.go +++ b/common/network_manager.go @@ -732,9 +732,9 @@ func (m *NetworkManager) SetExtraHTTPHeaders(headers network.Headers) { } // SetOfflineMode toggles offline mode on/off. -func (m *NetworkManager) SetOfflineMode(offline bool) { +func (m *NetworkManager) SetOfflineMode(offline bool) error { if m.offline == offline { - return + return nil } m.offline = offline @@ -745,8 +745,10 @@ func (m *NetworkManager) SetOfflineMode(offline bool) { m.networkProfile.Upload, ) if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil { - k6ext.Panic(m.ctx, "setting offline mode: %w", err) + return fmt.Errorf("emulating network conditions: %w", err) } + + return nil } // ThrottleNetwork changes the network attributes in chrome to simulate slower diff --git a/common/page.go b/common/page.go index fd6f4dfa4..8d3a95811 100644 --- a/common/page.go +++ b/common/page.go @@ -572,15 +572,19 @@ func (p *Page) updateGeolocation() error { return nil } -func (p *Page) updateOffline() { +func (p *Page) updateOffline() error { p.logger.Debugf("Page:updateOffline", "sid:%v", p.sessionID()) p.frameSessionsMu.RLock() defer p.frameSessionsMu.RUnlock() for _, fs := range p.frameSessions { - fs.updateOffline(false) + if err := fs.updateOffline(false); err != nil { + return fmt.Errorf("updating page frame sessions to offline: %w", err) + } } + + return nil } func (p *Page) updateHTTPCredentials() error {