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

Refactor isHidden to return an error #1109

Merged
merged 2 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
8 changes: 4 additions & 4 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,19 +1242,19 @@ func (f *Frame) isDisabled(selector string, opts *FrameIsDisabledOptions) (bool,

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

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

return hidden
return hidden, nil
}

func (f *Frame) isHidden(selector string, opts *FrameIsHiddenOptions) (bool, error) {
Expand Down
8 changes: 4 additions & 4 deletions common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,19 @@ func (l *Locator) isVisible(opts *FrameIsVisibleOptions) (bool, error) {

// IsHidden returns true if the element matches the locator's
// selector and is hidden. Otherwise, returns false.
func (l *Locator) IsHidden(opts goja.Value) bool {
func (l *Locator) IsHidden(opts goja.Value) (bool, error) {
l.log.Debugf("Locator:IsHidden", "fid:%s furl:%q sel:%q opts:%+v", l.frame.ID(), l.frame.URL(), l.selector, opts)

copts := NewFrameIsHiddenOptions(l.frame.defaultTimeout())
if err := copts.Parse(l.ctx, opts); err != nil {
k6ext.Panic(l.ctx, "parsing is hidden options: %w", err)
return false, fmt.Errorf("parsing is hidden options: %w", err)
}
hidden, err := l.isHidden(copts)
if err != nil {
k6ext.Panic(l.ctx, "checking is %q hidden: %w", l.selector, err)
return false, fmt.Errorf("checking is %q hidden: %w", l.selector, err)
}

return hidden
return hidden, nil
}

// isHidden is like IsHidden but takes parsed options and does not
Expand Down
5 changes: 4 additions & 1 deletion common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,10 @@ func (p *Page) IsEnabled(selector string, opts goja.Value) bool {
return p.MainFrame().IsEnabled(selector, opts)
}

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

return p.MainFrame().IsHidden(selector, opts)
Expand Down
8 changes: 6 additions & 2 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func TestLocatorElementState(t *testing.T) {
{
"hidden",
`() => document.getElementById('inputText').style.visibility = 'hidden'`,
func(l *common.Locator) bool { return !l.IsHidden(nil) },
func(l *common.Locator) bool { resp, _ := l.IsHidden(nil); return !resp },
},
{
"readOnly",
Expand Down Expand Up @@ -398,7 +398,11 @@ func TestLocatorElementState(t *testing.T) {
"IsDisabled", func(l *common.Locator, tb *testBrowser) { l.IsDisabled(timeout(tb)) },
},
{
"IsHidden", func(l *common.Locator, tb *testBrowser) { l.IsHidden(timeout(tb)) },
"IsHidden", func(l *common.Locator, tb *testBrowser) {
if _, err := l.IsHidden(timeout(tb)); err != nil {
panic(err)
inancgumus marked this conversation as resolved.
Show resolved Hide resolved
}
},
},
}
for _, tt := range sanityTests {
Expand Down
Loading