merge master

This commit is contained in:
余泓铮
2024-08-27 16:56:26 +08:00
30 changed files with 836 additions and 263 deletions

View File

@@ -1,6 +1,8 @@
# Release History
## v4.3.9 (2023-09-19)
## v4.3.9 (2024-01-18)
- feat: add Shell step type
- fix: OCR calls use compressed image
## v4.3.8 (2023-09-19)

View File

@@ -0,0 +1,18 @@
{
"config": {
"name": "run ui test on bili android",
"variables": {
"RunTimes": 3,
"SerialNumber": "${ENV(SerialNumber)}"
}
},
"teststeps": [
{
"name": "run bili android",
"shell": {
"string": "bili_android",
"expect_exit_code": 0
}
}
]
}

View File

@@ -0,0 +1,137 @@
package main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
)
var (
serial string
runTimes int
)
func init() {
serial = os.Getenv("SerialNumber")
numStr := os.Getenv("RunTimes")
defaultNum := 20
var err error
runTimes, err = strconv.Atoi(numStr)
if err != nil {
runTimes = defaultNum
}
fmt.Printf("=== start running cases, serial=%s, runTimes=%d ===\n", serial, runTimes)
}
func launchAppDriver(pkgName string) (driver *uixt.DriverExt, err error) {
device, _ := uixt.NewAndroidDevice(uixt.WithSerialNumber(serial))
driver, err = device.NewDriver()
if err != nil {
return nil, err
}
_, err = driver.Driver.AppTerminate(pkgName)
if err != nil {
return nil, err
}
err = driver.Driver.Homescreen()
if err != nil {
return nil, err
}
err = driver.Driver.AppLaunch(pkgName)
if err != nil {
return nil, err
}
time.Sleep(15 * time.Second)
// 处理弹窗
err = driver.ClosePopupsHandler()
if err != nil {
return nil, err
}
// 进入推荐页
err = driver.TapByOCR("推荐", uixt.WithScope(0, 0, 1, 0.3))
if err != nil {
return nil, err
}
return driver, nil
}
func watchVideo(driver *uixt.DriverExt) (err error) {
time.Sleep(3 * time.Second)
err = driver.SwipeRelative(0.7, 0.7, 0.7, 0.2)
if err != nil {
return err
}
time.Sleep(1 * time.Second)
// 点击进入某视频
err = driver.TapXY(0.3, 0.5)
if err != nil {
return err
}
time.Sleep(5 * time.Second)
// 点击播放区域,展现横屏图标
err = driver.TapXY(0.5, 0.1)
if err != nil {
return err
}
time.Sleep(500 * time.Millisecond)
// 切换横屏
err = driver.TapByUIDetection(
uixt.WithScreenShotUITypes("fullScreen"))
if err != nil {
// 未找到横屏图标,该页面可能不是横版视频(直播|广告|Feed
// 退出回到推荐页
driver.Driver.PressBack()
return nil
}
// 观播 10s
time.Sleep(10 * time.Second)
// 返回视频页面
err = driver.Driver.PressBack()
if err != nil {
return err
}
time.Sleep(1 * time.Second)
// 返回推荐页
err = driver.Driver.PressBack()
if err != nil {
return err
}
return nil
}
// build shell command
// go build -o bili_android examples/uitest/bilibili/cli.go
func main() {
driver, err := launchAppDriver("tv.danmaku.bili")
if err != nil {
panic(err)
}
// 重复采集 XX 次
for i := 0; i < runTimes; i++ {
err = watchVideo(driver)
if err != nil {
panic(err)
}
}
}

View File

@@ -0,0 +1,35 @@
//go:build localtest
package main
import (
"testing"
"github.com/httprunner/httprunner/v4/hrp"
)
func TestMain(t *testing.T) {
main()
}
func TestRunCaseWithShell(t *testing.T) {
testcase1 := &hrp.TestCase{
Config: hrp.NewConfig("run ui test on bili android").
WithVariables(map[string]interface{}{
"SerialNumber": "${ENV(SerialNumber)}",
"RunTimes": 3,
}),
TestSteps: []hrp.IStep{
hrp.NewStep("run bili android").
Shell("bili_android"),
},
}
testcase1.Dump2JSON("bili_android.json")
r := hrp.NewRunner(t)
err := r.Run(testcase1)
if err != nil {
t.Fatal()
}
}

View File

@@ -0,0 +1,18 @@
{
"config": {
"name": "run ui test on bili ios",
"variables": {
"RunTimes": 3,
"UDID": "${ENV(UDID)}"
}
},
"teststeps": [
{
"name": "run bili ios",
"shell": {
"string": "bili_ios",
"expect_exit_code": 0
}
}
]
}

View File

@@ -0,0 +1,137 @@
package main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
)
var (
serial string
runTimes int
)
func init() {
serial = os.Getenv("UDID")
numStr := os.Getenv("RunTimes")
defaultNum := 20
var err error
runTimes, err = strconv.Atoi(numStr)
if err != nil {
runTimes = defaultNum
}
fmt.Printf("=== start running cases, serial=%s, runTimes=%d ===\n", serial, runTimes)
}
func launchAppDriver(pkgName string) (driver *uixt.DriverExt, err error) {
device, _ := uixt.NewIOSDevice(uixt.WithUDID(serial))
driver, err = device.NewDriver()
if err != nil {
return nil, err
}
//_, err = driver.Driver.AppTerminate(pkgName)
//if err != nil {
// return nil, err
//}
//
//err = driver.Driver.Homescreen()
//if err != nil {
// return nil, err
//}
//
//err = driver.Driver.AppLaunch(pkgName)
//if err != nil {
// return nil, err
//}
time.Sleep(15 * time.Second)
// 处理弹窗
err = driver.ClosePopupsHandler()
if err != nil {
return nil, err
}
// 进入推荐页
err = driver.TapByOCR("推荐", uixt.WithScope(0, 0, 1, 0.3))
if err != nil {
return nil, err
}
return driver, nil
}
func watchVideo(driver *uixt.DriverExt) (err error) {
time.Sleep(3 * time.Second)
err = driver.SwipeRelative(0.7, 0.7, 0.7, 0.2)
if err != nil {
return err
}
time.Sleep(1 * time.Second)
// 点击进入某视频
err = driver.TapXY(0.3, 0.5)
if err != nil {
return err
}
time.Sleep(5 * time.Second)
// 点击播放区域,展现横屏图标
err = driver.TapXY(0.5, 0.1)
if err != nil {
return err
}
time.Sleep(500 * time.Millisecond)
// 切换横屏
err = driver.TapByUIDetection(
uixt.WithScreenShotUITypes("fullScreen"))
if err != nil {
// 未找到横屏图标,该页面可能不是横版视频(直播|广告|Feed
// 退出回到推荐页
driver.Driver.PressBack()
return nil
}
// 观播 10s
time.Sleep(10 * time.Second)
// 返回视频页面
err = driver.Driver.PressBack()
if err != nil {
return err
}
time.Sleep(1 * time.Second)
// 返回推荐页
err = driver.Driver.PressBack()
if err != nil {
return err
}
return nil
}
// build shell command
// go build -o bili_android examples/uitest/bilibili/cli.go
func main() {
driver, err := launchAppDriver("tv.danmaku.bilianime")
if err != nil {
panic(err)
}
// 重复采集 XX 次
for i := 0; i < runTimes; i++ {
err = watchVideo(driver)
if err != nil {
panic(err)
}
}
}

