From eff7c9785ce2ae78231d5fdce51c32a4e76af2b4 Mon Sep 17 00:00:00 2001 From: madflow Date: Tue, 22 Aug 2023 08:59:08 +0200 Subject: [PATCH] add InputColor (#919) --- element.go | 24 ++++++++++++++++++++++++ element_test.go | 28 ++++++++++++++++++++++++++++ fixtures/input.html | 7 +++++++ lib/js/helper.go | 7 +++++++ lib/js/helper.js | 5 +++++ must.go | 9 ++++++++- 6 files changed, 79 insertions(+), 1 deletion(-) diff --git a/element.go b/element.go index 93a0e20f..c78de541 100644 --- a/element.go +++ b/element.go @@ -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()) diff --git a/element_test.go b/element_test.go index a570c8e4..23ce9fa5 100644 --- a/element_test.go +++ b/element_test.go @@ -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) diff --git a/fixtures/input.html b/fixtures/input.html index 6c08aaf2..2c7a048b 100644 --- a/fixtures/input.html +++ b/fixtures/input.html @@ -80,6 +80,13 @@
+ + +
+