From 187bd71e29098ab5c3d4a24392e29f263f22d09a Mon Sep 17 00:00:00 2001 From: lingxiaozu <1430173805@qq.com> Date: Mon, 10 Oct 2022 17:16:56 +0800 Subject: [PATCH] fix: failed to send keys in android ui automation --- hrp/internal/uixt/android_driver.go | 20 ++++++++++++++- hrp/internal/uixt/ext.go | 40 ++++++++++++++++++++++++++--- hrp/step.go | 3 +++ 3 files changed, 58 insertions(+), 5 deletions(-) diff --git a/hrp/internal/uixt/android_driver.go b/hrp/internal/uixt/android_driver.go index 2c80a6a1..cb39fd78 100644 --- a/hrp/internal/uixt/android_driver.go +++ b/hrp/internal/uixt/android_driver.go @@ -684,13 +684,31 @@ func (ud *uiaDriver) SendKeys(text string, options ...DataOption) (err error) { } func (ud *uiaDriver) Input(text string, options ...DataOption) (err error) { - element, err := ud.FindElement(BySelector{ClassName: ElementType{EditText: true}}) + data := map[string]interface{}{ + "view": text, + } + // append options in post data for extra uiautomator configurations + for _, option := range options { + option(data) + } + + var element WebElement + if valuetext, ok := data["text"]; ok { + element, err = ud.FindElement(BySelector{UiAutomator: NewUiSelectorHelper().TextContains(fmt.Sprintf("%v", valuetext)).String()}) + } else if valueid, ok := data["id"]; ok { + element, err = ud.FindElement(BySelector{ResourceIdID: fmt.Sprintf("%v", valueid)}) + } else if valuedesc, ok := data["description"]; ok { + element, err = ud.FindElement(BySelector{UiAutomator: NewUiSelectorHelper().Description(fmt.Sprintf("%v", valuedesc)).String()}) + } else { + element, err = ud.FindElement(BySelector{ClassName: ElementType{EditText: true}}) + } if err != nil { return err } return element.SendKeys(text, options...) } + func (ud *uiaDriver) KeyboardDismiss(keyNames ...string) (err error) { // TODO return errDriverNotImplemented diff --git a/hrp/internal/uixt/ext.go b/hrp/internal/uixt/ext.go index bf7b6422..43f3328b 100644 --- a/hrp/internal/uixt/ext.go +++ b/hrp/internal/uixt/ext.go @@ -68,6 +68,9 @@ type MobileAction struct { Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element, should start from 1 Timeout int `json:"timeout,omitempty" yaml:"timeout,omitempty"` // TODO: wait timeout in seconds for mobile action IgnoreNotFoundError bool `json:"ignore_NotFoundError,omitempty" yaml:"ignore_NotFoundError,omitempty"` // ignore error if target element not found + Text string `json:"text,omitempty" yaml:"text,omitempty"` + ID string `json:"id,omitempty" yaml:"id,omitempty"` + Description string `json:"description,omitempty" yaml:"description,omitempty"` } type ActionOption func(o *MobileAction) @@ -84,6 +87,26 @@ func WithIndex(index int) ActionOption { } } +func WithText(text string) ActionOption { + return func(o *MobileAction) { + o.Text = text + } +} + +func WithID(id string) ActionOption { + return func(o *MobileAction) { + o.ID = id + } +} + +func WithDescription(description string) ActionOption { + return func(o *MobileAction) { + o.Description = description + } +} + + + func WithMaxRetryTimes(maxRetryTimes int) ActionOption { return func(o *MobileAction) { o.MaxRetryTimes = maxRetryTimes @@ -464,14 +487,23 @@ func (dExt *DriverExt) DoAction(action MobileAction) error { // append \n to send text with enter // send \b\b\b to delete 3 chars param := fmt.Sprintf("%v", action.Params) + options := []DataOption{} + if action.Text != "" { + options = append(options, WithCustomOption("text", action.Text)) + } + if action.ID != "" { + options = append(options, WithCustomOption("id", action.ID)) + } + if action.Description != "" { + options = append(options, WithCustomOption("description", action.Description)) + } if action.Identifier != "" { - option := WithCustomOption("log", map[string]interface{}{ + options = append(options,WithCustomOption("log", map[string]interface{}{ "enable": true, "data": action.Identifier, - }) - return dExt.Driver.Input(param, option) + })) } - return dExt.Driver.Input(param) + return dExt.Driver.Input(param, options...) case CtlSleep: if param, ok := action.Params.(json.Number); ok { seconds, _ := param.Float64() diff --git a/hrp/step.go b/hrp/step.go index 13b6da61..370f7ab4 100644 --- a/hrp/step.go +++ b/hrp/step.go @@ -22,6 +22,9 @@ var ( WithIndex = uixt.WithIndex WithTimeout = uixt.WithTimeout WithIgnoreNotFoundError = uixt.WithIgnoreNotFoundError + WithText = uixt.WithText + WithID = uixt.WithID + WithDescription = uixt.WithDescription ) type StepResult struct {