From 4722553537139afee7e79d492d57a37bd13018b1 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 27 Mar 2025 19:56:12 +0800 Subject: [PATCH] feat: add PushFile for ADBDriver --- internal/version/VERSION | 2 +- uixt/android_driver_adb.go | 150 +++++++++++++++++++------------------ uixt/browser_driver.go | 20 +++-- uixt/driver.go | 1 + uixt/harmony_driver_hdc.go | 5 ++ uixt/ios_driver_wda.go | 5 ++ 6 files changed, 102 insertions(+), 81 deletions(-) diff --git a/internal/version/VERSION b/internal/version/VERSION index 7f4c0ac3..da67cfac 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0-beta-2503271948 +v5.0.0-beta-2503271956 diff --git a/uixt/android_driver_adb.go b/uixt/android_driver_adb.go index 944d8e24..7e617bde 100644 --- a/uixt/android_driver_adb.go +++ b/uixt/android_driver_adb.go @@ -944,78 +944,8 @@ func (ad *ADBDriver) OpenUrl(url string) (err error) { func (ad *ADBDriver) PushImage(localPath string) error { log.Info().Str("localPath", localPath).Msg("ADBDriver.PushImage") - remotePath := path.Join("/sdcard/DCIM/Camera/", path.Base(localPath)) - if err := ad.Device.PushFile(localPath, remotePath); err != nil { - return err - } - // refresh - _, _ = ad.Device.RunShellCommand("am", "broadcast", - "-a", "android.intent.action.MEDIA_SCANNER_SCAN_FILE", - "-d", fmt.Sprintf("file://%s", remotePath)) - return nil -} - -func (ad *ADBDriver) ClearImages() error { - log.Info().Msg("ADBDriver.ClearImages") - _, _ = ad.Device.RunShellCommand("rm", "-rf", "/sdcard/DCIM/Camera/*") - return nil -} - -func (ad *ADBDriver) PullFiles(localDir string, remoteDirs ...string) error { - log.Info().Str("localDir", localDir).Strs("remoteDirs", remoteDirs).Msg("ADBDriver.PullFiles") - - // create local directory if not exists - if err := os.MkdirAll(localDir, 0o755); err != nil { - return fmt.Errorf("failed to create local directory: %w", err) - } - - for _, remoteDir := range remoteDirs { - files, err := ad.Device.List(remoteDir) - if err != nil { - return fmt.Errorf("failed to list directory %s: %w", remoteDir, err) - } - - for _, file := range files { - remotePath := path.Join(remoteDir, file.Name) - localPath := path.Join(localDir, file.Name) - - // check if file already exists - if _, err := os.Stat(localPath); err == nil { - log.Debug().Str("localPath", localPath).Msg("file already exists, skipping") - continue - } - - // create local file - f, err := os.Create(localPath) - if err != nil { - log.Error().Err(err).Str("localPath", localPath).Msg("failed to create local file") - continue - } - defer f.Close() - - // pull image file - if err := ad.Device.Pull(remotePath, f); err != nil { - log.Error().Err(err). - Str("remotePath", remotePath). - Str("localPath", localPath). - Msg("failed to pull file") - continue // continue with next file - } - log.Info(). - Str("remotePath", remotePath). - Str("localPath", localPath). - Msg("file pulled successfully") - } - } - return nil -} - -func (ad *ADBDriver) ClearFiles(paths ...string) error { - log.Info().Strs("paths", paths).Msg("ADBDriver.ClearFiles") - for _, path := range paths { - _, _ = ad.Device.RunShellCommand("rm", "-rf", path) - } - return nil + remoteDir := "/sdcard/DCIM/Camera/" + return ad.PushFile(localPath, remoteDir) } // PullImages pulls all images from device's DCIM/Camera directory to local directory @@ -1087,6 +1017,82 @@ func isImageFile(ext string) bool { return imageExts[ext] } +func (ad *ADBDriver) ClearImages() error { + log.Info().Msg("ADBDriver.ClearImages") + _, _ = ad.Device.RunShellCommand("rm", "-rf", "/sdcard/DCIM/Camera/*") + return nil +} + +func (ad *ADBDriver) PushFile(localPath string, remoteDir string) error { + log.Info().Str("localPath", localPath).Str("remoteDir", remoteDir).Msg("ADBDriver.PushFile") + remotePath := path.Join(remoteDir, path.Base(localPath)) + if err := ad.Device.PushFile(localPath, remotePath); err != nil { + return err + } + // refresh + _, _ = ad.Device.RunShellCommand("am", "broadcast", + "-a", "android.intent.action.MEDIA_SCANNER_SCAN_FILE", + "-d", fmt.Sprintf("file://%s", remotePath)) + return nil +} + +func (ad *ADBDriver) PullFiles(localDir string, remoteDirs ...string) error { + log.Info().Str("localDir", localDir).Strs("remoteDirs", remoteDirs).Msg("ADBDriver.PullFiles") + + // create local directory if not exists + if err := os.MkdirAll(localDir, 0o755); err != nil { + return fmt.Errorf("failed to create local directory: %w", err) + } + + for _, remoteDir := range remoteDirs { + files, err := ad.Device.List(remoteDir) + if err != nil { + return fmt.Errorf("failed to list directory %s: %w", remoteDir, err) + } + + for _, file := range files { + remotePath := path.Join(remoteDir, file.Name) + localPath := path.Join(localDir, file.Name) + + // check if file already exists + if _, err := os.Stat(localPath); err == nil { + log.Debug().Str("localPath", localPath).Msg("file already exists, skipping") + continue + } + + // create local file + f, err := os.Create(localPath) + if err != nil { + log.Error().Err(err).Str("localPath", localPath).Msg("failed to create local file") + continue + } + defer f.Close() + + // pull image file + if err := ad.Device.Pull(remotePath, f); err != nil { + log.Error().Err(err). + Str("remotePath", remotePath). + Str("localPath", localPath). + Msg("failed to pull file") + continue // continue with next file + } + log.Info(). + Str("remotePath", remotePath). + Str("localPath", localPath). + Msg("file pulled successfully") + } + } + return nil +} + +func (ad *ADBDriver) ClearFiles(paths ...string) error { + log.Info().Strs("paths", paths).Msg("ADBDriver.ClearFiles") + for _, path := range paths { + _, _ = ad.Device.RunShellCommand("rm", "-rf", path) + } + return nil +} + type ExportPoint struct { Start int `json:"start" yaml:"start"` End int `json:"end" yaml:"end"` diff --git a/uixt/browser_driver.go b/uixt/browser_driver.go index 2c15b971..42f6649d 100644 --- a/uixt/browser_driver.go +++ b/uixt/browser_driver.go @@ -444,10 +444,22 @@ func (wd *BrowserDriver) AppClear(packageName string) error { return errors.New("not support") } +func (wd *BrowserDriver) PushImage(localPath string) error { + return errors.New("not support") +} + +func (wd *BrowserDriver) PullImages(localDir string) error { + return errors.New("not support") +} + func (wd *BrowserDriver) ClearImages() error { return errors.New("not support") } +func (wd *BrowserDriver) PushFile(localPath string, remoteDir string) error { + return errors.New("not support") +} + func (wd *BrowserDriver) PullFiles(localDir string, remoteDirs ...string) error { return errors.New("not support") } @@ -456,14 +468,6 @@ func (wd *BrowserDriver) ClearFiles(paths ...string) error { return errors.New("not support") } -func (wd *BrowserDriver) PushImage(localPath string) error { - return errors.New("not support") -} - -func (wd *BrowserDriver) PullImages(localDir string) error { - return errors.New("not support") -} - func (wd *BrowserDriver) Orientation() (orientation types.Orientation, err error) { log.Warn().Msg("Orientation not implemented in ADBDriver") return diff --git a/uixt/driver.go b/uixt/driver.go index 2586adfa..f9557594 100644 --- a/uixt/driver.go +++ b/uixt/driver.go @@ -72,6 +72,7 @@ type IDriver interface { ClearImages() error // files related + PushFile(localPath string, remoteDir string) error PullFiles(localDir string, remoteDirs ...string) error ClearFiles(paths ...string) error diff --git a/uixt/harmony_driver_hdc.go b/uixt/harmony_driver_hdc.go index 59e80845..19509bc6 100644 --- a/uixt/harmony_driver_hdc.go +++ b/uixt/harmony_driver_hdc.go @@ -298,6 +298,11 @@ func (hd *HDCDriver) ClearImages() error { return nil } +func (hd *HDCDriver) PushFile(localPath string, remoteDir string) error { + log.Warn().Msg("PushFile not implemented in HDCDriver") + return nil +} + func (hd *HDCDriver) PullFiles(localDir string, remoteDirs ...string) error { log.Warn().Msg("PullFiles not implemented in HDCDriver") return nil diff --git a/uixt/ios_driver_wda.go b/uixt/ios_driver_wda.go index 94189c5a..7d09948c 100644 --- a/uixt/ios_driver_wda.go +++ b/uixt/ios_driver_wda.go @@ -1002,6 +1002,11 @@ func (wd *WDADriver) ClearImages() error { return err } +func (wd *WDADriver) PushFile(localPath string, remoteDir string) error { + log.Warn().Msg("PushFile not implemented in WDADriver") + return nil +} + func (wd *WDADriver) PullFiles(localDir string, remoteDirs ...string) error { log.Warn().Msg("PullFiles not implemented in WDADriver") return nil