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 isHidden #1110

Merged
merged 4 commits into from
Nov 23, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ func (f *Frame) IsHidden(selector string, opts goja.Value) (bool, error) {
}
hidden, err := f.isHidden(selector, popts)
if err != nil {
return false, fmt.Errorf("checking is %q hidden: %w", selector, err)
return false, err
}

return hidden, nil
Expand All @@ -1265,20 +1265,12 @@ func (f *Frame) isHidden(selector string, opts *FrameIsHiddenOptions) (bool, err
}
return v, err
}
act := f.newAction(
selector, DOMElementStateAttached, opts.Strict, isHidden, []string{}, false, true, opts.Timeout,
)
v, err := call(f.ctx, act, opts.Timeout)
v, err := f.runActionOnSelector(f.ctx, selector, opts.Strict, isHidden, func() bool { return true })
if err != nil {
return false, errorFromDOMError(err)
}

bv, ok := v.(bool)
if !ok {
return false, fmt.Errorf("checking is %q hidden: unexpected type %T", selector, v)
return false, fmt.Errorf("checking is %q hidden: %w", selector, err)
}

return bv, nil
return v, nil
}

// IsVisible returns true if the first element that matches the selector
Expand Down Expand Up @@ -1306,7 +1298,7 @@ func (f *Frame) isVisible(selector string, opts *FrameIsVisibleOptions) (bool, e
}
return v, err
}
v, err := f.runActionOnSelector(f.ctx, selector, opts.Strict, isVisible)
v, err := f.runActionOnSelector(f.ctx, selector, opts.Strict, isVisible, func() bool { return false })
if err != nil {
return false, fmt.Errorf("checking is %q visible: %w", selector, err)
}
Expand Down Expand Up @@ -1966,15 +1958,15 @@ type frameExecutionContext interface {
}

func (f *Frame) runActionOnSelector(
ctx context.Context, selector string, strict bool, fn elementHandleActionFunc,
ctx context.Context, selector string, strict bool, fn elementHandleActionFunc, nullResponder func() bool,
) (bool, error) {
handle, err := f.Query(selector, strict)
if err != nil {
return false, fmt.Errorf("query: %w", err)
}
if handle == nil {
f.log.Debugf("Frame:runActionOnSelector:nilHandler", "fid:%s furl:%q selector:%s", f.ID(), f.URL(), selector)
return false, nil
return nullResponder(), err
}

v, err := fn(ctx, handle)
Expand Down
7 changes: 0 additions & 7 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,6 @@ func TestLocatorElementState(t *testing.T) {
{
"IsDisabled", func(l *common.Locator, tb *testBrowser) { l.IsDisabled(timeout(tb)) },
},
{
"IsHidden", func(l *common.Locator, tb *testBrowser) {
if _, err := l.IsHidden(timeout(tb)); err != nil {
panic(err)
}
},
},
}
for _, tt := range sanityTests {
tt := tt
Expand Down
67 changes: 67 additions & 0 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,3 +1302,70 @@ func TestPageIsVisible(t *testing.T) {
})
}
}

func TestPageIsHidden(t *testing.T) {
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
t.Parallel()

testCases := []struct {
name string
selector string
options common.FrameIsVisibleOptions
want bool
wantErr string
}{
{
name: "hidden",
selector: "div[id=my-div-3]",
want: true,
},
{
name: "visible",
selector: "div[id=my-div]",
want: false,
},
{
name: "not_found",
selector: "div[id=does-not-exist]",
want: true,
},
{
name: "first_div",
selector: "div",
want: false,
},
{
name: "first_div",
selector: "div",
options: common.FrameIsVisibleOptions{
FrameBaseOptions: common.FrameBaseOptions{
Strict: true,
},
},
wantErr: "error:strictmodeviolation",
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

tb := newTestBrowser(t, withFileServer())

page := tb.NewPage(nil)

_, err := page.Goto(tb.staticURL("visible.html"), nil)
require.NoError(t, err)

got, err := page.IsHidden(tc.selector, tb.toGojaValue(tc.options))

if tc.wantErr != "" {
assert.ErrorContains(t, err, tc.wantErr)
return
}

assert.NoError(t, err)
assert.Equal(t, tc.want, got)
})
}
}
Loading