Skip to content

Commit

Permalink
Refactor Locator.Fill to return err
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 13, 2024
1 parent e0eea1d commit c610031
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ type locatorAPI interface {
IsDisabled(opts goja.Value) (bool, error)
IsVisible(opts goja.Value) (bool, error)
IsHidden(opts goja.Value) (bool, error)
Fill(value string, opts goja.Value)
Fill(value string, opts goja.Value) error
Focus(opts goja.Value)
GetAttribute(name string, opts goja.Value) goja.Value
InnerHTML(opts goja.Value) string
Expand Down
19 changes: 9 additions & 10 deletions common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,24 +276,23 @@ func (l *Locator) IsHidden() (bool, error) {
}

// Fill out the element using locator's selector with strict mode on.
func (l *Locator) Fill(value string, opts goja.Value) {
func (l *Locator) Fill(value string, opts goja.Value) error {
l.log.Debugf(
"Locator:Fill", "fid:%s furl:%q sel:%q val:%q opts:%+v",
l.frame.ID(), l.frame.URL(), l.selector, value, opts,
)

var err error
defer func() { panicOrSlowMo(l.ctx, err) }()

copts := NewFrameFillOptions(l.frame.defaultTimeout())
if err = copts.Parse(l.ctx, opts); err != nil {
err = fmt.Errorf("parsing fill options: %w", err)
return
if err := copts.Parse(l.ctx, opts); err != nil {
return fmt.Errorf("parsing fill options: %w", err)
}
if err = l.fill(value, copts); err != nil {
err = fmt.Errorf("filling %q with %q: %w", l.selector, value, err)
return
if err := l.fill(value, copts); err != nil {
return fmt.Errorf("filling %q with %q: %w", l.selector, value, err)
}

applySlowMo(l.ctx)

return nil
}

func (l *Locator) fill(value string, opts *FrameFillOptions) error {
Expand Down
30 changes: 19 additions & 11 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ func TestLocator(t *testing.T) {
},
},
{
"Clear", func(tb *testBrowser, p *common.Page) {
"Clear", func(_ *testBrowser, p *common.Page) {
const value = "fill me up"
l := p.Locator("#inputText", nil)

l.Fill(value, nil)
require.NoError(t, l.Fill(value, nil))
require.Equal(t, value, p.InputValue("#inputText", nil))

err := l.Clear(common.NewFrameFillOptions(l.Timeout()))
Expand Down Expand Up @@ -104,26 +104,29 @@ func TestLocator(t *testing.T) {
},
},
{
"Fill", func(tb *testBrowser, p *common.Page) {
"Fill", func(_ *testBrowser, p *common.Page) {
const value = "fill me up"
p.Locator("#inputText", nil).Fill(value, nil)
lo := p.Locator("#inputText", nil)
require.NoError(t, lo.Fill(value, nil))
require.Equal(t, value, p.InputValue("#inputText", nil))
},
},
{
"FillTextarea", func(tb *testBrowser, p *common.Page) {
"FillTextarea", func(_ *testBrowser, p *common.Page) {
const value = "fill me up"
p.Locator("textarea", nil).Fill(value, nil)
lo := p.Locator("textarea", nil)
require.NoError(t, lo.Fill(value, nil))
require.Equal(t, value, p.InputValue("textarea", nil))
},
},
{
"FillParagraph", func(tb *testBrowser, p *common.Page) {
"FillParagraph", func(_ *testBrowser, p *common.Page) {
const value = "fill me up"
p.Locator("#firstParagraph", nil).Fill(value, nil)
lo := p.Locator("#firstParagraph", nil)
require.NoError(t, lo.Fill(value, nil))
require.Equal(t, value, p.TextContent("#firstParagraph", nil))
l := p.Locator("#secondParagraph", nil)
assert.Panics(t, func() { l.Fill(value, nil) }, "should panic")
lo = p.Locator("#secondParagraph", nil)
require.Error(t, lo.Fill(value, nil))
},
},
{
Expand Down Expand Up @@ -327,7 +330,12 @@ func TestLocator(t *testing.T) {
"Focus", func(l *common.Locator, tb *testBrowser) { l.Focus(timeout(tb)) },
},
{
"Fill", func(l *common.Locator, tb *testBrowser) { l.Fill("fill me up", timeout(tb)) },
"Fill", func(l *common.Locator, tb *testBrowser) {
if err := l.Fill("fill me up", timeout(tb)); err != nil {
// TODO: remove panic and update tests when all locator methods return error.
panic(err)
}
},
},
{
"GetAttribute", func(l *common.Locator, tb *testBrowser) { l.GetAttribute("value", timeout(tb)) },
Expand Down

0 comments on commit c610031

Please sign in to comment.