feat: add adb screencap sub command

This commit is contained in:
lilong.129
2023-04-14 16:35:10 +08:00
parent 4803d7fbf9
commit b5cb8c6ffe
4 changed files with 58 additions and 3 deletions
+19 -1
View File
@@ -1,6 +1,13 @@
package adb package adb
import "github.com/spf13/cobra" import (
"fmt"
"github.com/spf13/cobra"
"github.com/httprunner/httprunner/v4/hrp/pkg/gadb"
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
)
var androidRootCmd = &cobra.Command{ var androidRootCmd = &cobra.Command{
Use: "adb", Use: "adb",
@@ -8,6 +15,17 @@ var androidRootCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) {}, PersistentPreRun: func(cmd *cobra.Command, args []string) {},
} }
func getDevice(serial string) (*gadb.Device, error) {
devices, err := uixt.GetAndroidDevices(serial)
if err != nil {
return nil, err
}
if len(devices) > 1 {
return nil, fmt.Errorf("found multiple attached devices, please specify android serial")
}
return devices[0], nil
}
func Init(rootCmd *cobra.Command) { func Init(rootCmd *cobra.Command) {
rootCmd.AddCommand(androidRootCmd) rootCmd.AddCommand(androidRootCmd)
} }
+37
View File
@@ -0,0 +1,37 @@
package adb
import (
"fmt"
"io/ioutil"
"time"
"github.com/spf13/cobra"
)
var screencapAndroidDevicesCmd = &cobra.Command{
Use: "screencap",
Short: "Start android screen capture",
RunE: func(cmd *cobra.Command, args []string) error {
device, err := getDevice(serial)
if err != nil {
return err
}
res, err := device.ScreenCap()
if err != nil {
return err
}
filepath := fmt.Sprintf("screencap_%d.png", time.Now().Unix())
if err = ioutil.WriteFile(filepath, res, 0o644); err != nil {
return err
}
fmt.Println("screencap saved to", filepath)
return nil
},
}
func init() {
screencapAndroidDevicesCmd.Flags().StringVarP(&serial, "serial", "s", "", "filter by device's serial")
androidRootCmd.AddCommand(screencapAndroidDevicesCmd)
}
+1 -1
View File
@@ -20,7 +20,7 @@ func getDevice(udid string) (gidevice.Device, error) {
return nil, err return nil, err
} }
if len(devices) > 1 { if len(devices) > 1 {
return nil, fmt.Errorf("multiple devices found, please specify ios udid") return nil, fmt.Errorf("found multiple attached devices, please specify ios udid")
} }
return devices[0], nil return devices[0], nil
} }
+1 -1
View File
@@ -102,7 +102,7 @@ func (s *veDEMOCRService) getOCRResult(imageBuf *bytes.Buffer) ([]OCRResult, err
logID = getLogID(resp.Header) logID = getLogID(resp.Header)
} }
log.Error().Err(err). log.Error().Err(err).
Str("logID", logID). Str("X-TT-LOGID", logID).
Int("imageBufSize", size). Int("imageBufSize", size).
Msgf("request OCR service failed, retry %d", i) Msgf("request OCR service failed, retry %d", i)
time.Sleep(1 * time.Second) time.Sleep(1 * time.Second)