Skip to content

Commit a231691

Browse files
committed
Update check hit target
We now recurse through all the parent frames looking for a valid hit. I'm not certain this is a good change, and i think this will result in unwanted behaviour (trying to click on an element that is blocked by another element e.g. cookie banner).
1 parent c2a0239 commit a231691

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

internal/js/modules/k6/browser/common/element_handle.go

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,27 +74,16 @@ func (h *ElementHandle) boundingBox() (*Rect, error) {
7474
return &Rect{X: x + position.X, Y: y + position.Y, Width: width, Height: height}, nil
7575
}
7676

77+
// checkHitTargetAt checks if the element is hit by the pointer at the given point.
78+
//
79+
// It will recurse through frames and iframes to check if the point hits a target
80+
// when an error:intercept occurs.
81+
//
82+
// It will eventually return error:intercept if the point doesn't hit any target. At
83+
// this point the caller should retry the action after scrolling.
7784
func (h *ElementHandle) checkHitTargetAt(apiCtx context.Context, point Position) (bool, error) {
78-
frame, err := h.ownerFrame(apiCtx)
79-
if err != nil {
80-
return false, fmt.Errorf("checking hit target at %v: %w", point, err)
81-
}
82-
if frame != nil && frame.parentFrame != nil {
83-
el, err := frame.FrameElement()
84-
if err != nil {
85-
return false, err
86-
}
87-
box, err := el.boundingBox()
88-
if err != nil {
89-
return false, err
90-
}
91-
if box == nil {
92-
return false, errors.New("missing bounding box of element")
93-
}
94-
// Translate from viewport coordinates to frame coordinates.
95-
point.X -= box.X
96-
point.Y -= box.Y
97-
}
85+
h.logger.Debugf("ElementHandle:checkHitTargetAt", "checking hit target at %v", point)
86+
9887
fn := `
9988
(node, injected, point) => {
10089
return injected.checkHitTargetAt(node, point);
@@ -112,14 +101,28 @@ func (h *ElementHandle) checkHitTargetAt(apiCtx context.Context, point Position)
112101
// Either we're done or an error happened (returned as "error:..." from JS)
113102
const done = resultDone
114103
if v, ok := result.(string); !ok {
115-
// We got a { hitTargetDescription: ... } result
116-
// Meaning: Another element is preventing pointer events.
117-
//
118-
// It's safe to count an object return as an interception.
119-
// We just don't interpret what is intercepting with the target element
120-
// because we don't need any more functionality from this JS function
121-
// right now.
122-
return false, errorFromDOMError("error:intercept")
104+
frame, err := h.ownerFrame(apiCtx)
105+
if err != nil {
106+
return false, fmt.Errorf("checking hit target at %v: %w", point, err)
107+
}
108+
109+
if frame == nil {
110+
// We got a { hitTargetDescription: ... } result
111+
// Meaning: Another element is preventing pointer events.
112+
//
113+
// It's safe to count an object return as an interception.
114+
// We just don't interpret what is intercepting with the target element
115+
// because we don't need any more functionality from this JS function
116+
// right now.
117+
return false, errorFromDOMError("error:intercept")
118+
}
119+
120+
el, err := frame.FrameElement()
121+
if err != nil {
122+
return false, err
123+
}
124+
125+
return el.checkHitTargetAt(apiCtx, point)
123126
} else if v != done {
124127
return false, errorFromDOMError(v)
125128
}

0 commit comments

Comments
 (0)