Skip to content

Commit

Permalink
add InputColor (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
madflow authored Aug 22, 2023
1 parent bb8cf4a commit eff7c97
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
24 changes: 24 additions & 0 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,30 @@ func (el *Element) InputTime(t time.Time) error {
return err
}

// InputColor focuses on the element and inputs a color string to it.
// Before the action, it will scroll to the element, wait until it's visible, enabled and writable.
func (el *Element) InputColor(color string) error {
err := el.Focus()
if err != nil {
return err
}

err = el.WaitEnabled()
if err != nil {
return err
}

err = el.WaitWritable()
if err != nil {
return err
}

defer el.tryTrace(TraceTypeInput, "input "+color)()

_, err = el.Evaluate(evalHelper(js.InputColor, color))
return err
}

// Blur removes focus from the element.
func (el *Element) Blur() error {
_, err := el.Evaluate(Eval("() => this.blur()").ByUser())
Expand Down
28 changes: 28 additions & 0 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,34 @@ func TestInputTime(t *testing.T) {
})
}

func TestInputColor(t *testing.T) {
g := setup(t)

p := g.page.MustNavigate(g.srcFile("fixtures/input.html"))

var el *rod.Element
{
el = p.MustElement("[type=color]")
el.MustInputColor("#ff6f00")

g.Eq(el.MustText(), "#ff6f00")
g.True(p.MustHas("[event=input-color-change]"))
}

g.Panic(func() {
g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})
el.MustInputColor("#ff6f00")
})
g.Panic(func() {
g.mc.stubErr(5, proto.RuntimeCallFunctionOn{})
el.MustInputColor("#ff6f00")
})
g.Panic(func() {
g.mc.stubErr(6, proto.RuntimeCallFunctionOn{})
el.MustInputColor("#ff6f00")
})
}

func TestElementInputDate(t *testing.T) {
g := setup(t)

Expand Down
7 changes: 7 additions & 0 deletions fixtures/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@

<hr />

<input
type="color"
onchange="this.setAttribute('event', 'input-color-change')"
/>

<hr />

<button type="button" id="EnabledButton">Enabled Button</button>

<button type="button" id="DisabledButton" disabled>
Expand Down
7 changes: 7 additions & 0 deletions lib/js/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ var InputTime = &Function{
Dependencies: []*Function{InputEvent},
}

// InputColor ...
var InputColor = &Function{
Name: "inputColor",
Definition: `function(e){this.value=""+e,functions.inputEvent.call(this)}`,
Dependencies: []*Function{InputEvent},
}

// SelectText ...
var SelectText = &Function{
Name: "selectText",
Expand Down
5 changes: 5 additions & 0 deletions lib/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ const functions = {
functions.inputEvent.call(this)
},

inputColor(color) {
this.value = `${color}`

functions.inputEvent.call(this)
},
selectText(pattern) {
const m = this.value.match(new RegExp(pattern))
if (m) {
Expand Down
9 changes: 8 additions & 1 deletion must.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (
"strings"
"time"

"github.com/ysmood/gson"

"github.com/go-rod/rod/lib/devices"
"github.com/go-rod/rod/lib/input"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/gson"
)

// It must be generated by genE.
Expand Down Expand Up @@ -826,6 +827,12 @@ func (el *Element) MustInputTime(t time.Time) *Element {
return el
}

// MustInputColor is similar to [Element.InputColor].
func (el *Element) MustInputColor(color string) *Element {
el.e(el.InputColor(color))
return el
}

// MustBlur is similar to [Element.Blur].
func (el *Element) MustBlur() *Element {
el.e(el.Blur())
Expand Down

0 comments on commit eff7c97

Please sign in to comment.