Skip to content

Commit

Permalink
Add Locator.SetChecked
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed Aug 14, 2024
1 parent 2223845 commit 9835197
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
26 changes: 26 additions & 0 deletions common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,32 @@ func (l *Locator) dblclick(opts *FrameDblclickOptions) error {
return l.frame.dblclick(l.selector, opts)
}

// SetChecked sets the checked state of the element using locator's selector
// with strict mode on.
func (l *Locator) SetChecked(checked bool, opts sobek.Value) error {
l.log.Debugf(
"Locator:SetChecked", "fid:%s furl:%q sel:%q checked:%v opts:%+v",
l.frame.ID(), l.frame.URL(), l.selector, checked, opts,
)

copts := NewFrameCheckOptions(l.frame.defaultTimeout())
if err := copts.Parse(l.ctx, opts); err != nil {
return fmt.Errorf("parsing set checked options: %w", err)
}
if err := l.setChecked(checked, copts); err != nil {
return fmt.Errorf("setting %q checked to %v: %w", l.selector, checked, err)
}

applySlowMo(l.ctx)

return nil
}

func (l *Locator) setChecked(checked bool, opts *FrameCheckOptions) error {
opts.Strict = true
return l.frame.setChecked(l.selector, checked, opts)
}

// Check on an element using locator's selector with strict mode on.
func (l *Locator) Check(opts sobek.Value) error {
l.log.Debugf("Locator:Check", "fid:%s furl:%q sel:%q opts:%+v", l.frame.ID(), l.frame.URL(), l.selector, opts)
Expand Down
23 changes: 18 additions & 5 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,27 @@ func TestLocator(t *testing.T) {
}{
{
"Check", func(_ *testBrowser, p *common.Page) {
check := func() bool {
v, err := p.Evaluate(`() => window.check`)
require.NoError(t, err)
return asBool(t, v)
}
t.Run("check", func(t *testing.T) {
check := func() bool {
v, err := p.Evaluate(`() => window.check`)
require.NoError(t, err)
return asBool(t, v)
}
l := p.Locator("#inputCheckbox", nil)
require.False(t, check(), "should be unchecked first")
require.NoError(t, l.Check(nil))
require.True(t, check(), "cannot not check the input box")
require.NoError(t, l.Uncheck(nil))
require.False(t, check(), "cannot not uncheck the input box")
})
t.Run("setChecked", func(t *testing.T) {
l := p.Locator("#inputCheckbox", nil)
require.False(t, check(), "should be unchecked first")
require.NoError(t, l.SetChecked(true, nil))
require.True(t, check(), "cannot not check the input box")
require.NoError(t, l.SetChecked(false, nil))
require.False(t, check(), "cannot not uncheck the input box")
})
t.Run("is_checked", func(t *testing.T) {
l := p.Locator("#inputCheckbox", nil)
require.NoError(t, l.Check(nil))
Expand Down Expand Up @@ -401,6 +409,11 @@ func TestLocator(t *testing.T) {
return l.Press("a", timeout(tb))
},
},
{
"SetChecked", func(l *common.Locator, tb *testBrowser) error {
return l.SetChecked(true, timeout(tb))
},
},
{
"SelectOption", func(l *common.Locator, tb *testBrowser) error {
_, err := l.SelectOption(tb.toSobekValue(""), timeout(tb))
Expand Down

0 comments on commit 9835197

Please sign in to comment.