From 7312d50a3370f01b336b929adcf00e8081464bc2 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 27 Nov 2022 00:16:02 +0800 Subject: [PATCH] docs: enter WC live --- examples/worldcup/README.md | 15 ++++++++ examples/worldcup/cli.go | 37 ++++++++++++++----- examples/worldcup/main.go | 66 ++++++++++++++++++++++++++++------ examples/worldcup/main_test.go | 22 +++++++----- hrp/cmd/ios/ios_test.go | 12 +++++++ hrp/pkg/uixt/input.go | 5 +++ 6 files changed, 130 insertions(+), 27 deletions(-) create mode 100644 examples/worldcup/README.md create mode 100644 hrp/cmd/ios/ios_test.go create mode 100644 hrp/pkg/uixt/input.go diff --git a/examples/worldcup/README.md b/examples/worldcup/README.md new file mode 100644 index 00000000..a8bdc6b4 --- /dev/null +++ b/examples/worldcup/README.md @@ -0,0 +1,15 @@ +# World Cup Live + +```bash +$ wcl -n "法国vs丹麦" --android com.ss.android.ugc.aweme -d 120 +``` + + +抖音: + +- com.ss.iphone.ugc.Aweme +- com.ss.android.ugc.aweme + +央视频: + +咪咕视频: diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go index 544ca2a3..69ca9601 100644 --- a/examples/worldcup/cli.go +++ b/examples/worldcup/cli.go @@ -1,12 +1,15 @@ package main import ( + "errors" "os" "strings" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "github.com/spf13/cobra" + + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) var rootCmd = &cobra.Command{ @@ -21,7 +24,21 @@ var rootCmd = &cobra.Command{ setLogLevel(logLevel) }, RunE: func(cmd *cobra.Command, args []string) error { - wc := NewWorldCupLive(matchName, osType, duration, interval) + var device uixt.Device + var bundleID string + if iosApp != "" { + log.Info().Str("bundleID", iosApp).Msg("init ios device") + device = initIOSDevice() + bundleID = iosApp + } else if androidApp != "" { + log.Info().Str("bundleID", androidApp).Msg("init android device") + device = initAndroidDevice() + bundleID = androidApp + } else { + return errors.New("android or ios app bundldID is required") + } + + wc := NewWorldCupLive(device, matchName, bundleID, duration, interval) wc.Start() wc.DumpResult() return nil @@ -29,19 +46,21 @@ var rootCmd = &cobra.Command{ } var ( - uuid string - osType string - duration int - interval int - logLevel string - matchName string - perf []string + uuid string + iosApp string + androidApp string + duration int + interval int + logLevel string + matchName string + perf []string ) func main() { rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "INFO", "set log level") rootCmd.PersistentFlags().StringVarP(&uuid, "uuid", "u", "", "specify device serial or udid") - rootCmd.PersistentFlags().StringVarP(&osType, "os-type", "t", "ios", "specify mobile os type") + rootCmd.PersistentFlags().StringVar(&iosApp, "ios", "", "run ios app") + rootCmd.PersistentFlags().StringVar(&androidApp, "android", "", "run android app") rootCmd.PersistentFlags().IntVarP(&duration, "duration", "d", 30, "set duration in seconds") rootCmd.PersistentFlags().IntVarP(&interval, "interval", "i", 15, "set interval in seconds") rootCmd.PersistentFlags().StringVarP(&matchName, "match-name", "n", "", "specify match name") diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index d647f678..8cf6eaf3 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -90,6 +90,7 @@ type WorldCupLive struct { resultDir string UUID string `json:"uuid"` MatchName string `json:"matchName"` + BundleID string `json:"bundleID"` StartTime string `json:"startTime"` EndTime string `json:"endTime"` Interval int `json:"interval"` // seconds @@ -98,15 +99,7 @@ type WorldCupLive struct { PerfData []string `json:"perfData"` } -func NewWorldCupLive(matchName, osType string, duration, interval int) *WorldCupLive { - var device uixt.Device - log.Info().Str("osType", osType).Msg("init device") - if osType == "ios" { - device = initIOSDevice() - } else { - device = initAndroidDevice() - } - +func NewWorldCupLive(device uixt.Device, matchName, bundleID string, duration, interval int) *WorldCupLive { driverExt, err := device.NewDriver(nil) if err != nil { log.Fatal().Err(err).Msg("failed to init driver") @@ -130,6 +123,7 @@ func NewWorldCupLive(matchName, osType string, duration, interval int) *WorldCup log.Fatal().Err(err).Msg("failed to open file") } // write title + f.WriteString(fmt.Sprintf("%s\t%s\t%s\n", matchName, device.UUID(), bundleID)) f.WriteString("utc_time\tutc_timestamp\tlive_time\tlive_seconds\n") if interval == 0 { @@ -139,16 +133,22 @@ func NewWorldCupLive(matchName, osType string, duration, interval int) *WorldCup duration = 30 } - return &WorldCupLive{ + wc := &WorldCupLive{ driver: driverExt, file: f, resultDir: resultDir, UUID: device.UUID(), + BundleID: bundleID, Duration: duration, Interval: interval, StartTime: startTime.Format("2006-01-02 15:04:05"), MatchName: matchName, } + + if bundleID != "" { + wc.EnterLive(bundleID) + } + return wc } func (wc *WorldCupLive) getCurrentLiveTime(utcTime time.Time) error { @@ -183,6 +183,52 @@ func (wc *WorldCupLive) getCurrentLiveTime(utcTime time.Time) error { return nil } +func (wc *WorldCupLive) EnterLive(bundleID string) error { + log.Info().Msg("enter world cup live") + + // kill app + _, err := wc.driver.Driver.AppTerminate(bundleID) + if err != nil { + log.Error().Err(err).Msg("terminate app failed") + } + + // launch app + err = wc.driver.Driver.AppLaunch(bundleID) + if err != nil { + log.Error().Err(err).Msg("launch app failed") + return err + } + + // 青少年弹窗处理 + if points, err := wc.driver.GetTextXYs([]string{"青少年模式", "我知道了"}); err == nil { + _ = wc.driver.TapAbsXY(points[1].X, points[1].Y) + } + + // 点击进入搜索 + err = wc.driver.TapXY(0.9, 0.07) + if err != nil { + log.Error().Err(err).Msg("enter search failed") + return err + } + + // 搜索世界杯 + _ = wc.driver.Input("世界杯") + err = wc.driver.TapByOCR("搜索") + if err != nil { + log.Error().Err(err).Msg("search 世界杯 failed") + return err + } + time.Sleep(2 * time.Second) + + // 进入世界杯直播 + if err = wc.driver.TapByOCR("直播中"); err != nil { + log.Error().Err(err).Msg("enter 直播中 failed") + return err + } + + return nil +} + func (wc *WorldCupLive) Start() { wc.done = make(chan bool) go func() { diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index cbc75f4e..d1ad2ecf 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -1,11 +1,7 @@ +//go:build localtest + package main -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - func TestConvertTimeToSeconds(t *testing.T) { testData := []struct { timeStr string @@ -26,6 +22,16 @@ func TestConvertTimeToSeconds(t *testing.T) { } } -func TestMain(t *testing.T) { - main() +func TestMainIOS(t *testing.T) { + device := initIOSDevice() + wc := NewWorldCupLive(device, "", "com.ss.iphone.ugc.Aweme", 30, 10) + wc.Start() + wc.DumpResult() +} + +func TestMainAndroid(t *testing.T) { + device := initAndroidDevice() + wc := NewWorldCupLive(device, "", "com.ss.android.ugc.aweme", 30, 10) + wc.Start() + wc.DumpResult() } diff --git a/hrp/cmd/ios/ios_test.go b/hrp/cmd/ios/ios_test.go new file mode 100644 index 00000000..cd0709cd --- /dev/null +++ b/hrp/cmd/ios/ios_test.go @@ -0,0 +1,12 @@ +//go:build localtest + +package ios + +func TestGetDevice(t *testing.T) { + device, err := getDevice(udid) + if err != nil { + t.Fatal(err) + } + + t.Logf("device: %v", device) +} diff --git a/hrp/pkg/uixt/input.go b/hrp/pkg/uixt/input.go new file mode 100644 index 00000000..f23df857 --- /dev/null +++ b/hrp/pkg/uixt/input.go @@ -0,0 +1,5 @@ +package uixt + +func (dExt *DriverExt) Input(text string) (err error) { + return dExt.Driver.Input(text) +}