Skip to content

Commit

Permalink
Fix dispose of original handle
Browse files Browse the repository at this point in the history
We don't want to dispose the new handle, only the original one, and
only when the handle is successfully adopted.

We also don't want to end the iteration if we fail to dispose of the
original handle since we're not going to be working with it. Chances
are that chrome will have dealt with it already.
  • Loading branch information
ankur22 committed Jan 27, 2025
1 parent 2a3a92e commit cfed5fc
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions internal/js/modules/k6/browser/common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ func (f *Frame) waitForSelectorRetry(
// It will auto retry on certain errors until the retryCount is below 0. The
// retry workaround is needed since the underlying DOM can change when the
// wait action is performed during a navigation.
func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptions) (_ *ElementHandle, rerr error) {
func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptions) (*ElementHandle, error) {
f.log.Debugf("Frame:waitForSelector", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)

handle, err := f.waitFor(selector, opts, 20)
Expand All @@ -497,21 +497,25 @@ func (f *Frame) waitForSelector(selector string, opts *FrameWaitForSelectorOptio
if ec == nil {
return nil, fmt.Errorf("waiting for selector %q: execution context %q not found", selector, mainWorld)
}

// an element should belong to the current execution context.
// otherwise, we should adopt it to this execution context.
adopted := handle
if ec != handle.execCtx {
defer func(handle *ElementHandle) {
if err := handle.Dispose(); err != nil {
err = fmt.Errorf("disposing element handle: %w", err)
rerr = errors.Join(err, rerr)
}
}(handle)
if handle, err = ec.adoptElementHandle(handle); err != nil {
if adopted, err = ec.adoptElementHandle(handle); err != nil {
return nil, fmt.Errorf("waiting for selector %q: adopting element handle: %w", selector, err)
}

if err = handle.Dispose(); err != nil {
f.log.Warnf(
"Frame:waitForSelector",
"fid:%s furl:%q sel:%q disposing element handle: %v",
f.ID(), f.URL(), selector, err,
)
}
}

return handle, nil
return adopted, nil
}

func (f *Frame) waitFor(
Expand Down

0 comments on commit cfed5fc

Please sign in to comment.