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 isVisible #1108

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 32 additions & 15 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -1283,19 +1283,19 @@ func (f *Frame) isHidden(selector string, opts *FrameIsHiddenOptions) (bool, err

// IsVisible returns true if the first element that matches the selector
// is visible. Otherwise, returns false.
func (f *Frame) IsVisible(selector string, opts goja.Value) bool {
func (f *Frame) IsVisible(selector string, opts goja.Value) (bool, error) {
f.log.Debugf("Frame:IsVisible", "fid:%s furl:%q sel:%q", f.ID(), f.URL(), selector)

popts := NewFrameIsVisibleOptions(f.defaultTimeout())
if err := popts.Parse(f.ctx, opts); err != nil {
k6ext.Panic(f.ctx, "parsing is visible options: %w", err)
return false, fmt.Errorf("parsing is visible options: %w", err)
}
visible, err := f.isVisible(selector, popts)
if err != nil {
k6ext.Panic(f.ctx, "checking is %q visible: %w", selector, err)
return false, err
}

return visible
return visible, nil
}

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

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

return bv, nil
return v, nil
}

// ID returns the frame id.
Expand Down Expand Up @@ -1973,6 +1965,31 @@ type frameExecutionContext interface {
ID() runtime.ExecutionContextID
}

func (f *Frame) runActionOnSelector(
ctx context.Context, selector string, strict bool, fn elementHandleActionFunc,
) (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
}

v, err := fn(ctx, handle)
if err != nil {
return false, fmt.Errorf("calling function: %w", err)
}

bv, ok := v.(bool)
if !ok {
return false, fmt.Errorf("unexpected type %T", v)
}

return bv, nil
}

//nolint:unparam
func (f *Frame) newAction(
selector string, state DOMElementState, strict bool, fn elementHandleActionFunc, states []string,
Expand Down
4 changes: 3 additions & 1 deletion common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,9 @@ func (p *Page) IsHidden(selector string, opts goja.Value) bool {
return p.MainFrame().IsHidden(selector, opts)
}

func (p *Page) IsVisible(selector string, opts goja.Value) bool {
// IsVisible will look for an element in the dom with given selector. It will
// not wait for a match to occur. If no elements match `false` will be returned.
func (p *Page) IsVisible(selector string, opts goja.Value) (bool, error) {
p.logger.Debugf("Page:IsVisible", "sid:%v selector:%s", p.sessionID(), selector)

return p.MainFrame().IsVisible(selector, opts)
Expand Down
3 changes: 0 additions & 3 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,6 @@ func TestLocatorElementState(t *testing.T) {
{
"IsDisabled", func(l *common.Locator, tb *testBrowser) { l.IsDisabled(timeout(tb)) },
},
{
"IsVisible", func(l *common.Locator, tb *testBrowser) { l.IsVisible(timeout(tb)) },
},
{
"IsHidden", func(l *common.Locator, tb *testBrowser) { l.IsHidden(timeout(tb)) },
},
Expand Down
67 changes: 67 additions & 0 deletions tests/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1235,3 +1235,70 @@ func performPingTest(t *testing.T, tb *testBrowser, page *common.Page, iteration

return ms / int64(iterations)
}

func TestPageIsVisible(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
selector string
options common.FrameIsVisibleOptions
want bool
wantErr string
}{
{
name: "visible",
selector: "div[id=my-div]",
want: true,
},
{
name: "not_visible",
selector: "div[id=my-div-3]",
want: false,
},
{
name: "not_found",
selector: "div[id=does-not-exist]",
want: false,
},
{
name: "first_div",
selector: "div",
want: true,
},
{
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.IsVisible(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)
})
}
}
8 changes: 8 additions & 0 deletions tests/static/visible.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html lang="en">
<head></head>
<body>
<div id='my-div'>My DIV</div>
<div id='my-div-2'>My DIV 2</div>
<div id='my-div-3' style='display: none;'>My DIV 3</div>
</body>
</html>