feat: add uixt tool press_button

This commit is contained in:
lilong.129
2025-05-21 17:25:17 +08:00
parent 5c68760cca
commit 7724cf0062
9 changed files with 79 additions and 6 deletions

View File

@@ -936,6 +936,23 @@ func (ad *ADBDriver) OpenUrl(url string) (err error) {
return
}
var androidButtonMap = map[types.DeviceButton]string{
types.DeviceButtonBack: "KEYCODE_BACK",
types.DeviceButtonHome: "KEYCODE_HOME",
types.DeviceButtonEnter: "KEYCODE_ENTER",
types.DeviceButtonVolumeUp: "KEYCODE_VOLUME_UP",
types.DeviceButtonVolumeDown: "KEYCODE_VOLUME_DOWN",
}
func (ad *ADBDriver) PressButton(button types.DeviceButton) error {
buttonName, ok := androidButtonMap[button]
if !ok {
return fmt.Errorf("unsupported button: %s", button)
}
_, err := ad.runShellCommand("input", "keyevent", buttonName)
return err
}
func (ad *ADBDriver) PushImage(localPath string) error {
log.Info().Str("localPath", localPath).Msg("ADBDriver.PushImage")
remoteDir := "/sdcard/DCIM/Camera/"

View File

@@ -610,7 +610,7 @@ func (wd *BrowserDriver) PressBack(options ...option.ActionOption) error {
return err
}
func (wd *BrowserDriver) PressKeyCode(keyCode KeyCode) (err error) {
func (wd *BrowserDriver) PressButton(button types.DeviceButton) error {
return errors.New("not support")
}

View File

@@ -50,6 +50,8 @@ type IDriver interface {
Home() error
Unlock() error
Back() error
PressButton(button types.DeviceButton) error
// hover
HoverBySelector(selector string, opts ...option.ActionOption) error
// tap

View File

@@ -231,6 +231,22 @@ func (hd *HDCDriver) PressHarmonyKeyCode(keyCode ghdc.KeyCode) (err error) {
return hd.uiDriver.PressKey(keyCode)
}
var harmonyButtonMap = map[types.DeviceButton]ghdc.KeyCode{
types.DeviceButtonBack: ghdc.KEYCODE_BACK,
types.DeviceButtonHome: ghdc.KEYCODE_HOME,
types.DeviceButtonEnter: ghdc.KEYCODE_ENTER,
types.DeviceButtonVolumeUp: ghdc.KEYCODE_VOLUME_UP,
types.DeviceButtonVolumeDown: ghdc.KEYCODE_VOLUME_DOWN,
}
func (hd *HDCDriver) PressButton(button types.DeviceButton) (err error) {
keyCode, ok := harmonyButtonMap[button]
if !ok {
return fmt.Errorf("unsupported button: %s", button)
}
return hd.uiDriver.PressKey(keyCode)
}
func (hd *HDCDriver) ScreenShot(opts ...option.ActionOption) (*bytes.Buffer, error) {
tempDir := os.TempDir()
screenshotPath := fmt.Sprintf("%s/screenshot_%d.png", tempDir, time.Now().Unix())

View File

@@ -744,9 +744,14 @@ func (wd *WDADriver) Back() (err error) {
return wd.Swipe(0, 0.5, 0.6, 0.5)
}
func (wd *WDADriver) PressButton(devBtn types.DeviceButton) (err error) {
func (wd *WDADriver) PressButton(button types.DeviceButton) (err error) {
// [[FBRoute POST:@"/wda/pressButton"] respondWithTarget:self action:@selector(handlePressButtonCommand:)]
data := map[string]interface{}{"name": devBtn}
if button == types.DeviceButtonEnter {
return wd.Input("\n")
}
data := map[string]interface{}{"name": button}
urlStr := fmt.Sprintf("/session/%s/wda/pressButton", wd.Session.ID)
_, err = wd.Session.POST(data, urlStr)
return

View File

@@ -174,13 +174,15 @@ func (bs BatteryStatus) String() string {
}
}
// DeviceButton A physical button on an iOS device.
// DeviceButton A physical button on a device.
type DeviceButton string
const (
DeviceButtonHome DeviceButton = "home"
DeviceButtonVolumeUp DeviceButton = "volumeUp"
DeviceButtonVolumeDown DeviceButton = "volumeDown"
DeviceButtonEnter DeviceButton = "enter" // use "\n" for ios
DeviceButtonBack DeviceButton = "back" // android only
)
type NotificationType string

View File

@@ -26,3 +26,7 @@ type AppLaunchRequest struct {
type AppTerminateRequest struct {
PackageName string `json:"packageName" binding:"required" desc:"The package name of the app to terminate"`
}
type PressButtonRequest struct {
Button DeviceButton `json:"button" binding:"required" desc:"The button to press. Supported buttons: BACK (android only), HOME, VOLUME_UP, VOLUME_DOWN, ENTER."`
}