mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-11 18:11:21 +08:00
Merge pull request #1531 from httprunner/wcl
release v4.3.1 - feat: add option WithScreenShot - feat: run xctest before start ios automation - feat: run step with specified loop times - feat: add options for FindTexts - refactor: move all UI APIs to uixt pkg - docs: add examples for UI APIs
This commit is contained in:
36
examples/worldcup/README.md
Normal file
36
examples/worldcup/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# World Cup Live
|
||||
|
||||
```text
|
||||
$ wcl -h
|
||||
Monitor FIFA World Cup Live
|
||||
|
||||
Usage:
|
||||
wcl [flags]
|
||||
|
||||
Flags:
|
||||
--android string run android app
|
||||
--auto auto enter live
|
||||
-d, --duration int set duration in seconds (default 30)
|
||||
-h, --help help for wcl
|
||||
-i, --interval int set interval in seconds (default 15)
|
||||
--ios string run ios app
|
||||
-l, --log-level string set log level (default "INFO")
|
||||
-n, --match-name string specify match name
|
||||
-p, --perf strings specify performance monitor, e.g. sys_cpu,sys_mem,sys_net,sys_disk,fps,network,gpu
|
||||
-u, --uuid string specify device serial or udid
|
||||
-v, --version version for wcl
|
||||
```
|
||||
|
||||
|
||||
```bash
|
||||
$ wcl -n "比利时vs摩洛哥" --android com.ss.android.ugc.aweme -d 300 -i 15 -u caf0cd51
|
||||
$ wcl -n "比利时vs摩洛哥" --ios com.ss.iphone.ugc.Aweme -d 300 -i 15 -p sys_cpu,sys_mem,sys_disk,sys_net,fps,network,gpu -u 00008030-000438191421802E
|
||||
```
|
||||
|
||||
## bundle id
|
||||
|
||||
| app | iOS | Android |
|
||||
| -- | -- | -- |
|
||||
| 抖音 | com.ss.iphone.ugc.Aweme | com.ss.android.ugc.aweme |
|
||||
| 央视频 | com.cctv.yangshipin.app.iphone | com.cctv.yangshipin.app.androidp |
|
||||
| 咪咕视频 | com.wondertek.hecmccmobile | com.cmcc.cmvideo |
|
||||
99
examples/worldcup/cli.go
Normal file
99
examples/worldcup/cli.go
Normal file
@@ -0,0 +1,99 @@
|
||||
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{
|
||||
Use: "wcl",
|
||||
Short: "Monitor FIFA World Cup Live",
|
||||
Version: "2022.12.03.0018",
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
log.Logger = zerolog.New(
|
||||
zerolog.ConsoleWriter{NoColor: false, Out: os.Stderr},
|
||||
).With().Timestamp().Logger()
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
setLogLevel(logLevel)
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
var device uixt.Device
|
||||
var bundleID string
|
||||
if iosApp != "" {
|
||||
log.Info().Str("bundleID", iosApp).Msg("init ios device")
|
||||
device = initIOSDevice(uuid)
|
||||
bundleID = iosApp
|
||||
} else if androidApp != "" {
|
||||
log.Info().Str("bundleID", androidApp).Msg("init android device")
|
||||
device = initAndroidDevice(uuid)
|
||||
bundleID = androidApp
|
||||
} else {
|
||||
return errors.New("android or ios app bundldID is required")
|
||||
}
|
||||
|
||||
wc := NewWorldCupLive(device, matchName, bundleID, duration, interval)
|
||||
|
||||
if auto {
|
||||
wc.EnterLive(bundleID)
|
||||
}
|
||||
|
||||
wc.Start()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
uuid string
|
||||
iosApp string
|
||||
androidApp string
|
||||
auto bool
|
||||
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().StringVar(&iosApp, "ios", "", "run ios app")
|
||||
rootCmd.PersistentFlags().StringVar(&androidApp, "android", "", "run android app")
|
||||
rootCmd.PersistentFlags().BoolVar(&auto, "auto", false, "auto enter live")
|
||||
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")
|
||||
rootCmd.PersistentFlags().StringSliceVarP(&perf, "perf", "p", nil,
|
||||
"specify performance monitor, e.g. sys_cpu,sys_mem,sys_net,sys_disk,fps,network,gpu")
|
||||
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func setLogLevel(level string) {
|
||||
level = strings.ToUpper(level)
|
||||
log.Info().Str("level", level).Msg("Set log level")
|
||||
switch level {
|
||||
case "DEBUG":
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
case "INFO":
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
case "WARN":
|
||||
zerolog.SetGlobalLevel(zerolog.WarnLevel)
|
||||
case "ERROR":
|
||||
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
|
||||
case "FATAL":
|
||||
zerolog.SetGlobalLevel(zerolog.FatalLevel)
|
||||
case "PANIC":
|
||||
zerolog.SetGlobalLevel(zerolog.PanicLevel)
|
||||
}
|
||||
}
|
||||
286
examples/worldcup/main.go
Normal file
286
examples/worldcup/main.go
Normal file
@@ -0,0 +1,286 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
|
||||
)
|
||||
|
||||
func convertTimeToSeconds(timeStr string) (int, error) {
|
||||
if !strings.Contains(timeStr, ":") {
|
||||
return 0, fmt.Errorf("invalid time string: %s", timeStr)
|
||||
}
|
||||
|
||||
ss := strings.Split(timeStr, ":")
|
||||
var seconds int
|
||||
for idx, s := range ss {
|
||||
i, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
seconds += i * int(math.Pow(60, float64(len(ss)-idx-1)))
|
||||
}
|
||||
|
||||
return seconds, nil
|
||||
}
|
||||
|
||||
func initIOSDevice(uuid string) uixt.Device {
|
||||
perfOptions := []uixt.IOSPerfOption{}
|
||||
for _, p := range perf {
|
||||
switch p {
|
||||
case "sys_cpu":
|
||||
perfOptions = append(perfOptions, uixt.WithIOSPerfSystemCPU(true))
|
||||
case "sys_mem":
|
||||
perfOptions = append(perfOptions, uixt.WithIOSPerfSystemMem(true))
|
||||
case "sys_net":
|
||||
perfOptions = append(perfOptions, uixt.WithIOSPerfSystemNetwork(true))
|
||||
case "sys_disk":
|
||||
perfOptions = append(perfOptions, uixt.WithIOSPerfSystemDisk(true))
|
||||
case "network":
|
||||
perfOptions = append(perfOptions, uixt.WithIOSPerfNetwork(true))
|
||||
case "fps":
|
||||
perfOptions = append(perfOptions, uixt.WithIOSPerfFPS(true))
|
||||
case "gpu":
|
||||
perfOptions = append(perfOptions, uixt.WithIOSPerfGPU(true))
|
||||
}
|
||||
}
|
||||
perfOptions = append(perfOptions, uixt.WithIOSPerfOutputInterval(interval*1000))
|
||||
|
||||
device, err := uixt.NewIOSDevice(
|
||||
uixt.WithUDID(uuid),
|
||||
uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800),
|
||||
uixt.WithResetHomeOnStartup(false), // not reset home on startup
|
||||
uixt.WithIOSPerfOptions(perfOptions...),
|
||||
uixt.WithXCTest("com.gtf.wda.runner.xctrunner"),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to init ios device")
|
||||
}
|
||||
return device
|
||||
}
|
||||
|
||||
func initAndroidDevice(uuid string) uixt.Device {
|
||||
device, err := uixt.NewAndroidDevice(uixt.WithSerialNumber(uuid))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to init android device")
|
||||
}
|
||||
return device
|
||||
}
|
||||
|
||||
type timeLog struct {
|
||||
UTCTimeStr string `json:"utc_time_str"`
|
||||
UTCTime int64 `json:"utc_time"`
|
||||
LiveTime string `json:"live_time"`
|
||||
LiveTimeSeconds int `json:"live_time_seconds"`
|
||||
}
|
||||
|
||||
type WorldCupLive struct {
|
||||
driver *uixt.DriverExt
|
||||
file *os.File
|
||||
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
|
||||
Duration int `json:"duration"` // seconds
|
||||
Timelines []timeLog `json:"timelines"`
|
||||
PerfData []string `json:"perfData"`
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
if matchName == "" {
|
||||
matchName = "unknown-match"
|
||||
}
|
||||
|
||||
startTime := time.Now()
|
||||
matchName = fmt.Sprintf("%s-%s", startTime.Format("2006-01-02"), matchName)
|
||||
resultDir := filepath.Join("worldcuplive", matchName, startTime.Format("15:04:05"))
|
||||
|
||||
if err = os.MkdirAll(filepath.Join(resultDir, "screenshot"), 0o755); err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to create result dir")
|
||||
}
|
||||
|
||||
filename := filepath.Join(resultDir, "log.txt")
|
||||
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o755)
|
||||
if err != nil {
|
||||
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 {
|
||||
interval = 15
|
||||
}
|
||||
if duration == 0 {
|
||||
duration = 30
|
||||
}
|
||||
|
||||
return &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,
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *WorldCupLive) getCurrentLiveTime(utcTime time.Time) error {
|
||||
utcTimeStr := utcTime.Format("15:04:05")
|
||||
fileName := filepath.Join(
|
||||
wc.resultDir, "screenshot", utcTimeStr)
|
||||
ocrTexts, err := wc.driver.GetTextsByOCR(
|
||||
uixt.WithScreenShot(fileName),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("get ocr texts failed")
|
||||
return err
|
||||
}
|
||||
|
||||
// filter ocr texts with time format
|
||||
secondsMap := map[string]int{}
|
||||
var secondsTexts []string
|
||||
for _, ocrText := range ocrTexts {
|
||||
seconds, err := convertTimeToSeconds(ocrText.Text)
|
||||
if err == nil {
|
||||
secondsTexts = append(secondsTexts, ocrText.Text)
|
||||
secondsMap[ocrText.Text] = seconds
|
||||
}
|
||||
}
|
||||
|
||||
var secondsText string
|
||||
if len(secondsTexts) == 1 {
|
||||
secondsText = secondsTexts[0]
|
||||
} else if len(secondsTexts) >= 2 {
|
||||
// select the second, the first maybe mobile system time
|
||||
secondsText = secondsTexts[1]
|
||||
} else {
|
||||
log.Warn().Msg("no time text found")
|
||||
return nil
|
||||
}
|
||||
|
||||
liveTimeSeconds := secondsMap[secondsText]
|
||||
line := fmt.Sprintf("%s\t%d\t%s\t%d\n",
|
||||
utcTimeStr, utcTime.Unix(), secondsText, liveTimeSeconds)
|
||||
log.Info().Str("utcTime", utcTimeStr).Str("liveTime", secondsText).Msg("log live time")
|
||||
if _, err := wc.file.WriteString(line); err != nil {
|
||||
log.Error().Err(err).Str("line", line).Msg("write timeseries failed")
|
||||
}
|
||||
wc.Timelines = append(wc.Timelines, timeLog{
|
||||
UTCTimeStr: utcTimeStr,
|
||||
UTCTime: utcTime.Unix(),
|
||||
LiveTime: secondsText,
|
||||
LiveTimeSeconds: liveTimeSeconds,
|
||||
})
|
||||
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
|
||||
}
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
// 青少年弹窗处理
|
||||
if points, err := wc.driver.GetTextXYs([]string{"青少年模式", "我知道了"}); err == nil {
|
||||
_ = wc.driver.TapAbsXY(points[1].X, points[1].Y)
|
||||
}
|
||||
|
||||
// 进入世界杯 tab
|
||||
if err = wc.driver.TapByOCR("世界杯"); err != nil {
|
||||
log.Error().Err(err).Msg("enter 直播中 failed")
|
||||
return err
|
||||
}
|
||||
time.Sleep(3 * time.Second)
|
||||
|
||||
// 进入世界杯直播
|
||||
if err = wc.driver.TapByOCR("直播中"); err != nil {
|
||||
log.Error().Err(err).Msg("enter 直播中 failed")
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wc *WorldCupLive) Start() {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
|
||||
timer := time.NewTimer(time.Duration(wc.Duration) * time.Second)
|
||||
for {
|
||||
select {
|
||||
case <-timer.C:
|
||||
wc.dumpResult()
|
||||
return
|
||||
case <-c:
|
||||
wc.dumpResult()
|
||||
return
|
||||
default:
|
||||
utcTime := time.Now()
|
||||
if utcTime.Unix()%int64(wc.Interval) == 0 {
|
||||
wc.getCurrentLiveTime(utcTime)
|
||||
} else {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (wc *WorldCupLive) dumpResult() error {
|
||||
wc.EndTime = time.Now().Format("2006-01-02 15:04:05")
|
||||
|
||||
// init json encoder
|
||||
buffer := new(bytes.Buffer)
|
||||
encoder := json.NewEncoder(buffer)
|
||||
encoder.SetEscapeHTML(false)
|
||||
encoder.SetIndent("", " ")
|
||||
|
||||
wc.PerfData = wc.driver.GetPerfData()
|
||||
err := encoder.Encode(wc)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("encode json failed")
|
||||
return err
|
||||
}
|
||||
|
||||
path := filepath.Join(wc.resultDir, "summary.json")
|
||||
err = os.WriteFile(path, buffer.Bytes(), 0o755)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("dump json path failed")
|
||||
return err
|
||||
}
|
||||
log.Info().Str("path", path).Msg("dump summary success")
|
||||
return nil
|
||||
}
|
||||
100
examples/worldcup/main_test.go
Normal file
100
examples/worldcup/main_test.go
Normal file
@@ -0,0 +1,100 @@
|
||||
//go:build localtest
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp"
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
|
||||
)
|
||||
|
||||
func TestConvertTimeToSeconds(t *testing.T) {
|
||||
testData := []struct {
|
||||
timeStr string
|
||||
seconds int
|
||||
}{
|
||||
{"00:00", 0},
|
||||
{"00:01", 1},
|
||||
{"01:00", 60},
|
||||
{"01:01", 61},
|
||||
{"00:01:02", 62},
|
||||
{"01:02:03", 3723},
|
||||
}
|
||||
|
||||
for _, td := range testData {
|
||||
seconds, err := convertTimeToSeconds(td.timeStr)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, td.seconds, seconds)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainIOS(t *testing.T) {
|
||||
uuid := "00008030-00194DA421C1802E"
|
||||
device := initIOSDevice(uuid)
|
||||
bundleID := "com.ss.iphone.ugc.Aweme"
|
||||
wc := NewWorldCupLive(device, "", bundleID, 30, 10)
|
||||
wc.EnterLive(bundleID)
|
||||
wc.Start()
|
||||
}
|
||||
|
||||
func TestMainAndroid(t *testing.T) {
|
||||
device := initAndroidDevice(uuid)
|
||||
bundleID := "com.ss.android.ugc.aweme"
|
||||
wc := NewWorldCupLive(device, "", bundleID, 30, 10)
|
||||
wc.EnterLive(bundleID)
|
||||
wc.Start()
|
||||
}
|
||||
|
||||
func TestIOSDouyinWorldCupLive(t *testing.T) {
|
||||
testCase := &hrp.TestCase{
|
||||
Config: hrp.NewConfig("直播_抖音_世界杯_ios").
|
||||
WithVariables(map[string]interface{}{
|
||||
"appBundleID": "com.ss.iphone.ugc.Aweme",
|
||||
}).
|
||||
SetIOS(
|
||||
uixt.WithUDID(uuid),
|
||||
uixt.WithWDALogOn(true),
|
||||
uixt.WithWDAPort(8700),
|
||||
uixt.WithWDAMjpegPort(8800),
|
||||
uixt.WithXCTest("com.gtf.wda.runner.xctrunner"),
|
||||
),
|
||||
TestSteps: []hrp.IStep{
|
||||
hrp.NewStep("启动抖音").
|
||||
IOS().
|
||||
Home().
|
||||
AppTerminate("$appBundleID"). // 关闭已运行的抖音
|
||||
AppLaunch("$appBundleID").
|
||||
TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)). // 处理青少年弹窗
|
||||
Validate().
|
||||
AssertOCRExists("首页", "抖音启动失败,「首页」不存在"),
|
||||
hrp.NewStep("点击首页").
|
||||
IOS().
|
||||
TapByOCR("首页", uixt.WithIndex(-1)).Sleep(2),
|
||||
hrp.NewStep("点击世界杯页").
|
||||
IOS().
|
||||
SwipeToTapText("世界杯",
|
||||
uixt.WithMaxRetryTimes(5),
|
||||
uixt.WithCustomDirection(0.4, 0.07, 0.6, 0.07), // 滑动 tab,从左到右,解决「世界杯」被遮挡的问题
|
||||
uixt.WithScope(0, 0, 1, 0.15), // 限定 tab 区域
|
||||
uixt.WithWaitTime(1),
|
||||
),
|
||||
hrp.NewStep("点击进入赛程晋级").
|
||||
Loop(5). // 重复执行 5 次
|
||||
IOS().
|
||||
TapByOCR("赛程晋级", uixt.WithIdentifier("click_live"), uixt.WithIndex(-1)).
|
||||
Sleep(3).Back().Sleep(3),
|
||||
hrp.NewStep("关闭抖音").
|
||||
IOS().
|
||||
AppTerminate("$appBundleID"),
|
||||
},
|
||||
}
|
||||
|
||||
runner := hrp.NewRunner(t).SetSaveTests(true)
|
||||
err := runner.Run(testCase)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user