From aa35fbf1ada7619c6e8c9f48475b23128983cb66 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 25 Oct 2022 17:34:52 +0800 Subject: [PATCH] fix: android by selector --- hrp/pkg/uixt/android_device.go | 35 ++++------------------------------ hrp/pkg/uixt/android_driver.go | 6 +++--- hrp/pkg/uixt/android_test.go | 3 +-- hrp/pkg/uixt/client.go | 2 +- hrp/pkg/uixt/interface.go | 4 ++-- hrp/runner.go | 3 +-- 6 files changed, 12 insertions(+), 41 deletions(-) diff --git a/hrp/pkg/uixt/android_device.go b/hrp/pkg/uixt/android_device.go index d3545f2a..e2a30d93 100644 --- a/hrp/pkg/uixt/android_device.go +++ b/hrp/pkg/uixt/android_device.go @@ -6,7 +6,6 @@ import ( "fmt" "net" "os/exec" - "reflect" "strings" "github.com/pkg/errors" @@ -74,6 +73,9 @@ func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, er if err != nil { return nil, errors.Wrap(code.AndroidDeviceConnectionError, fmt.Sprintf("get attached devices failed: %v", err)) + } else if len(deviceList) == 0 { + return nil, errors.Wrap(code.AndroidDeviceConnectionError, + "not attached device found") } device = &AndroidDevice{ @@ -93,7 +95,7 @@ func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, er device.SerialNumber = dev.Serial() device.d = dev - device.logcat = NewAdbLogcat(serialNumber) + device.logcat = NewAdbLogcat(device.SerialNumber) return device, nil } @@ -662,32 +664,3 @@ func (s UiSelectorHelper) FromParent(selector UiSelectorHelper) UiSelectorHelper s.value.WriteString(fmt.Sprintf(`.fromParent(%s)`, selector.value.String())) return s } - -type AndroidBySelector struct { - // Set the search criteria to match the given resource ResourceIdID. - ResourceIdID string `json:"id"` - // Set the search criteria to match the content-description property for a widget. - ContentDescription string `json:"accessibility id"` - XPath string `json:"xpath"` - // Set the search criteria to match the class property for a widget (for example, "android.widget.Button"). - ClassName string `json:"class name"` - UiAutomator string `json:"-android uiautomator"` -} - -func (by AndroidBySelector) getMethodAndSelector() (method, selector string) { - vBy := reflect.ValueOf(by) - tBy := reflect.TypeOf(by) - for i := 0; i < vBy.NumField(); i++ { - vi := vBy.Field(i).Interface() - // switch vi := vi.(type) { - // case string: - // selector = vi - // } - selector = vi.(string) - if selector != "" && selector != "UNKNOWN" { - method = tBy.Field(i).Tag.Get("json") - return - } - } - return -} diff --git a/hrp/pkg/uixt/android_driver.go b/hrp/pkg/uixt/android_driver.go index 30138970..255e9757 100644 --- a/hrp/pkg/uixt/android_driver.go +++ b/hrp/pkg/uixt/android_driver.go @@ -424,8 +424,8 @@ func (ud *uiaDriver) AppLaunch(bundleId string, launchOpt ...AppLaunchOption) (e var ce error exists := func(ud WebDriver) (bool, error) { for _, opt := range launchOpt { - if waitForComplete, ok := opt["androidBySelector"]; ok { - for _, e := range waitForComplete.([]BySelector) { + if bySelector, ok := opt["bySelector"]; ok { + for _, e := range bySelector.([]BySelector) { _, ce = ud.FindElement(e) if ce == nil { return true, nil @@ -436,7 +436,7 @@ func (ud *uiaDriver) AppLaunch(bundleId string, launchOpt ...AppLaunchOption) (e return false, nil } if err = ud.WaitWithTimeoutAndInterval(exists, 45, 1); err != nil { - return fmt.Errorf("app launch (waitForComplete): %s: %w", err.Error(), ce) + return fmt.Errorf("app launch: %s: %w", err.Error(), ce) } } return diff --git a/hrp/pkg/uixt/android_test.go b/hrp/pkg/uixt/android_test.go index eecf7b91..03354830 100644 --- a/hrp/pkg/uixt/android_test.go +++ b/hrp/pkg/uixt/android_test.go @@ -868,8 +868,7 @@ func TestDriver_AppLaunch(t *testing.T) { t.Fatal(err) } - // err = driver.AppLaunch("tv.danmaku.bili", BySelector{ResourceIdID: "tv.danmaku.bili:id/action_bar_root"}) - err = driver.AppLaunch("com.android.settings", AppLaunchOption{}.WithAndroidBySelector(AndroidBySelector{ResourceIdID: "android:id/list"})) + err = driver.AppLaunch("com.android.settings") if err != nil { t.Fatal(err) } diff --git a/hrp/pkg/uixt/client.go b/hrp/pkg/uixt/client.go index cedbdff8..d2308b10 100644 --- a/hrp/pkg/uixt/client.go +++ b/hrp/pkg/uixt/client.go @@ -98,7 +98,7 @@ func convertToHTTPClient(conn net.Conn) *http.Client { if err := tcpConn.SetKeepAlive(true); err != nil { log.Error().Err(err).Msg("set tcp keep alive failed") } - if err := tcpConn.SetKeepAlivePeriod(5 * time.Second); err != nil { + if err := tcpConn.SetKeepAlivePeriod(30 * time.Second); err != nil { log.Error().Err(err).Msg("set tcp keep alive period failed") } return &http.Client{ diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index f805a2bd..c31612c6 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -319,8 +319,8 @@ func (opt AppLaunchOption) WithEnvironment(env map[string]string) AppLaunchOptio return opt } -func (opt AppLaunchOption) WithAndroidBySelector(waitForComplete ...AndroidBySelector) AppLaunchOption { - opt["androidBySelector"] = waitForComplete +func (opt AppLaunchOption) WithBySelector(bySelector ...BySelector) AppLaunchOption { + opt["bySelector"] = bySelector return opt } diff --git a/hrp/runner.go b/hrp/runner.go index 26aa4de4..e5a770b3 100644 --- a/hrp/runner.go +++ b/hrp/runner.go @@ -174,8 +174,7 @@ func (r *HRPRunner) GenHTMLReport() *HRPRunner { // Run starts to execute one or multiple testcases. func (r *HRPRunner) Run(testcases ...ITestCase) error { - log.Info().Str("hrp_version", version.VERSION). - Interface("testcases", testcases).Msg("start running") + log.Info().Str("hrp_version", version.VERSION).Msg("start running") event := sdk.EventTracking{ Category: "RunAPITests", Action: "hrp run",