fix: android by selector

This commit is contained in:
debugtalk
2022-10-25 17:34:52 +08:00
parent 7f4c3ae248
commit aa966123bc
6 changed files with 12 additions and 41 deletions

View File

@@ -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
}

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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{

View File

@@ -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
}

View File

@@ -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",