Skip to content

Commit

Permalink
support UserGesture for Eval
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Sep 24, 2020
1 parent 6da761b commit dff1505
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
12 changes: 6 additions & 6 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (el *Element) Focus() error {
return err
}

_, err = el.Eval(`this.focus()`)
_, err = el.EvalWithOptions(NewEvalOptions(`this.focus()`, nil).ByUser())
return err
}

Expand Down Expand Up @@ -198,7 +198,7 @@ func (el *Element) SelectText(regex string) error {
defer el.tryTraceInput("select text: " + regex)()
el.page.browser.trySlowmotion()

_, err = el.EvalWithOptions(jsHelper(js.SelectText, JSArgs{regex}))
_, err = el.EvalWithOptions(jsHelper(js.SelectText, JSArgs{regex}).ByUser())
return err
}

Expand All @@ -212,7 +212,7 @@ func (el *Element) SelectAllText() error {
defer el.tryTraceInput("select all text")()
el.page.browser.trySlowmotion()

_, err = el.EvalWithOptions(jsHelper(js.SelectAllText, nil))
_, err = el.EvalWithOptions(jsHelper(js.SelectAllText, nil).ByUser())
return err
}

Expand All @@ -235,13 +235,13 @@ func (el *Element) Input(text string) error {
return err
}

_, err = el.EvalWithOptions(jsHelper(js.InputEvent, nil))
_, err = el.EvalWithOptions(jsHelper(js.InputEvent, nil).ByUser())
return err
}

// Blur is similar to the method Blur
func (el *Element) Blur() error {
_, err := el.Eval("this.blur()")
_, err := el.EvalWithOptions(NewEvalOptions("this.blur()", nil).ByUser())
return err
}

Expand All @@ -255,7 +255,7 @@ func (el *Element) Select(selectors []string) error {
defer el.tryTraceInput(fmt.Sprintf(`select "%s"`, strings.Join(selectors, "; ")))()
el.page.browser.trySlowmotion()

_, err = el.EvalWithOptions(jsHelper(js.Select, JSArgs{selectors}))
_, err = el.EvalWithOptions(jsHelper(js.Select, JSArgs{selectors}).ByUser())
return err
}

Expand Down
1 change: 1 addition & 0 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ func (p *Page) EvalWithOptions(opts *EvalOptions) (*proto.RuntimeRemoteObject, e
ObjectID: objectID,
AwaitPromise: true,
ReturnByValue: opts.ByValue,
UserGesture: opts.UserGesture,
FunctionDeclaration: SprintFnThis(opts.JS),
Arguments: args,
}.Call(p)
Expand Down
13 changes: 11 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ type EvalOptions struct {

// JSArgs of the js function
JSArgs JSArgs

// Whether execution should be treated as initiated by user in the UI.
UserGesture bool
}

// This set the ThisID
Expand All @@ -79,9 +82,15 @@ func (e *EvalOptions) ByObject() *EvalOptions {
return e
}

// NewEvalOptions creates a new EvalPayload
// ByUser enables UserGesture.
func (e *EvalOptions) ByUser() *EvalOptions {
e.UserGesture = true
return e
}

// NewEvalOptions instance. ByValue will be set to true.
func NewEvalOptions(js string, args JSArgs) *EvalOptions {
return &EvalOptions{true, "", js, args}
return &EvalOptions{true, "", js, args, false}
}

const jsHelperID = proto.RuntimeRemoteObjectID("rodJSHelper")
Expand Down

0 comments on commit dff1505

Please sign in to comment.