10
go.mod
View File

@@ -5,12 +5,12 @@ go 1.18
require (
github.com/andybalholm/brotli v1.0.4
github.com/denisbrodbeck/machineid v1.0.1
github.com/fatih/color v1.15.0
github.com/fatih/color v1.16.0
github.com/getsentry/sentry-go v0.13.0
github.com/go-openapi/spec v0.20.7
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/gorilla/websocket v1.5.0
github.com/httprunner/funplugin v0.5.3
github.com/httprunner/funplugin v0.5.5
github.com/jinzhu/copier v0.3.5
github.com/jmespath/go-jmespath v0.4.0
github.com/json-iterator/go v1.1.12
@@ -36,7 +36,7 @@ require (
)
require (
cloud.google.com/go/compute v1.20.1 // indirect
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
@@ -64,7 +64,6 @@ require (
github.com/hashicorp/go-plugin v1.4.10 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/incu6us/goimports-reviser/v2 v2.5.3 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
@@ -101,7 +100,4 @@ require (
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230815205213-6bfd019c3878 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
mvdan.cc/gofumpt v0.6.0 // indirect
)
// replace github.com/httprunner/funplugin => ../funplugin

25
go.sum
View File

@@ -19,8 +19,8 @@ cloud.google.com/go/bigquery v1.4.0/go.mod h1:S8dzgnTigyfTmLBfrtrhyYhwRxG72rYxvf
cloud.google.com/go/bigquery v1.5.0/go.mod h1:snEHRnqQbz117VIFhE8bmtwIDY80NLUZUMb4Nv6dBIg=
cloud.google.com/go/bigquery v1.7.0/go.mod h1://okPTzCYNXSlb24MZs83e2Do+h+VXtc4gLoIoXIAPc=
cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM77hZzJN/fQ=
cloud.google.com/go/compute v1.20.1 h1:6aKEtlUiwEpJzM001l0yFkpXmUVXaN8W+fbkb2AZNbg=
cloud.google.com/go/compute v1.20.1/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdiEZc9FEIbM=
cloud.google.com/go/compute v1.23.0 h1:tP41Zoavr8ptEqaW6j+LQOnyBBhO7OkOMAGrgLopTwY=
cloud.google.com/go/compute v1.23.0/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdiEZc9FEIbM=
cloud.google.com/go/compute/metadata v0.2.3 h1:mg4jlk7mCAj6xXp9UJ4fjI9VUI5rubuGBW5aJ7UnBMY=
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE=
@@ -167,7 +167,6 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -195,15 +194,13 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE=
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/httprunner/funplugin v0.5.3 h1:OHYXqq8fuO/qzT+TzXxhS3HVfKdb8kh+Q/0/S3n4afA=
github.com/httprunner/funplugin v0.5.3/go.mod h1:YZzBBSOSdLZEpHZz0P2E5SOQ+o1+Fbn30oWS4RGHBz0=
github.com/httprunner/funplugin v0.5.5 h1:VU1a6kj1AsJ/ucIhhI5NLHXOP4xnW2JGgk50vBV3Zis=
github.com/httprunner/funplugin v0.5.5/go.mod h1:YZzBBSOSdLZEpHZz0P2E5SOQ+o1+Fbn30oWS4RGHBz0=
github.com/hybridgroup/mjpeg v0.0.0-20140228234708-4680f319790e/go.mod h1:eagM805MRKrioHYuU7iKLUyFPVKqVV6um5DAvCkUtXs=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.0.1 h1:U3uMjPSQEBMNp1lFxmllqCPM6P5u/Xq7Pgzkat/bFNc=
github.com/inconshreveable/mousetrap v1.0.1/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/incu6us/goimports-reviser/v2 v2.5.3 h1:DzvFl1+qOIDukqN8vMM/10MQswFQywUdwXxsjuowxlc=
github.com/incu6us/goimports-reviser/v2 v2.5.3/go.mod h1:P18aXhQaED7izHIP9IPI9PqEs7Y7D9okq71Q8Y8yHN4=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
github.com/jinzhu/copier v0.3.5 h1:GlvfUwHk62RokgqVNvYsku0TATCF7bAHVwEXoBh3iJg=
@@ -324,8 +321,6 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
@@ -424,8 +419,6 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -457,8 +450,6 @@ golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81R
golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
@@ -481,8 +472,6 @@ golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -530,8 +519,6 @@ golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
@@ -545,8 +532,6 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
@@ -594,8 +579,6 @@ golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@@ -2,7 +2,7 @@ package adb
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
@@ -36,7 +36,7 @@ var screencapAndroidDevicesCmd = &cobra.Command{
}
filepath := fmt.Sprintf("%s.png", builtin.GenNameWithTimestamp("screencap_%d"))
if err = ioutil.WriteFile(filepath, res, 0o644); err != nil {
if err = os.WriteFile(filepath, res, 0o644); err != nil {
return err
}
fmt.Println("screencap saved to", filepath)

View File

@@ -1 +1 @@
v4.6.5
v4.6.5

View File

@@ -3,7 +3,7 @@
package gadb
import (
"io/ioutil"
"os"
"testing"
)
@@ -116,6 +116,6 @@ func TestScreenCap(t *testing.T) {
t.Error(err)
}
t.Log(len(res))
ioutil.WriteFile("/tmp/1.png", res, 0o644)
os.WriteFile("/tmp/1.png", res, 0o644)
}
}

View File

@@ -4,7 +4,6 @@ package gadb
import (
"bytes"
"io/ioutil"
"os"
"reflect"
"strings"
@@ -271,7 +270,7 @@ func TestDevice_Pull(t *testing.T) {
}
userHomeDir, _ := os.UserHomeDir()
if err = ioutil.WriteFile(userHomeDir+"/Desktop/hello.txt", buffer.Bytes(), DefaultFileMode); err != nil {
if err = os.WriteFile(userHomeDir+"/Desktop/hello.txt", buffer.Bytes(), DefaultFileMode); err != nil {
t.Fatal(err)
}
}

View File

@@ -752,7 +752,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) {
}
return dExt.VideoCrawler(configs)
case ACTION_ClosePopups:
return dExt.ClosePopups(action.GetOptions()...)
return dExt.ClosePopupsHandler()
case ACTION_EndToEndDelay:
dExt.CollectEndToEndDelay(action.GetOptions()...)
return nil

View File

@@ -195,6 +195,10 @@ func (dev *AndroidDevice) Init() error {
return nil
}
func (dev *AndroidDevice) System() string {
return "android"
}
func (dev *AndroidDevice) UUID() string {
return dev.SerialNumber
}

View File

@@ -91,7 +91,7 @@ func TestDriver_Screenshot(t *testing.T) {
t.Fatal(err)
}
t.Log(ioutil.WriteFile("/Users/hero/Desktop/s1.png", screenshot.Bytes(), 0o600))
t.Log(os.WriteFile("/Users/hero/Desktop/s1.png", screenshot.Bytes(), 0o600))
}
func TestDriver_Rotation(t *testing.T) {
@@ -363,7 +363,7 @@ func TestDriver_AppLaunch(t *testing.T) {
t.Fatal(err)
}
t.Log(ioutil.WriteFile("s1.png", raw.Bytes(), 0o600))
t.Log(os.WriteFile("s1.png", raw.Bytes(), 0o600))
}
func TestDriver_IsAppInForeground(t *testing.T) {

View File

@@ -14,7 +14,6 @@ import (
"os"
"os/signal"
"path/filepath"
"sort"
"strings"
"syscall"
"time"
@@ -63,9 +62,12 @@ type ScreenResult struct {
Video *Video `json:"video,omitempty"`
Popup *PopupInfo `json:"popup,omitempty"`
SwipeStartTime int64 `json:"swipe_start_time"` // 滑动开始时间戳
SwipeFinishTime int64 `json:"swipe_finish_time"` // 滑动结束时间戳
SwipeStartTime int64 `json:"swipe_start_time"` // 滑动开始时间戳
SwipeFinishTime int64 `json:"swipe_finish_time"` // 滑动结束时间戳
FetchVideoStartTime int64 `json:"fetch_video_start_time"` // 抓取视频开始时间戳
FetchVideoFinishTime int64 `json:"fetch_video_finish_time"` // 抓取视频结束时间戳
FetchVideoElapsed int64 `json:"fetch_video_elapsed"` // 抓取视频耗时(ms)
ScreenshotTakeElapsed int64 `json:"screenshot_take_elapsed"` // 设备截图耗时(ms)
ScreenshotCVElapsed int64 `json:"screenshot_cv_elapsed"` // CV 识别耗时(ms)
@@ -87,40 +89,6 @@ func (screenResults ScreenResultMap) getScreenShotUrls() map[string]string {
return screenShotsUrls
}
// updatePopupCloseStatus checks if popup closed normally in every screenResult with close_popups on:
func (screenResults ScreenResultMap) updatePopupCloseStatus() {
var popupScreenResultList []*ScreenResult
for _, screenResult := range screenResults {
if screenResult.Popup == nil {
continue
}
popupScreenResultList = append(popupScreenResultList, screenResult)
}
if len(popupScreenResultList) == 0 {
return
}
sort.Slice(popupScreenResultList, func(i, j int) bool {
return popupScreenResultList[i].Popup.RetryCount < popupScreenResultList[j].Popup.RetryCount
})
for i := 0; i < len(popupScreenResultList)-1; i++ {
curPopup := popupScreenResultList[i].Popup
nextPopup := popupScreenResultList[i+1].Popup
// popup not existed, no need to close
if curPopup.CloseArea.IsEmpty() {
continue
}
// popup existed, but identical popups occurs during next retry
if nextPopup.CloseArea.IsIdentical(curPopup.CloseArea) {
popupScreenResultList[i].Popup.CloseStatus = CloseStatusFail
continue
}
// popup existed, but no popup or different popup occurs during next retry (IsClosed=true)
popupScreenResultList[i].Popup.CloseStatus = CloseStatusSuccess
}
}
type cacheStepData struct {
// cache step screenshot paths
screenShots []string
@@ -153,6 +121,9 @@ type DriverExt struct {
// funplugin
plugin funplugin.IPlugin
// cache last popup to check if popup handle result
lastPopup *PopupInfo
}
func newDriverExt(device Device, driver WebDriver, options ...DriverOption) (dExt *DriverExt, err error) {
@@ -364,7 +335,6 @@ func (dExt *DriverExt) GetStepCacheData() map[string]interface{} {
cacheData["screenshots"] = dExt.cacheStepData.screenShots
cacheData["screenshots_urls"] = dExt.cacheStepData.screenResults.getScreenShotUrls()
dExt.cacheStepData.screenResults.updatePopupCloseStatus()
cacheData["screen_results"] = dExt.cacheStepData.screenResults
cacheData["e2e_results"] = dExt.cacheStepData.e2eDelay
cacheData["driver_request_results"] = dExt.Driver.GetDriverResults()

View File

@@ -7,8 +7,6 @@ import (
"time"
"github.com/httprunner/funplugin"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
)
var (
@@ -443,8 +441,8 @@ type PointF struct {
}
func (p PointF) IsIdentical(p2 PointF) bool {
return builtin.IsZeroFloat64(math.Abs(p.X-p2.X)) &&
builtin.IsZeroFloat64(math.Abs(p.Y-p2.Y))
// set the coordinate precision to 1 pixel
return math.Abs(p.X-p2.X) < 1 && math.Abs(p.Y-p2.Y) < 1
}
type Rect struct {

View File

@@ -325,7 +325,7 @@ func Test_remoteWD_GetPasteboard(t *testing.T) {
// t.Fatal(err)
// }
// userHomeDir, _ := os.UserHomeDir()
// if err = ioutil.WriteFile(userHomeDir+"/Desktop/p1.png", buffer.Bytes(), 0600); err != nil {
// if err = os.WriteFile(userHomeDir+"/Desktop/p1.png", buffer.Bytes(), 0600); err != nil {
// t.Error(err)
// }
}

View File

@@ -1,12 +1,11 @@
package uixt
import (
"time"
"math/rand"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/code"
)
@@ -27,12 +26,6 @@ var popups = [][]string{
{"管理使用时间", ".*忽略.*"},
}
const (
CloseStatusFound = "found"
CloseStatusSuccess = "success"
CloseStatusFail = "fail"
)
func findTextPopup(screenTexts OCRTexts) (closePoint *OCRText) {
for _, popup := range popups {
if len(popup) != 2 {
@@ -80,6 +73,12 @@ func (dExt *DriverExt) AutoPopupHandler() error {
return dExt.handleTextPopup(screenResult.Texts)
}
const (
CloseStatusFound = "found"
CloseStatusSuccess = "success"
CloseStatusFail = "fail"
)
// ClosePopupsResult represents the result of recognized popup to close
type ClosePopupsResult struct {
Type string `json:"type"`
@@ -89,80 +88,123 @@ type ClosePopupsResult struct {
}
type PopupInfo struct {
CloseStatus string `json:"close_status"`
Type string `json:"type"`
Text string `json:"text"`
RetryCount int `json:"retry_count"`
PicName string `json:"pic_name"`
PicURL string `json:"pic_url"`
PopupArea Box `json:"popup_area"`
CloseArea Box `json:"close_area"`
*ClosePopupsResult
CloseStatus string `json:"close_status"` // found/success/fail
ClosePoints []PointF `json:"close_points,omitempty"` // CV 识别的所有关闭按钮(仅关闭按钮,可能存在多个)
RetryCount int `json:"retry_count"`
PicName string `json:"pic_name"`
PicURL string `json:"pic_url"`
}
func (dExt *DriverExt) ClosePopups(options ...ActionOption) error {
actionOptions := NewActionOptions(options...)
func (p *PopupInfo) getClosePoint(lastPopup *PopupInfo) (*PointF, error) {
closeResult := p.ClosePopupsResult
if closeResult == nil {
return nil, nil
}
// default to retry 5 times
if actionOptions.MaxRetryTimes == 0 {
options = append(options, WithMaxRetryTimes(5))
// 弹框不存在 && 关闭按钮不存在
if closeResult.PopupArea.IsEmpty() && closeResult.CloseArea.IsEmpty() {
if p.ClosePoints == nil {
// 关闭图标不存在 => 100% 确定不存在弹窗
return nil, nil
}
// 存在关闭按钮,结合上一次的 popup 进行判断
if lastPopup == nil || lastPopup.ClosePoints == nil {
// 当前关闭图标为首次出现,确定是弹窗关闭按钮的概率较小
log.Debug().Interface("closePoints", p.ClosePoints).
Msg("skip close points for the first time")
return nil, nil
}
// 连续两次都存在关闭图标
if p.ClosePoints[0].IsIdentical(lastPopup.ClosePoints[0]) {
// 连续两次图标位置相同 => 存在弹窗 => 点击关闭
log.Warn().
Interface("closePoint", p.ClosePoints[0]).
Interface("lastClosePoints", lastPopup.ClosePoints).
Msg("popup close point detected")
return getRandomClosePoint(p.ClosePoints), nil
}
// 连续两次图标位置不同 => 可能不是弹窗 => skip
log.Debug().Interface("closePoints", p.ClosePoints).
Interface("lastClosePoints", lastPopup.ClosePoints).
Msg("skip close points for not sure")
return nil, nil
}
// set default swipe interval to 1 second
if builtin.IsZeroFloat64(actionOptions.Interval) {
options = append(options, WithInterval(1))
// 弹窗存在 && 关闭按钮不存在
if !closeResult.PopupArea.IsEmpty() && closeResult.CloseArea.IsEmpty() {
if p.ClosePoints == nil {
// 关闭图标不存在 => 无法处理,抛异常
log.Error().Interface("popup", p).Msg("popup close area not found")
return nil, errors.Wrap(code.MobileUIPopupError, "popup close area not found")
}
// 使用关闭图标作为关闭按钮(随机选择一个)
return getRandomClosePoint(p.ClosePoints), nil
}
// 关闭按钮存在 && (弹框存在 || 不存在)
if closeResult.Type != "" || p.ClosePoints == nil {
// 弹窗类型存在 || 关闭图标不存在 => 基于关闭按钮关闭弹窗
closePoint := closeResult.CloseArea.Center()
return &closePoint, nil
} else {
// 弹窗类型不存在 && 关闭图标存在,使用关闭图标作为关闭按钮(随机选择一个)
return getRandomClosePoint(p.ClosePoints), nil
}
return dExt.ClosePopupsHandler(options...)
}
func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error {
func getRandomClosePoint(closePoints []PointF) *PointF {
if len(closePoints) == 1 {
return &closePoints[0]
}
return &closePoints[rand.Intn(len(closePoints))]
}
func (dExt *DriverExt) ClosePopupsHandler() (err error) {
log.Info().Msg("try to find and close popups")
actionOptions := NewActionOptions(options...)
maxRetryTimes := actionOptions.MaxRetryTimes
interval := actionOptions.Interval
for retryCount := 0; retryCount < maxRetryTimes; retryCount++ {
screenResult, err := dExt.GetScreenResult(
WithScreenShotClosePopups(true), WithScreenShotUpload(true))
if err != nil {
log.Error().Err(err).Msg("get screen result failed for popup handler")
continue
}
// 1. there are no popups here (fast return normally)
// 2. failed to close popup maybe tap error, return error
// 3. successful to close popup (sleep and wait for next retry if existed)
if screenResult.Popup == nil {
break
}
screenResult.Popup.RetryCount = retryCount
if !screenResult.Popup.PopupArea.IsEmpty() {
screenResult.Popup.CloseStatus = CloseStatusFound
}
if screenResult.Popup.CloseArea.IsEmpty() {
break
}
screenResult.Popup.CloseStatus = CloseStatusFound
if err = dExt.tapPopupHandler(screenResult.Popup); err != nil {
return err
}
// sleep for another popup (if existed) to pop
time.Sleep(time.Duration(1000*interval) * time.Millisecond)
screenResult, err := dExt.GetScreenResult(
WithScreenShotUpload(true),
WithScreenShotClosePopups(true), // get popup area and close area
WithScreenShotUITypes("close"), // get all close buttons
)
if err != nil {
log.Error().Err(err).Msg("get screen result failed for popup handler")
return err
}
return nil
}
func (dExt *DriverExt) tapPopupHandler(popup *PopupInfo) error {
if popup == nil {
popup := screenResult.Popup
defer func() {
dExt.lastPopup = popup
}()
closePoint, err := popup.getClosePoint(dExt.lastPopup)
if err != nil {
return err
}
if closePoint == nil {
// close point not found
log.Debug().Msg("close point not found")
return nil
}
if popup.CloseArea.IsEmpty() {
return nil
}
log.Info().Str("type", popup.Type).Str("text", popup.Text).Msg("close popup")
popupCenter := popup.CloseArea.Center()
if err := dExt.TapAbsXY(popupCenter.X, popupCenter.Y); err != nil {
popup.CloseStatus = CloseStatusFound
log.Info().
Interface("closePoint", closePoint).
Interface("popup", popup).
Msg("tap to close popup")
if err := dExt.TapAbsXY(closePoint.X, closePoint.Y); err != nil {
log.Error().Err(err).Msg("tap popup failed")
return errors.Wrap(code.MobileUIPopupError, err.Error())
}
// tap popup success
// wait 1s and check if popup still exists
log.Info().Msg("tap close point success, check if popup still exists")
return nil
}

View File

@@ -67,10 +67,10 @@ type ImageResult struct {
// Media媒体
// Chat语音
// Event赛事
LiveType string `json:"liveType,omitempty"` // 直播间类型
LivePopularity int64 `json:"livePopularity,omitempty"` // 直播间热度
UIResult UIResultMap `json:"uiResult,omitempty"` // 图标检测
CPResult *ClosePopupsResult `json:"closeResult,omitempty"` // 弹窗按钮检测
LiveType string `json:"liveType,omitempty"` // 直播间类型
LivePopularity int64 `json:"livePopularity,omitempty"` // 直播间热度
UIResult UIResultMap `json:"uiResult,omitempty"` // 图标检测
ClosePopupsResult *ClosePopupsResult `json:"closeResult,omitempty"` // 弹窗按钮检测
}
type APIResponseImage struct {
@@ -410,7 +410,7 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
imageResult, err := dExt.ImageService.GetImage(bufSource, options...)
if err != nil {
log.Error().Err(err).Msg("GetImage from ImageService failed")
return nil, err
return screenResult, err
}
if imageResult != nil {
screenResult.imageResult = imageResult
@@ -420,14 +420,16 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
screenResult.Icons = imageResult.UIResult
screenResult.Video = &Video{LiveType: imageResult.LiveType, ViewCount: imageResult.LivePopularity}
if actionOptions.ScreenShotWithClosePopups && imageResult.CPResult != nil {
if actionOptions.ScreenShotWithClosePopups && imageResult.ClosePopupsResult != nil {
screenResult.Popup = &PopupInfo{
Type: imageResult.CPResult.Type,
Text: imageResult.CPResult.Text,
PicName: imagePath,
PicURL: imageResult.URL,
PopupArea: imageResult.CPResult.PopupArea,
CloseArea: imageResult.CPResult.CloseArea,
ClosePopupsResult: imageResult.ClosePopupsResult,
PicName: imagePath,
PicURL: imageResult.URL,
}
closeAreas, _ := imageResult.UIResult.FilterUIResults([]string{"close"})
for _, closeArea := range closeAreas {
screenResult.Popup.ClosePoints = append(screenResult.Popup.ClosePoints, closeArea.Center())
}
}
}
@@ -491,6 +493,7 @@ func (box Box) IsEmpty() bool {
}
func (box Box) IsIdentical(box2 Box) bool {
// set the coordinate precision to 1 pixel
return box.Point.IsIdentical(box2.Point) &&
builtin.IsZeroFloat64(math.Abs(box.Width-box2.Width)) &&
builtin.IsZeroFloat64(math.Abs(box.Height-box2.Height))

View File

@@ -102,21 +102,7 @@ func TestDriverExtOCR(t *testing.T) {
func TestClosePopup(t *testing.T) {
setupAndroid(t)
screenResult, err := driverExt.GetScreenResult(
WithScreenShotClosePopups(true), WithScreenShotUpload(true))
if err != nil {
t.Logf("get screen result failed for popup handler: %v", err)
return
}
t.Logf("screen result: %v", screenResult)
if screenResult.Popup == nil {
t.Log("there are no popups here")
return
}
t.Logf("popup info: %v", screenResult.Popup)
if err = driverExt.tapPopupHandler(screenResult.Popup); err != nil {
if err := driverExt.ClosePopupsHandler(); err != nil {
t.Fatal(err)
}
}

View File

@@ -11,10 +11,28 @@ import (
"github.com/httprunner/httprunner/v4/hrp/internal/code"
)
var directionSlice = [][]float64{
{0.85, 0.83, 0.85, 0.1},
{0.9, 0.75, 0.9, 0.1},
{0.6, 0.5, 0.6, 0.1},
}
func assertRelative(p float64) bool {
return p >= 0 && p <= 1
}
func (dExt *DriverExt) SwipeUpUtil(count int64, options ...ActionOption) error {
width := dExt.windowSize.Width
height := dExt.windowSize.Height
fromX := float64(width) * directionSlice[count%3][0]
fromY := float64(height) * directionSlice[count%3][1]
toX := float64(width) * directionSlice[count%3][2]
toY := float64(height) * directionSlice[count%3][3]
return dExt.Driver.SwipeFloat(fromX, fromY, toX, toY, options...)
}
// SwipeRelative swipe from relative position [fromX, fromY] to relative position [toX, toY]
func (dExt *DriverExt) SwipeRelative(fromX, fromY, toX, toY float64, options ...ActionOption) error {
width := dExt.windowSize.Width
@@ -156,17 +174,17 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption)
screenResult, err := d.GetScreenResult(
WithScreenShotOCR(true),
WithScreenShotUpload(true),
WithScreenShotClosePopups(true),
)
if err != nil {
return err
}
points, err := screenResult.Texts.FindTexts(texts, dExt.ParseActionOptions(optionsWithoutIdentifier...)...)
points, err := screenResult.Texts.FindTexts(texts,
dExt.ParseActionOptions(optionsWithoutIdentifier...)...)
if err != nil {
log.Error().Err(err).Msg("swipeToTapTexts failed")
log.Error().Err(err).Strs("texts", texts).Msg("find texts failed")
// target texts not found, try to auto handle popup
if e := dExt.tapPopupHandler(screenResult.Popup); e != nil {
log.Error().Err(e).Msg("auto handle popup failed")
if e := dExt.ClosePopupsHandler(); e != nil {
log.Error().Err(e).Msg("run popup handler failed")
}
return err
}
@@ -192,7 +210,7 @@ func (dExt *DriverExt) swipeToTapApp(appName string, options ...ActionOption) er
}
// automatic handling popups before swipe
if err := dExt.ClosePopups(); err != nil {
if err := dExt.ClosePopupsHandler(); err != nil {
log.Error().Err(err).Msg("auto handle popup failed")
}

View File

@@ -2,6 +2,7 @@ package uixt
import (
"math"
"math/rand"
"time"
"github.com/pkg/errors"
@@ -113,7 +114,12 @@ func (vc *VideoCrawler) isTargetAchieved() bool {
func (vc *VideoCrawler) exitLiveRoom() error {
log.Info().Msg("press back to exit live room")
return vc.driverExt.Driver.PressBack()
err := vc.driverExt.Driver.PressBack()
time.Sleep(time.Duration(3) * time.Second)
if vc.driverExt.TapByOCR("退出直播间") == nil {
log.Info().Msg("clicked the button to exit the live room successfully")
}
return err
}
const (
@@ -148,6 +154,9 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
dExt.cacheStepData.videoCrawler = crawler
}()
// flag仅当 flag 为 false 时,并处于内流时,才执行退出直播间逻辑
isFeed := true
// loop until target count achieved or timeout
// the main loop is feed crawler
crawler.timer = time.NewTimer(time.Duration(configs.Timeout) * time.Second)
@@ -160,65 +169,64 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
log.Warn().Msg("interrupted in feed crawler")
return errors.Wrap(code.InterruptError, "feed crawler interrupted")
default:
if err = crawler.clearCurrentVideo(); err != nil {
log.Error().Err(err).Msg("clear cache failed")
}
// swipe to next feed video
log.Info().Msg("swipe to next feed video")
swipeStartTime := time.Now()
if err = dExt.SwipeRelative(0.9, 0.8, 0.9, 0.1, WithOffsetRandomRange(-10, 10)); err != nil {
if err = dExt.SwipeUpUtil(crawler.failedCount, WithOffsetRandomRange(-10, 10)); err != nil {
log.Error().Err(err).Msg("feed swipe up failed")
return err
}
swipeFinishTime := time.Now()
// get app event trackings
// retry 10 times if get feed failed, abort if fail 10 consecutive times
feedVideo, err := crawler.getCurrentVideo()
if err != nil || feedVideo.Type == "" {
if crawler.failedCount >= 10 {
// failed 10 consecutive times
// retry 3 times if get feed failed, abort if fail 3 consecutive times
fetchVideoStartTime := time.Now()
currentVideo, err := crawler.getCurrentVideo()
if err != nil || currentVideo.Type == "" {
crawler.failedCount++
if crawler.failedCount >= 3 {
// failed 3 consecutive times
return errors.Wrap(code.TrackingGetError,
"get current feed video failed 10 consecutive times")
"get current feed video failed 3 consecutive times")
}
log.Warn().Msg("get current feed video failed")
log.Warn().
Int64("failedCount", crawler.failedCount).
Msg("get current feed video failed")
// check and handle popups
if err := crawler.driverExt.ClosePopupsHandler(WithMaxRetryTimes(3)); err != nil {
if err := crawler.driverExt.ClosePopupsHandler(); err != nil {
return err
}
// retry
crawler.failedCount++
continue
}
fetchVideoFinishTime := time.Now()
screenResult := &ScreenResult{
Resolution: dExt.windowSize,
Video: feedVideo,
// 直播预览流线上概率
livePreviewProb := crawler.getLivePreviewProb()
// log swipe timelines
SwipeStartTime: swipeStartTime.UnixMilli(),
SwipeFinishTime: swipeFinishTime.UnixMilli(),
}
switch feedVideo.Type {
switch currentVideo.Type {
case VideoType_PreviewLive:
isFeed = true
// 直播预览流
var skipEnterLive bool
if crawler.isLiveTargetAchieved() {
// 达标后不再进入直播间
crawler.LiveCount++
dExt.cacheStepData.screenResults[time.Now().String()] = screenResult
// 观播时长取随机时长与仿真时长的最小值
sleepTime := math.Min(float64(feedVideo.SimulationPlayDuration), float64(feedVideo.RandomPlayDuration))
feedVideo.PlayDuration = int64(sleepTime)
log.Info().
Strs("tags", screenResult.Tags).
Interface("video", feedVideo).
Msg(FOUND_LIVE_SUCCESS)
// simulation watch feed video
sleepStrict(swipeFinishTime, feedVideo.PlayDuration)
break
} else {
log.Info().Interface("video", currentVideo).
Msg("live count achieved, skip entering live room")
skipEnterLive = true
} else if rand.Float64() <= livePreviewProb {
log.Info().Interface("livePreviewProb", livePreviewProb).Msg("skip entering preview")
skipEnterLive = true
}
if !skipEnterLive {
time.Sleep(1 * time.Second)
// live target not achieved, enter live
// enter live room
entryPoint := PointF{
X: float64(dExt.windowSize.Width / 2),
Y: float64(dExt.windowSize.Height / 2),
@@ -230,53 +238,64 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
log.Error().Err(err).Msg("tap live video failed")
continue
}
currentVideo.Type = VideoType_Live
} else {
// skip entering live room
// only mock simulation play duration
sleepTime := math.Min(float64(currentVideo.SimulationPlayDuration), float64(currentVideo.RandomPlayDuration))
currentVideo.PlayDuration = int64(sleepTime)
}
fallthrough
case VideoType_Live:
// 直播
log.Info().
Strs("tags", screenResult.Tags).
Interface("video", feedVideo).
Msg(FOUND_LIVE_SUCCESS)
crawler.LiveCount++
log.Info().Interface("video", currentVideo).Msg(FOUND_LIVE_SUCCESS)
// wait 3s for live loading
time.Sleep(3 * time.Second)
// take screenshot and get screen texts by OCR
screenResultFromOCR, err := crawler.driverExt.GetScreenResult(
screenResult, err := crawler.driverExt.GetScreenResult(
WithScreenShotOCR(true),
WithScreenShotUpload(true),
WithScreenShotLiveType(true),
WithScreenShotClosePopups(true),
)
if err != nil {
log.Error().Err(err).Msg("get screen result failed")
time.Sleep(3 * time.Second)
continue
}
if e := crawler.driverExt.tapPopupHandler(screenResultFromOCR.Popup); e != nil {
log.Error().Err(e).Msg("auto handle popup failed")
continue
}
// add live type
if screenResultFromOCR.imageResult != nil &&
screenResultFromOCR.imageResult.LiveType != "" &&
screenResultFromOCR.imageResult.LiveType != "NoLive" {
screenResult.Video.LiveType = screenResultFromOCR.imageResult.LiveType
if screenResult.imageResult != nil &&
screenResult.imageResult.LiveType != "" &&
screenResult.imageResult.LiveType != "NoLive" {
currentVideo.LiveType = screenResult.imageResult.LiveType
}
crawler.LiveCount++
// simulation watch feed video
sleepStrict(swipeFinishTime, screenResult.Video.PlayDuration)
// simulation watch live video
simulationPlayDuration := math.Min(float64(currentVideo.PlayDuration), 300000)
sleepStrict(swipeFinishTime, int64(simulationPlayDuration))
screenResultFromOCR.Video = screenResult.Video
screenResultFromOCR.Resolution = screenResult.Resolution
screenResultFromOCR.SwipeStartTime = screenResult.SwipeStartTime
screenResultFromOCR.SwipeFinishTime = screenResult.SwipeFinishTime
screenResultFromOCR.TotalElapsed = time.Since(swipeFinishTime).Milliseconds()
screenResult.Video = currentVideo
screenResult.Resolution = dExt.windowSize
screenResult.SwipeStartTime = swipeStartTime.UnixMilli()
screenResult.SwipeFinishTime = swipeFinishTime.UnixMilli()
screenResult.TotalElapsed = time.Since(swipeFinishTime).Milliseconds()
screenResult.FetchVideoStartTime = fetchVideoStartTime.UnixMilli()
screenResult.FetchVideoFinishTime = fetchVideoFinishTime.UnixMilli()
screenResult.FetchVideoElapsed = fetchVideoFinishTime.Sub(fetchVideoStartTime).Milliseconds()
var exitLive bool
if crawler.isLiveTargetAchieved() {
log.Info().Interface("live", screenResult.Video).
Msg("live count achieved, exit live house")
log.Info().Interface("live", currentVideo).
Msg("live count achieved, exit live room")
exitLive = true
} else if rand.Float64() <= livePreviewProb {
log.Info().Interface("livePreviewProb", livePreviewProb).Msg("exit live room by preview live chance")
exitLive = true
}
// isFeed通过预览流进入内流失败的情况下防止使用退出直播间逻辑影响首次进入内流至少会消费两个直播间才能退出
if !isFeed && exitLive && currentVideo.Type == VideoType_Live {
err = crawler.exitLiveRoom()
if err != nil {
if errors.Is(err, code.TimeoutError) || errors.Is(err, code.InterruptError) {
@@ -284,21 +303,34 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
}
log.Error().Err(err).Msg("run live crawler failed, continue")
}
} else {
isFeed = false
}
default:
isFeed = true
// 点播 || 图文 || 广告 || etc.
crawler.FeedCount++
log.Info().Interface("video", currentVideo).Msg(FOUND_FEED_SUCCESS)
screenResult := &ScreenResult{
Resolution: dExt.windowSize,
Video: currentVideo,
// log swipe timelines
SwipeStartTime: swipeStartTime.UnixMilli(),
SwipeFinishTime: swipeFinishTime.UnixMilli(),
FetchVideoStartTime: fetchVideoStartTime.UnixMilli(),
FetchVideoFinishTime: fetchVideoFinishTime.UnixMilli(),
FetchVideoElapsed: fetchVideoFinishTime.Sub(fetchVideoStartTime).Milliseconds(),
}
dExt.cacheStepData.screenResults[time.Now().String()] = screenResult
log.Info().
Strs("tags", screenResult.Tags).
Interface("video", feedVideo).
Msg(FOUND_FEED_SUCCESS)
// simulation watch feed video
sleepStrict(swipeFinishTime, screenResult.Video.PlayDuration)
simulationPlayDuration := math.Min(float64(currentVideo.PlayDuration), 600000)
sleepStrict(swipeFinishTime, int64(simulationPlayDuration))
screenResult.TotalElapsed = time.Since(swipeFinishTime).Milliseconds()
}
screenResult.TotalElapsed = time.Since(swipeFinishTime).Milliseconds()
// check if target count achieved
if crawler.isTargetAchieved() {
@@ -332,6 +364,9 @@ type Video struct {
UserName string `json:"user_name"` // 视频作者
Duration int64 `json:"duration,omitempty"` // 视频时长(ms)
Caption string `json:"caption,omitempty"` // 视频文案
// 作者信息
UserID string `json:"user_id"` // 作者用户名
FollowerCount int64 `json:"follower_count"` // 作者粉丝数
// 视频热度数据
ViewCount int64 `json:"view_count,omitempty"` // feed 观看数
LikeCount int64 `json:"like_count,omitempty"` // feed 点赞数
@@ -363,6 +398,19 @@ type Video struct {
RandomPlayDuration int64 `json:"random_play_duration"` // 随机播放时长(ms)
}
func (vc *VideoCrawler) clearCurrentVideo() error {
if !vc.driverExt.plugin.Has("ClearCurrentVideo") {
return errors.New("plugin missing ClearCurrentVideo method")
}
_, err := vc.driverExt.plugin.Call("ClearCurrentVideo")
if err != nil {
return errors.Wrap(err, "call plugin ClearCurrentVideo failed")
}
return nil
}
func (vc *VideoCrawler) getCurrentVideo() (video *Video, err error) {
if !vc.driverExt.plugin.Has("GetCurrentVideo") {
return nil, errors.New("plugin missing GetCurrentVideo method")
@@ -407,3 +455,12 @@ func (vc *VideoCrawler) getCurrentVideo() (video *Video, err error) {
Msg("get current video success")
return video, nil
}
func (vc *VideoCrawler) getLivePreviewProb() float64 {
if vc.driverExt.Device.System() == "ios" {
return 0.5326
} else if vc.driverExt.Device.System() == "android" {
return 0.3414
}
return -1
}

View File

@@ -240,6 +240,13 @@ func (r *HRPRunner) Run(testcases ...ITestCase) (err error) {
}
}()
if caseRunner.parsedConfig.PluginSetting != nil {
if p, ok := pluginMap.Load(caseRunner.parsedConfig.PluginSetting.Path); ok {
log.Info().Msg(fmt.Sprintf("starting to keep live, path: %v, address: %v", caseRunner.parsedConfig.PluginSetting.Path, p))
go p.(funplugin.IPlugin).StartHeartbeat()
}
}
for it := caseRunner.parametersIterator; it.HasNext(); {
// case runner can run multiple times with different parameters
// each run has its own session runner

View File

@@ -1,7 +1,6 @@
package hrp
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -31,8 +30,8 @@ func removeHashicorpGoPlugin() {
func buildHashicorpPyPlugin() {
log.Info().Msg("[init] prepare hashicorp python plugin")
src, _ := ioutil.ReadFile(tmpl("plugin/debugtalk.py"))
err := ioutil.WriteFile(tmpl("debugtalk.py"), src, 0o644)
src, _ := os.ReadFile(tmpl("plugin/debugtalk.py"))
err := os.WriteFile(tmpl("debugtalk.py"), src, 0o644)
if err != nil {
log.Error().Err(err).Msg("copy hashicorp python plugin failed")
os.Exit(code.GetErrorCode(err))
@@ -156,6 +155,27 @@ func TestRunCaseWithThinkTime(t *testing.T) {
}
}
func TestRunCaseWithShell(t *testing.T) {
testcase1 := &TestCase{
Config: NewConfig("complex shell with env variables").
WithVariables(map[string]interface{}{
"SS": "12345",
"ABC": "$SS",
}),
TestSteps: []IStep{
NewStep("shell21").Shell("echo hello world"),
// NewStep("shell21").Shell("echo $ABC"),
// NewStep("shell21").Shell("which hrp"),
},
}
r := NewRunner(t)
err := r.Run(testcase1)
if err != nil {
t.Fatal()
}
}
func TestRunCaseWithPluginJSON(t *testing.T) {
buildHashicorpGoPlugin()
defer removeHashicorpGoPlugin()

View File

@@ -12,6 +12,10 @@ const (
stepTypeWebSocket StepType = "websocket"
stepTypeAndroid StepType = "android"
stepTypeIOS StepType = "ios"
stepTypeShell StepType = "shell"
stepTypeSuffixExtraction StepType = "_extraction"
stepTypeSuffixValidation StepType = "_validation"
)
type StepResult struct {
@@ -41,6 +45,7 @@ type TStep struct {
WebSocket *WebSocketAction `json:"websocket,omitempty" yaml:"websocket,omitempty"`
Android *MobileStep `json:"android,omitempty" yaml:"android,omitempty"`
IOS *MobileStep `json:"ios,omitempty" yaml:"ios,omitempty"`
Shell *Shell `json:"shell,omitempty" yaml:"shell,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"`
SetupHooks []string `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"`
TeardownHooks []string `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"`

View File

@@ -531,7 +531,10 @@ func (s *StepMobileUIValidation) Name() string {
}
func (s *StepMobileUIValidation) Type() StepType {
return stepTypeIOS
if s.step.Android != nil {
return stepTypeAndroid + stepTypeSuffixValidation
}
return stepTypeIOS + stepTypeSuffixValidation
}
func (s *StepMobileUIValidation) Struct() *TStep {

View File

@@ -769,6 +769,18 @@ func (s *StepRequest) IOS() *StepMobile {
}
}
// Shell creates a new shell action
func (s *StepRequest) Shell(content string) *StepShell {
s.step.Shell = &Shell{
String: content,
ExpectExitCode: 0,
}
return &StepShell{
step: s.step,
}
}
// StepRequestWithOptionalArgs implements IStep interface.
type StepRequestWithOptionalArgs struct {
step *TStep
@@ -904,13 +916,13 @@ func (s *StepRequestExtraction) Name() string {
}
func (s *StepRequestExtraction) Type() StepType {
if s.step.Request != nil {
return StepType(fmt.Sprintf("request-%v", s.step.Request.Method))
}
var stepType StepType
if s.step.WebSocket != nil {
return StepType(fmt.Sprintf("websocket-%v", s.step.WebSocket.Type))
stepType = StepType(fmt.Sprintf("websocket-%v", s.step.WebSocket.Type))
} else {
stepType = StepType(fmt.Sprintf("request-%v", s.step.Request.Method))
}
return "extraction"
return stepType + stepTypeSuffixExtraction
}
func (s *StepRequestExtraction) Struct() *TStep {
@@ -940,13 +952,13 @@ func (s *StepRequestValidation) Name() string {
}
func (s *StepRequestValidation) Type() StepType {
if s.step.Request != nil {
return StepType(fmt.Sprintf("request-%v", s.step.Request.Method))
}
var stepType StepType
if s.step.WebSocket != nil {
return StepType(fmt.Sprintf("websocket-%v", s.step.WebSocket.Type))
stepType = StepType(fmt.Sprintf("websocket-%v", s.step.WebSocket.Type))
} else {
stepType = StepType(fmt.Sprintf("request-%v", s.step.Request.Method))
}
return "validation"
return stepType + stepTypeSuffixValidation
}
func (s *StepRequestValidation) Struct() *TStep {

119
hrp/step_shell.go Normal file
View File

@@ -0,0 +1,119 @@
package hrp
import (
"fmt"
"os"
"time"
"github.com/httprunner/funplugin/myexec"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
type Shell struct {
String string `json:"string" yaml:"string"`
ExpectExitCode int `json:"expect_exit_code" yaml:"expect_exit_code"`
}
// StepShell implements IStep interface.
type StepShell struct {
step *TStep
}
func (s *StepShell) Name() string {
return s.step.Name
}
func (s *StepShell) Type() StepType {
return stepTypeShell
}
func (s *StepShell) Struct() *TStep {
return s.step
}
func (s *StepShell) Run(r *SessionRunner) (*StepResult, error) {
return runStepShell(r, s.step)
}
// Validate switches to step validation.
func (s *StepShell) Validate() *StepShellValidation {
return &StepShellValidation{
step: s.step,
}
}
// StepShellValidation implements IStep interface.
type StepShellValidation struct {
step *TStep
}
func (s *StepShellValidation) Name() string {
return s.step.Name
}
func (s *StepShellValidation) Type() StepType {
return stepTypeShell + stepTypeSuffixValidation
}
func (s *StepShellValidation) Struct() *TStep {
return s.step
}
func (s *StepShellValidation) Run(r *SessionRunner) (*StepResult, error) {
return runStepShell(r, s.step)
}
func (s *StepShellValidation) AssertExitCode(expected int) *StepShellValidation {
s.step.Shell.ExpectExitCode = expected
return s
}
func runStepShell(r *SessionRunner, step *TStep) (stepResult *StepResult, err error) {
shell := step.Shell
log.Info().
Str("name", step.Name).
Str("type", string(stepTypeShell)).
Str("content", shell.String).
Msg("run shell string")
stepResult = &StepResult{
Name: step.Name,
StepType: stepTypeShell,
Success: false,
Elapsed: 0,
ContentSize: 0,
}
vars := r.caseRunner.parsedConfig.Variables
for key, value := range vars {
os.Setenv(key, fmt.Sprintf("%v", value))
}
start := time.Now()
exitCode, err := myexec.RunShell(shell.String)
stepResult.Elapsed = time.Since(start).Milliseconds()
if err != nil {
if exitCode == shell.ExpectExitCode {
// get expected error
log.Warn().Err(err).
Int("exitCode", exitCode).
Msg("get expected error, ignore")
stepResult.Success = true
return stepResult, nil
}
err = errors.Wrap(err, "exec shell string failed")
return
}
// validate response
if exitCode != shell.ExpectExitCode {
err = fmt.Errorf("unexpected exit code %d, expect %d",
exitCode, shell.ExpectExitCode)
return
}
stepResult.Success = true
return stepResult, nil
}

View File

@@ -282,6 +282,10 @@ func (tc *TCase) toTestCase() (*TestCase, error) {
testCase.TestSteps = append(testCase.TestSteps, &StepMobile{
step: step,
})
} else if step.Shell != nil {
testCase.TestSteps = append(testCase.TestSteps, &StepShell{
step: step,
})
} else {
log.Warn().Interface("step", step).Msg("[convertTestCase] unexpected step")
}