Skip to content

Commit

Permalink
optimize the trace
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 22, 2020
1 parent c3af673 commit f9d2a20
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
3 changes: 3 additions & 0 deletions browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,10 @@ func (s *S) TestTrace() {
p := s.page.MustNavigate(srcFile("fixtures/click.html"))
el := p.MustElement("button")
el.MustClick()

s.Equal(rod.TraceTypeInput, msg.Type)
s.Equal("left click", msg.Details)
s.Equal(`[input] "left click"`, msg.String())

s.stubErr(1, proto.RuntimeCallFunctionOn{})
_ = p.Mouse.Move(10, 10, 1)
Expand Down
21 changes: 12 additions & 9 deletions dev_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ const (
// TraceTypeWaitRequests type
TraceTypeWaitRequests TraceType = "wait requests"

// TraceTypeJS type
TraceTypeJS TraceType = "js"
// TraceTypeEval type
TraceTypeEval TraceType = "eval"

// TraceTypeAction type
TraceTypeAction TraceType = "act"

// TraceTypeInput type
TraceTypeInput TraceType = "input"
)

// TraceMsg for logger
Expand All @@ -96,7 +99,7 @@ type TraceMsg struct {
}

func (msg *TraceMsg) String() string {
return utils.MustToJSON(msg)
return fmt.Sprintf("[%s] %v", msg.Type, utils.MustToJSON(msg.Details))
}

// TraceLog handler
Expand Down Expand Up @@ -155,21 +158,21 @@ func (b *Browser) trySlowmotion() {
time.Sleep(b.slowmotion)
}

func (el *Element) traceAction(details string) func() {
func (el *Element) tryTraceInput(details string) func() {
if !el.page.browser.trace {
return func() {}
}

msg := &TraceMsg{TraceTypeAction, details}
msg := &TraceMsg{TraceTypeInput, details}

el.page.browser.traceLog(msg)

return el.Trace(msg.String())
return el.Trace(details)
}

var regHelperJS = regexp.MustCompile(`\A\(rod, \.\.\.args\) => (rod\..+)\.apply\(this, `)

func (p *Page) tryTraceFn(js string, params JSArgs) func() {
func (p *Page) tryTraceEval(js string, params JSArgs) func() {
if !p.browser.trace {
return func() {}
}
Expand All @@ -182,7 +185,7 @@ func (p *Page) tryTraceFn(js string, params JSArgs) func() {
paramsStr := strings.Trim(mustToJSONForDev(params), "[]\r\n")

p.browser.traceLog(&TraceMsg{
TraceTypeJS,
TraceTypeEval,
map[string]interface{}{
"js": js,
"params": params,
Expand All @@ -193,7 +196,7 @@ func (p *Page) tryTraceFn(js string, params JSArgs) func() {
return p.Overlay(0, 0, 500, 0, msg)
}

func (p *Page) traceReq(ctx context.Context, reqList *sync.Map, includes, excludes []string) {
func (p *Page) tryTraceReq(ctx context.Context, reqList *sync.Map, includes, excludes []string) {
if !p.browser.trace {
return
}
Expand Down
21 changes: 10 additions & 11 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/tidwall/gjson"

"github.com/go-rod/rod/lib/assets/js"
"github.com/go-rod/rod/lib/input"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
)
Expand Down Expand Up @@ -39,7 +40,7 @@ func (el *Element) Focus() error {

// ScrollIntoView doc is similar to the method MustScrollIntoViewIfNeeded
func (el *Element) ScrollIntoView() error {
defer el.traceAction("scroll into view")()
defer el.tryTraceInput("scroll into view")()
el.page.browser.trySlowmotion()

return proto.DOMScrollIntoViewIfNeeded{ObjectID: el.ObjectID}.Call(el)
Expand Down Expand Up @@ -82,7 +83,7 @@ func (el *Element) Click(button proto.InputMouseButton) error {
return err
}

defer el.traceAction(string(button) + " click")()
defer el.tryTraceInput(string(button) + " click")()

return el.page.Mouse.Click(button)
}
Expand All @@ -109,7 +110,7 @@ func (el *Element) Tap() error {
return err
}

defer el.traceAction("tap")()
defer el.tryTraceInput("tap")()

return el.page.Touch.Tap(box.CenterX(), box.CenterY())
}
Expand Down Expand Up @@ -182,7 +183,7 @@ func (el *Element) Press(key rune) error {
return err
}

defer el.traceAction("press " + string(key))()
defer el.tryTraceInput("press " + input.Keys[key].Key)()

return el.page.Keyboard.Press(key)
}
Expand All @@ -194,7 +195,7 @@ func (el *Element) SelectText(regex string) error {
return err
}

defer el.traceAction("select text: " + regex)()
defer el.tryTraceInput("select text: " + regex)()
el.page.browser.trySlowmotion()

_, err = el.EvalWithOptions(jsHelper(js.SelectText, JSArgs{regex}))
Expand All @@ -208,7 +209,7 @@ func (el *Element) SelectAllText() error {
return err
}

defer el.traceAction("select all text")()
defer el.tryTraceInput("select all text")()
el.page.browser.trySlowmotion()

_, err = el.EvalWithOptions(jsHelper(js.SelectAllText, nil))
Expand All @@ -227,7 +228,7 @@ func (el *Element) Input(text string) error {
return err
}

defer el.traceAction("input " + text)()
defer el.tryTraceInput("input " + text)()

err = el.page.Keyboard.InsertText(text)
if err != nil {
Expand All @@ -251,9 +252,7 @@ func (el *Element) Select(selectors []string) error {
return err
}

defer el.traceAction(fmt.Sprintf(
`select "%s"`,
strings.Join(selectors, "; ")))()
defer el.tryTraceInput(fmt.Sprintf(`select "%s"`, strings.Join(selectors, "; ")))()
el.page.browser.trySlowmotion()

_, err = el.EvalWithOptions(jsHelper(js.Select, JSArgs{selectors}))
Expand Down Expand Up @@ -302,7 +301,7 @@ func (el *Element) SetFiles(paths []string) error {
absPaths = append(absPaths, absPath)
}

defer el.traceAction(fmt.Sprintf("set files: %v", absPaths))
defer el.tryTraceInput(fmt.Sprintf("set files: %v", absPaths))()
el.page.browser.trySlowmotion()

err := proto.DOMSetFileInputFiles{
Expand Down
12 changes: 8 additions & 4 deletions lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func SDump(v interface{}) string {
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
E(enc.Encode(v))
return strings.TrimRight(string(buf.Bytes()), "\n")
data := buf.Bytes()
return string(data[:len(data)-1])
}

// Dump values to logger
Expand Down Expand Up @@ -251,9 +252,12 @@ func SyncMapToMap(s *sync.Map) map[string]interface{} {

// MustToJSONBytes encode data to json bytes
func MustToJSONBytes(data interface{}) []byte {
bytes, err := json.Marshal(data)
E(err)
return bytes
buf := bytes.NewBuffer(nil)
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
E(enc.Encode(data))
b := buf.Bytes()
return b[:len(b)-1]
}

// MustToJSON encode data to json string
Expand Down
5 changes: 2 additions & 3 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -406,7 +405,7 @@ func (p *Page) WaitRequestIdle(d time.Duration, includes, excludes []string) fun
})

return func() {
p.traceReq(ctx, reqList, includes, excludes)
p.tryTraceReq(ctx, reqList, includes, excludes)
timeout = time.NewTimer(d)

go func() {
Expand Down Expand Up @@ -567,7 +566,7 @@ func (p *Page) Wait(thisID proto.RuntimeRemoteObjectID, js string, params JSArgs
defer removeTrace()

return utils.Retry(p.ctx, p.sleeper(), func() (bool, error) {
remove := p.tryTraceFn(fmt.Sprintf("wait(%s)", js), params)
remove := p.tryTraceEval(js, params)
removeTrace()
removeTrace = remove

Expand Down
2 changes: 1 addition & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (p *Page) ElementByJS(opts *EvalOptions) (*Element, error) {

removeTrace := func() {}
err = utils.Retry(p.ctx, sleeper, func() (bool, error) {
remove := p.tryTraceFn(opts.JS, opts.JSArgs)
remove := p.tryTraceEval(opts.JS, opts.JSArgs)
removeTrace()
removeTrace = remove

Expand Down

0 comments on commit f9d2a20

Please sign in to comment.