mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
fix: android by selector
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -74,6 +73,9 @@ func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(code.AndroidDeviceConnectionError,
|
return nil, errors.Wrap(code.AndroidDeviceConnectionError,
|
||||||
fmt.Sprintf("get attached devices failed: %v", err))
|
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{
|
device = &AndroidDevice{
|
||||||
@@ -93,7 +95,7 @@ func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, er
|
|||||||
|
|
||||||
device.SerialNumber = dev.Serial()
|
device.SerialNumber = dev.Serial()
|
||||||
device.d = dev
|
device.d = dev
|
||||||
device.logcat = NewAdbLogcat(serialNumber)
|
device.logcat = NewAdbLogcat(device.SerialNumber)
|
||||||
return device, nil
|
return device, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -662,32 +664,3 @@ func (s UiSelectorHelper) FromParent(selector UiSelectorHelper) UiSelectorHelper
|
|||||||
s.value.WriteString(fmt.Sprintf(`.fromParent(%s)`, selector.value.String()))
|
s.value.WriteString(fmt.Sprintf(`.fromParent(%s)`, selector.value.String()))
|
||||||
return s
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -424,8 +424,8 @@ func (ud *uiaDriver) AppLaunch(bundleId string, launchOpt ...AppLaunchOption) (e
|
|||||||
var ce error
|
var ce error
|
||||||
exists := func(ud WebDriver) (bool, error) {
|
exists := func(ud WebDriver) (bool, error) {
|
||||||
for _, opt := range launchOpt {
|
for _, opt := range launchOpt {
|
||||||
if waitForComplete, ok := opt["androidBySelector"]; ok {
|
if bySelector, ok := opt["bySelector"]; ok {
|
||||||
for _, e := range waitForComplete.([]BySelector) {
|
for _, e := range bySelector.([]BySelector) {
|
||||||
_, ce = ud.FindElement(e)
|
_, ce = ud.FindElement(e)
|
||||||
if ce == nil {
|
if ce == nil {
|
||||||
return true, nil
|
return true, nil
|
||||||
@@ -436,7 +436,7 @@ func (ud *uiaDriver) AppLaunch(bundleId string, launchOpt ...AppLaunchOption) (e
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
if err = ud.WaitWithTimeoutAndInterval(exists, 45, 1); err != 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
|
return
|
||||||
|
|||||||
@@ -868,8 +868,7 @@ func TestDriver_AppLaunch(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// err = driver.AppLaunch("tv.danmaku.bili", BySelector{ResourceIdID: "tv.danmaku.bili:id/action_bar_root"})
|
err = driver.AppLaunch("com.android.settings")
|
||||||
err = driver.AppLaunch("com.android.settings", AppLaunchOption{}.WithAndroidBySelector(AndroidBySelector{ResourceIdID: "android:id/list"}))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ func convertToHTTPClient(conn net.Conn) *http.Client {
|
|||||||
if err := tcpConn.SetKeepAlive(true); err != nil {
|
if err := tcpConn.SetKeepAlive(true); err != nil {
|
||||||
log.Error().Err(err).Msg("set tcp keep alive failed")
|
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")
|
log.Error().Err(err).Msg("set tcp keep alive period failed")
|
||||||
}
|
}
|
||||||
return &http.Client{
|
return &http.Client{
|
||||||
|
|||||||
@@ -319,8 +319,8 @@ func (opt AppLaunchOption) WithEnvironment(env map[string]string) AppLaunchOptio
|
|||||||
return opt
|
return opt
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opt AppLaunchOption) WithAndroidBySelector(waitForComplete ...AndroidBySelector) AppLaunchOption {
|
func (opt AppLaunchOption) WithBySelector(bySelector ...BySelector) AppLaunchOption {
|
||||||
opt["androidBySelector"] = waitForComplete
|
opt["bySelector"] = bySelector
|
||||||
return opt
|
return opt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -174,8 +174,7 @@ func (r *HRPRunner) GenHTMLReport() *HRPRunner {
|
|||||||
|
|
||||||
// Run starts to execute one or multiple testcases.
|
// Run starts to execute one or multiple testcases.
|
||||||
func (r *HRPRunner) Run(testcases ...ITestCase) error {
|
func (r *HRPRunner) Run(testcases ...ITestCase) error {
|
||||||
log.Info().Str("hrp_version", version.VERSION).
|
log.Info().Str("hrp_version", version.VERSION).Msg("start running")
|
||||||
Interface("testcases", testcases).Msg("start running")
|
|
||||||
event := sdk.EventTracking{
|
event := sdk.EventTracking{
|
||||||
Category: "RunAPITests",
|
Category: "RunAPITests",
|
||||||
Action: "hrp run",
|
Action: "hrp run",
|
||||||
|
|||||||
Reference in New Issue
Block a user