Skip to content

Commit

Permalink
Refactor page.type to return err
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 28, 2024
1 parent 007d52a commit 461a16d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ type pageAPI interface {
ThrottleCPU(common.CPUProfile) error
ThrottleNetwork(common.NetworkProfile) error
Title() (string, error)
Type(selector string, text string, opts goja.Value)
Type(selector string, text string, opts goja.Value) error
Uncheck(selector string, opts goja.Value) error
URL() (string, error)
ViewportSize() map[string]float64
Expand Down Expand Up @@ -408,7 +408,7 @@ type frameAPI interface {
Tap(selector string, opts goja.Value) error
TextContent(selector string, opts goja.Value) (string, error)
Title() string
Type(selector string, text string, opts goja.Value)
Type(selector string, text string, opts goja.Value) error
Uncheck(selector string, opts goja.Value) error
URL() string
WaitForFunction(pageFunc, opts goja.Value, args ...goja.Value) (any, error)
Expand Down
8 changes: 5 additions & 3 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -1676,18 +1676,20 @@ func (f *Frame) Title() string {
}

// Type text on the first element found matches the selector.
func (f *Frame) Type(selector, text string, opts goja.Value) {
func (f *Frame) Type(selector, text string, opts goja.Value) error {
f.log.Debugf("Frame:Type", "fid:%s furl:%q sel:%q text:%q", f.ID(), f.URL(), selector, text)

popts := NewFrameTypeOptions(f.defaultTimeout())
if err := popts.Parse(f.ctx, opts); err != nil {
k6ext.Panic(f.ctx, "parsing type options: %w", err)
return fmt.Errorf("parsing type options: %w", err)
}
if err := f.typ(selector, text, popts); err != nil {
k6ext.Panic(f.ctx, "typing %q in %q: %w", text, selector, err)
return fmt.Errorf("typing %q in %q: %w", text, selector, err)
}

applySlowMo(f.ctx)

return nil
}

func (f *Frame) typ(selector, text string, opts *FrameTypeOptions) error {
Expand Down
5 changes: 3 additions & 2 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,10 +1263,11 @@ func (p *Page) ThrottleNetwork(networkProfile NetworkProfile) error {
return nil
}

func (p *Page) Type(selector string, text string, opts goja.Value) {
// Type text on the first element found matches the selector.
func (p *Page) Type(selector string, text string, opts goja.Value) error {
p.logger.Debugf("Page:Type", "sid:%v selector:%s text:%s", p.sessionID(), selector, text)

p.MainFrame().Type(selector, text, opts)
return p.MainFrame().Type(selector, text, opts)
}

// URL returns the location of the page.
Expand Down
6 changes: 4 additions & 2 deletions tests/launch_options_slowmo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ func TestBrowserOptionsSlowMo(t *testing.T) {
t.Parallel()
tb := newTestBrowser(t, withFileServer())
testPageSlowMoImpl(t, tb, func(_ *testBrowser, p *common.Page) {
p.Type(".fill", "a", nil)
err := p.Type(".fill", "a", nil)
require.NoError(t, err)
})
})
t.Run("uncheck", func(t *testing.T) {
Expand Down Expand Up @@ -287,7 +288,8 @@ func TestBrowserOptionsSlowMo(t *testing.T) {
t.Parallel()
tb := newTestBrowser(t, withFileServer())
testFrameSlowMoImpl(t, tb, func(_ *testBrowser, f *common.Frame) {
f.Type(".fill", "a", nil)
err := f.Type(".fill", "a", nil)
require.NoError(t, err)
})
})
t.Run("uncheck", func(t *testing.T) {
Expand Down

0 comments on commit 461a16d

Please sign in to comment.