fix: screen record with scrcpy

This commit is contained in:
lilong.129
2025-03-06 17:50:01 +08:00
parent cc81c00a82
commit 79e0323471
4 changed files with 50 additions and 11 deletions
+1 -1
View File
@@ -1 +1 @@
v5.0.0-beta-2503061657 v5.0.0-beta-2503061750
+17 -4
View File
@@ -791,7 +791,7 @@ func (ad *ADBDriver) ScreenRecord(opts ...option.ActionOption) (videoPath string
filePath = filepath.Join(config.GetConfig().ScreenShotsPath, fmt.Sprintf("%s.mp4", timestamp)) filePath = filepath.Join(config.GetConfig().ScreenShotsPath, fmt.Sprintf("%s.mp4", timestamp))
} }
duration := options.Duration duration := options.ScreenRecordDuration
audioOn := options.ScreenRecordWithAudio audioOn := options.ScreenRecordWithAudio
// get android system version // get android system version
@@ -806,7 +806,9 @@ func (ad *ADBDriver) ScreenRecord(opts ...option.ActionOption) (videoPath string
} }
var useAdbScreenRecord bool var useAdbScreenRecord bool
if !audioOn { if options.ScreenRecordWithScrcpy {
useAdbScreenRecord = false
} else if !audioOn {
log.Info().Bool("audioOn", audioOn).Msg("screen record with adb screenrecord by default") log.Info().Bool("audioOn", audioOn).Msg("screen record with adb screenrecord by default")
useAdbScreenRecord = true useAdbScreenRecord = true
} else if sysVersion != 0 && sysVersion < 11 { } else if sysVersion != 0 && sysVersion < 11 {
@@ -817,6 +819,17 @@ func (ad *ADBDriver) ScreenRecord(opts ...option.ActionOption) (videoPath string
useAdbScreenRecord = true useAdbScreenRecord = true
} }
defer func() {
if err == nil {
filePath, err = filepath.Abs(filePath)
if err != nil {
err = errors.Wrap(err, "get absolute path failed")
} else {
log.Info().Str("path", filePath).Msg("screen record success")
}
}
}()
if useAdbScreenRecord { if useAdbScreenRecord {
res, err := ad.Device.ScreenRecord(duration) res, err := ad.Device.ScreenRecord(duration)
if err != nil { if err != nil {
@@ -829,7 +842,7 @@ func (ad *ADBDriver) ScreenRecord(opts ...option.ActionOption) (videoPath string
} }
// screen record with audio // screen record with audio
log.Info().Msg("screen record with audio, use scrcpy") log.Info().Float64("duration(s)", duration).Msg("screen record with audio, use scrcpy")
file, err := os.Create(filePath) file, err := os.Create(filePath)
if err != nil { if err != nil {
@@ -878,7 +891,7 @@ func (ad *ADBDriver) ScreenRecord(opts ...option.ActionOption) (videoPath string
} }
} }
return filepath.Abs(filePath) return filePath, nil
} }
func (ad *ADBDriver) Setup() error { func (ad *ADBDriver) Setup() error {
+19 -3
View File
@@ -195,10 +195,26 @@ func TestDriver_ADB_ForegroundInfo(t *testing.T) {
func TestDriver_ADB_ScreenRecord(t *testing.T) { func TestDriver_ADB_ScreenRecord(t *testing.T) {
driver := setupADBDriverExt(t) driver := setupADBDriverExt(t)
path, err := driver.ScreenRecord(option.WithScreenRecordDuation(5)) path1, err := driver.ScreenRecord(option.WithScreenRecordDuation(182))
assert.Nil(t, err) assert.Nil(t, err)
defer os.Remove(path) defer os.Remove(path1)
t.Log(path) t.Log(path1)
path2, err := driver.ScreenRecord(
option.WithScreenRecordDuation(5),
option.WithScreenRecordAudio(true),
)
assert.Nil(t, err)
defer os.Remove(path2)
t.Log(path2)
path3, err := driver.ScreenRecord(
option.WithScreenRecordDuation(5),
option.WithScreenRecordScrcpy(true),
)
assert.Nil(t, err)
defer os.Remove(path3)
t.Log(path3)
} }
func TestDriver_ADB_Backspace(t *testing.T) { func TestDriver_ADB_Backspace(t *testing.T) {
+13 -3
View File
@@ -127,9 +127,10 @@ func WithScreenShotFileName(fileName string) ActionOption {
} }
type ScreenRecordOptions struct { type ScreenRecordOptions struct {
ScreenRecordDuration float64 `json:"screenrecord_duration,omitempty" yaml:"screenrecord_duration,omitempty"` ScreenRecordDuration float64 `json:"screenrecord_duration,omitempty" yaml:"screenrecord_duration,omitempty"`
ScreenRecordWithAudio bool `json:"screenrecord_with_audio,omitempty" yaml:"screenrecord_with_audio,omitempty"` ScreenRecordWithAudio bool `json:"screenrecord_with_audio,omitempty" yaml:"screenrecord_with_audio,omitempty"`
ScreenRecordPath string `json:"screenrecord_path,omitempty" yaml:"screenrecord_path,omitempty"` ScreenRecordWithScrcpy bool `json:"screenrecord_with_scrcpy,omitempty" yaml:"screenrecord_with_scrcpy,omitempty"`
ScreenRecordPath string `json:"screenrecord_path,omitempty" yaml:"screenrecord_path,omitempty"`
} }
func (o *ScreenRecordOptions) GetScreenRecordOptions() []ActionOption { func (o *ScreenRecordOptions) GetScreenRecordOptions() []ActionOption {
@@ -145,6 +146,9 @@ func (o *ScreenRecordOptions) GetScreenRecordOptions() []ActionOption {
if o.ScreenRecordWithAudio { if o.ScreenRecordWithAudio {
options = append(options, WithScreenRecordAudio(true)) options = append(options, WithScreenRecordAudio(true))
} }
if o.ScreenRecordWithScrcpy {
options = append(options, WithScreenRecordScrcpy(true))
}
if o.ScreenRecordPath != "" { if o.ScreenRecordPath != "" {
options = append(options, WithScreenRecordPath(o.ScreenRecordPath)) options = append(options, WithScreenRecordPath(o.ScreenRecordPath))
} }
@@ -163,6 +167,12 @@ func WithScreenRecordAudio(audioOn bool) ActionOption {
} }
} }
func WithScreenRecordScrcpy(scrcpyOn bool) ActionOption {
return func(o *ActionOptions) {
o.ScreenRecordWithScrcpy = scrcpyOn
}
}
func WithScreenRecordPath(path string) ActionOption { func WithScreenRecordPath(path string) ActionOption {
return func(o *ActionOptions) { return func(o *ActionOptions) {
o.ScreenRecordPath = path o.ScreenRecordPath = path