From c012107847fa181066366fba58da4b3717700488 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 23 Nov 2022 18:05:35 +0800 Subject: [PATCH 01/22] change: filter non-usb ios devices --- hrp/cmd/ios/devices.go | 8 +++++--- hrp/pkg/uixt/ios_device.go | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/hrp/cmd/ios/devices.go b/hrp/cmd/ios/devices.go index bc9724a44..c1f230d3b 100644 --- a/hrp/cmd/ios/devices.go +++ b/hrp/cmd/ios/devices.go @@ -85,11 +85,13 @@ var listDevicesCmd = &cobra.Command{ } for _, d := range devices { - deviceByte, _ := json.Marshal(d.Properties()) + deviceProperties := d.Properties() device := &Device{ - d: d, + d: d, + UDID: deviceProperties.SerialNumber, + ConnectionType: deviceProperties.ConnectionType, + ConnectionSpeed: deviceProperties.ConnectionSpeed, } - json.Unmarshal(deviceByte, device) device.Status = device.GetStatus() if isDetail { diff --git a/hrp/pkg/uixt/ios_device.go b/hrp/pkg/uixt/ios_device.go index ff318ea21..1cb0c8203 100644 --- a/hrp/pkg/uixt/ios_device.go +++ b/hrp/pkg/uixt/ios_device.go @@ -124,6 +124,10 @@ func IOSDevices(udid ...string) (devices []gidevice.Device, err error) { if u != "" && u != d.Properties().SerialNumber { continue } + // filter non-usb ios devices + if d.Properties().ConnectionType != "USB" { + continue + } deviceList = append(deviceList, d) } } From 14424680af2d14293253f3f4b9169497b293bd33 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 23 Nov 2022 20:33:30 +0800 Subject: [PATCH 02/22] change: add image version --- hrp/cmd/ios/mount.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hrp/cmd/ios/mount.go b/hrp/cmd/ios/mount.go index d17d34039..cb077ae3c 100644 --- a/hrp/cmd/ios/mount.go +++ b/hrp/cmd/ios/mount.go @@ -62,12 +62,13 @@ var mountCmd = &cobra.Command{ dmgPath = filepath.Join(developerDiskImageDir, version, "DeveloperDiskImage.dmg") signaturePath = filepath.Join(developerDiskImageDir, version, "DeveloperDiskImage.dmg.signature") } else { - log.Error().Str("dir", developerDiskImageDir).Msg("developer disk image not found in directory") - return fmt.Errorf("developer disk image not found") + log.Error().Str("dir", developerDiskImageDir).Msgf( + "developer disk image %s not found in directory", version) + return fmt.Errorf("developer disk image %s not found", version) } if err = device.MountDeveloperDiskImage(dmgPath, signaturePath); err != nil { - return fmt.Errorf("mount developer disk image failed: %s", err) + return fmt.Errorf("mount developer disk image %s failed: %s", version, err) } log.Info().Msg("mount developer disk image successfully") From f34027a63c29b053185cdfd8227970f5c009354a Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 23 Nov 2022 20:49:44 +0800 Subject: [PATCH 03/22] feat: get all ocr texts --- hrp/pkg/uixt/ocr_vedem.go | 130 ++++++++++++++++++++++++-------------- 1 file changed, 81 insertions(+), 49 deletions(-) diff --git a/hrp/pkg/uixt/ocr_vedem.go b/hrp/pkg/uixt/ocr_vedem.go index 1bcc96add..542fd711e 100644 --- a/hrp/pkg/uixt/ocr_vedem.go +++ b/hrp/pkg/uixt/ocr_vedem.go @@ -147,8 +147,23 @@ func getLogID(header http.Header) string { return logID[0] } -func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...DataOption) (rect image.Rectangle, err error) { - data := NewData(map[string]interface{}{}, options...) +type OCRText struct { + Text string + Rect image.Rectangle +} + +type OCRTexts []OCRText + +func (t OCRTexts) Texts() []string { + var texts []string + for _, text := range t { + texts = append(texts, text.Text) + } + return texts +} + +func (s *veDEMOCRService) GetAllTexts(imageBuf []byte) ( + ocrTexts OCRTexts, err error) { ocrResults, err := s.getOCRResult(imageBuf) if err != nil { @@ -156,10 +171,8 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data return } - var rects []image.Rectangle - var ocrTexts []string for _, ocrResult := range ocrResults { - rect = image.Rectangle{ + rect := image.Rectangle{ // ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下 Min: image.Point{ X: int(ocrResult.Points[0].X), @@ -170,31 +183,57 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data Y: int(ocrResult.Points[2].Y), }, } - if rect.Min.X >= data.Scope[0] && rect.Max.X <= data.Scope[2] && rect.Min.Y >= data.Scope[1] && rect.Max.Y <= data.Scope[3] { - ocrTexts = append(ocrTexts, ocrResult.Text) + ocrTexts = append(ocrTexts, OCRText{ + Text: ocrResult.Text, + Rect: rect, + }) + } + return +} - // not contains text - if !strings.Contains(ocrResult.Text, text) { - continue - } +func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...DataOption) ( + rect image.Rectangle, err error) { - rects = append(rects, rect) + ocrTexts, err := s.GetAllTexts(imageBuf) + if err != nil { + log.Error().Err(err).Msg("GetAllTexts failed") + return + } - // contains text while not match exactly - if ocrResult.Text != text { - continue - } + data := NewData(map[string]interface{}{}, options...) - // match exactly, and not specify index, return the first one - if data.Index == 0 { - return rect, nil - } + var rects []image.Rectangle + for _, ocrText := range ocrTexts { + rect = ocrText.Rect + + // check if text in scope + if rect.Min.X < data.Scope[0] || rect.Max.X > data.Scope[2] || + rect.Min.Y < data.Scope[1] || rect.Max.Y > data.Scope[3] { + // not in scope + continue + } + + // not contains text + if !strings.Contains(ocrText.Text, text) { + continue + } + + rects = append(rects, rect) + + // contains text while not match exactly + if ocrText.Text != text { + continue + } + + // match exactly, and not specify index, return the first one + if data.Index == 0 { + return rect, nil } } if len(rects) == 0 { return image.Rectangle{}, errors.Wrap(code.OCRTextNotFoundError, - fmt.Sprintf("text %s not found in %v", text, ocrTexts)) + fmt.Sprintf("text %s not found in %v", text, ocrTexts.Texts())) } // get index @@ -215,45 +254,38 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data return rects[idx], nil } -func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...DataOption) (rects []image.Rectangle, err error) { - ocrResults, err := s.getOCRResult(imageBuf) +func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...DataOption) ( + rects []image.Rectangle, err error) { + + ocrTexts, err := s.GetAllTexts(imageBuf) if err != nil { - log.Error().Err(err).Msg("getOCRResult failed") + log.Error().Err(err).Msg("GetAllTexts failed") return } data := NewData(map[string]interface{}{}, options...) - ocrTexts := map[string]bool{} var success bool - var rect image.Rectangle for _, text := range texts { var found bool - for _, ocrResult := range ocrResults { - rect = image.Rectangle{ - // ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下 - Min: image.Point{ - X: int(ocrResult.Points[0].X), - Y: int(ocrResult.Points[0].Y), - }, - Max: image.Point{ - X: int(ocrResult.Points[2].X), - Y: int(ocrResult.Points[2].Y), - }, - } - - if rect.Min.X >= data.Scope[0] && rect.Max.X <= data.Scope[2] && rect.Min.Y >= data.Scope[1] && rect.Max.Y <= data.Scope[3] { - ocrTexts[ocrResult.Text] = true + for _, ocrText := range ocrTexts { + rect := ocrText.Rect - // not contains text - if !strings.Contains(ocrResult.Text, text) { - continue - } + // check if text in scope + if rect.Min.X < data.Scope[0] || rect.Max.X > data.Scope[2] || + rect.Min.Y < data.Scope[1] || rect.Max.Y > data.Scope[3] { + // not in scope + continue + } - found = true - rects = append(rects, rect) - break + // not contains text + if !strings.Contains(ocrText.Text, text) { + continue } + + found = true + rects = append(rects, rect) + break } if !found { rects = append(rects, image.Rectangle{}) @@ -263,7 +295,7 @@ func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ... if !success { return rects, errors.Wrap(code.OCRTextNotFoundError, - fmt.Sprintf("texts %s not found in %v", texts, ocrTexts)) + fmt.Sprintf("texts %s not found in %v", texts, ocrTexts.Texts())) } return rects, nil From 9888cea3e54d0ee92cf4649443b0b12c842c97e7 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 23 Nov 2022 23:22:51 +0800 Subject: [PATCH 04/22] refactor: NewDataOptions and NewData --- hrp/pkg/uixt/android_driver.go | 24 +++++++------- hrp/pkg/uixt/android_elment.go | 4 +-- hrp/pkg/uixt/demo/main_test.go | 28 +++++++++++++++++ hrp/pkg/uixt/ext.go | 5 +++ hrp/pkg/uixt/interface.go | 39 +++++++++++++---------- hrp/pkg/uixt/ios_driver.go | 16 +++++----- hrp/pkg/uixt/ios_element.go | 4 +-- hrp/pkg/uixt/ocr_vedem.go | 57 ++++++++++++++++++++-------------- hrp/pkg/uixt/swipe.go | 12 +++---- hrp/pkg/uixt/tap.go | 12 +++---- 10 files changed, 125 insertions(+), 76 deletions(-) diff --git a/hrp/pkg/uixt/android_driver.go b/hrp/pkg/uixt/android_driver.go index 5ebf69dde..3bbc37c4a 100644 --- a/hrp/pkg/uixt/android_driver.go +++ b/hrp/pkg/uixt/android_driver.go @@ -508,9 +508,9 @@ func (ud *uiaDriver) TapFloat(x, y float64, options ...DataOption) (err error) { "y": y, } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = ud.httpPOST(d.Data, "/session", ud.sessionId, "appium/tap") + _, err = ud.httpPOST(newData, "/session", ud.sessionId, "appium/tap") return } @@ -566,9 +566,9 @@ func (ud *uiaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOp } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - return ud._drag(d.Data) + return ud._drag(newData) } func (ud *uiaDriver) _swipe(startX, startY, endX, endY interface{}, options ...DataOption) (err error) { @@ -581,9 +581,9 @@ func (ud *uiaDriver) _swipe(startX, startY, endX, endY interface{}, options ...D } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = ud.httpPOST(d.Data, "/session", ud.sessionId, "touch/perform") + _, err = ud.httpPOST(newData, "/session", ud.sessionId, "touch/perform") return } @@ -672,9 +672,9 @@ func (ud *uiaDriver) SendKeys(text string, options ...DataOption) (err error) { "text": text, } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = ud.httpPOST(d.Data, "/session", ud.sessionId, "keys") + _, err = ud.httpPOST(newData, "/session", ud.sessionId, "keys") return } @@ -683,14 +683,14 @@ func (ud *uiaDriver) Input(text string, options ...DataOption) (err error) { "view": text, } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) var element WebElement - if valuetext, ok := d.Data["textview"]; ok { + if valuetext, ok := newData["textview"]; ok { element, err = ud.FindElement(BySelector{UiAutomator: NewUiSelectorHelper().TextContains(fmt.Sprintf("%v", valuetext)).String()}) - } else if valueid, ok := d.Data["id"]; ok { + } else if valueid, ok := newData["id"]; ok { element, err = ud.FindElement(BySelector{ResourceIdID: fmt.Sprintf("%v", valueid)}) - } else if valuedesc, ok := d.Data["description"]; ok { + } else if valuedesc, ok := newData["description"]; ok { element, err = ud.FindElement(BySelector{UiAutomator: NewUiSelectorHelper().Description(fmt.Sprintf("%v", valuedesc)).String()}) } else { element, err = ud.FindElement(BySelector{ClassName: ElementType{EditText: true}}) diff --git a/hrp/pkg/uixt/android_elment.go b/hrp/pkg/uixt/android_elment.go index 9d45cd397..760f52abe 100644 --- a/hrp/pkg/uixt/android_elment.go +++ b/hrp/pkg/uixt/android_elment.go @@ -30,9 +30,9 @@ func (ue uiaElement) SendKeys(text string, options ...DataOption) (err error) { } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = ue.parent.httpPOST(d.Data, "/session", ue.parent.sessionId, "/element", ue.id, "/value") + _, err = ue.parent.httpPOST(newData, "/session", ue.parent.sessionId, "/element", ue.id, "/value") return } diff --git a/hrp/pkg/uixt/demo/main_test.go b/hrp/pkg/uixt/demo/main_test.go index 1ff036cef..de1698144 100644 --- a/hrp/pkg/uixt/demo/main_test.go +++ b/hrp/pkg/uixt/demo/main_test.go @@ -3,6 +3,7 @@ package demo import ( + "fmt" "testing" "time" @@ -44,3 +45,30 @@ func TestIOSDemo(t *testing.T) { } } } + +func TestIOSGetOCRTexts(t *testing.T) { + device, err := uixt.NewIOSDevice( + uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), + uixt.WithResetHomeOnStartup(false), // not reset home on startup + ) + if err != nil { + t.Fatal(err) + } + + capabilities := uixt.NewCapabilities() + capabilities.WithDefaultAlertAction(uixt.AlertActionAccept) // or uixt.AlertActionDismiss + driverExt, err := device.NewDriver(capabilities) + if err != nil { + t.Fatal(err) + } + + for { + texts, err := driverExt.GetTextsByOCR() + if err != nil { + t.Fatal(err) + } + + fmt.Println(texts) + time.Sleep(3 * time.Second) + } +} diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index 531f95b8b..88f6ab50f 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -205,6 +205,7 @@ type DriverExt struct { frame *bytes.Buffer doneMjpegStream chan bool scale float64 + ocrService OCRService // used to get text from image StartTime time.Time // used to associate screenshots name ScreenShots []string // save screenshots path perfStop chan struct{} // stop performance monitor @@ -227,6 +228,10 @@ func extend(driver WebDriver) (dExt *DriverExt, err error) { return nil, err } + if dExt.ocrService, err = newVEDEMOCRService(); err != nil { + return nil, err + } + return dExt, nil } diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index 77ae84cbc..5dfba32a6 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -860,13 +860,8 @@ func WithDataWaitTime(sec float64) DataOption { } } -func NewData(data map[string]interface{}, options ...DataOption) *DataOptions { - if data == nil { - data = make(map[string]interface{}) - } - dataOptions := &DataOptions{ - Data: data, - } +func NewDataOptions(options ...DataOption) *DataOptions { + dataOptions := &DataOptions{} for _, option := range options { option(dataOptions) } @@ -874,7 +869,18 @@ func NewData(data map[string]interface{}, options ...DataOption) *DataOptions { if len(dataOptions.Scope) == 0 { dataOptions.Scope = []int{0, 0, math.MaxInt64, math.MaxInt64} // default scope } + return dataOptions +} + +func NewData(data map[string]interface{}, options ...DataOption) map[string]interface{} { + dataOptions := NewDataOptions(options...) + + // merge with data options + for k, v := range dataOptions.Data { + data[k] = v + } + // handle point offset if len(dataOptions.Offset) == 2 { if x, ok := data["x"]; ok { xf, _ := builtin.Interface2Float64(x) @@ -886,23 +892,24 @@ func NewData(data map[string]interface{}, options ...DataOption) *DataOptions { } } - if _, ok := dataOptions.Data["steps"]; !ok { - dataOptions.Data["steps"] = 12 // default steps + // add default options + if _, ok := data["steps"]; !ok { + data["steps"] = 12 // default steps } - if _, ok := dataOptions.Data["duration"]; !ok { - dataOptions.Data["duration"] = 0 // default duration + if _, ok := data["duration"]; !ok { + data["duration"] = 0 // default duration } - if _, ok := dataOptions.Data["frequency"]; !ok { - dataOptions.Data["frequency"] = 60 // default frequency + if _, ok := data["frequency"]; !ok { + data["frequency"] = 60 // default frequency } - if _, ok := dataOptions.Data["isReplace"]; !ok { - dataOptions.Data["isReplace"] = true // default true + if _, ok := data["isReplace"]; !ok { + data["isReplace"] = true // default true } - return dataOptions + return data } // current implemeted device: IOSDevice, AndroidDevice diff --git a/hrp/pkg/uixt/ios_driver.go b/hrp/pkg/uixt/ios_driver.go index a14c08c8a..03f3e2135 100644 --- a/hrp/pkg/uixt/ios_driver.go +++ b/hrp/pkg/uixt/ios_driver.go @@ -381,9 +381,9 @@ func (wd *wdaDriver) TapFloat(x, y float64, options ...DataOption) (err error) { "y": y, } // new data options in post data for extra WDA configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/tap/0") + _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/tap/0") return } @@ -433,9 +433,9 @@ func (wd *wdaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOp } // new data options in post data for extra WDA configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/dragfromtoforduration") + _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/dragfromtoforduration") return } @@ -506,9 +506,9 @@ func (wd *wdaDriver) SendKeys(text string, options ...DataOption) (err error) { data := map[string]interface{}{"value": strings.Split(text, "")} // new data options in post data for extra WDA configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/keys") + _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/keys") return } @@ -541,9 +541,9 @@ func (wd *wdaDriver) PressBack(options ...DataOption) (err error) { } // new data options in post data for extra WDA configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/dragfromtoforduration") + _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/dragfromtoforduration") return } diff --git a/hrp/pkg/uixt/ios_element.go b/hrp/pkg/uixt/ios_element.go index 9e2208b5e..02f54aacb 100644 --- a/hrp/pkg/uixt/ios_element.go +++ b/hrp/pkg/uixt/ios_element.go @@ -31,9 +31,9 @@ func (we wdaElement) SendKeys(text string, options ...DataOption) (err error) { "value": strings.Split(text, ""), } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = we.parent.httpPOST(d.Data, "/session", we.parent.sessionId, "/element", we.id, "/value") + _, err = we.parent.httpPOST(newData, "/session", we.parent.sessionId, "/element", we.id, "/value") return } diff --git a/hrp/pkg/uixt/ocr_vedem.go b/hrp/pkg/uixt/ocr_vedem.go index 542fd711e..06a6dec1f 100644 --- a/hrp/pkg/uixt/ocr_vedem.go +++ b/hrp/pkg/uixt/ocr_vedem.go @@ -154,15 +154,14 @@ type OCRText struct { type OCRTexts []OCRText -func (t OCRTexts) Texts() []string { - var texts []string +func (t OCRTexts) Texts() (texts []string) { for _, text := range t { texts = append(texts, text.Text) } return texts } -func (s *veDEMOCRService) GetAllTexts(imageBuf []byte) ( +func (s *veDEMOCRService) GetTexts(imageBuf []byte, options ...DataOption) ( ocrTexts OCRTexts, err error) { ocrResults, err := s.getOCRResult(imageBuf) @@ -194,21 +193,21 @@ func (s *veDEMOCRService) GetAllTexts(imageBuf []byte) ( func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...DataOption) ( rect image.Rectangle, err error) { - ocrTexts, err := s.GetAllTexts(imageBuf) + ocrTexts, err := s.GetTexts(imageBuf) if err != nil { - log.Error().Err(err).Msg("GetAllTexts failed") + log.Error().Err(err).Msg("GetTexts failed") return } - data := NewData(map[string]interface{}{}, options...) + dataOptions := NewDataOptions(options...) var rects []image.Rectangle for _, ocrText := range ocrTexts { rect = ocrText.Rect // check if text in scope - if rect.Min.X < data.Scope[0] || rect.Max.X > data.Scope[2] || - rect.Min.Y < data.Scope[1] || rect.Max.Y > data.Scope[3] { + if rect.Min.X < dataOptions.Scope[0] || rect.Max.X > dataOptions.Scope[2] || + rect.Min.Y < dataOptions.Scope[1] || rect.Max.Y > dataOptions.Scope[3] { // not in scope continue } @@ -226,7 +225,7 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data } // match exactly, and not specify index, return the first one - if data.Index == 0 { + if dataOptions.Index == 0 { return rect, nil } } @@ -237,7 +236,7 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data } // get index - idx := data.Index + idx := dataOptions.Index if idx > 0 { // NOTICE: index start from 1 idx = idx - 1 @@ -257,13 +256,13 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...DataOption) ( rects []image.Rectangle, err error) { - ocrTexts, err := s.GetAllTexts(imageBuf) + ocrTexts, err := s.GetTexts(imageBuf) if err != nil { - log.Error().Err(err).Msg("GetAllTexts failed") + log.Error().Err(err).Msg("GetTexts failed") return } - data := NewData(map[string]interface{}{}, options...) + dataOptions := NewDataOptions(options...) var success bool for _, text := range texts { @@ -272,8 +271,8 @@ func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ... rect := ocrText.Rect // check if text in scope - if rect.Min.X < data.Scope[0] || rect.Max.X > data.Scope[2] || - rect.Min.Y < data.Scope[1] || rect.Max.Y > data.Scope[3] { + if rect.Min.X < dataOptions.Scope[0] || rect.Max.X > dataOptions.Scope[2] || + rect.Min.Y < dataOptions.Scope[1] || rect.Max.Y > dataOptions.Scope[3] { // not in scope continue } @@ -302,21 +301,35 @@ func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ... } type OCRService interface { - FindText(text string, imageBuf []byte, index ...int) (rect image.Rectangle, err error) + GetTexts(imageBuf []byte, options ...DataOption) (ocrTexts OCRTexts, err error) + FindText(text string, imageBuf []byte, options ...DataOption) (rect image.Rectangle, err error) + FindTexts(texts []string, imageBuf []byte, options ...DataOption) (rects []image.Rectangle, err error) } -func (dExt *DriverExt) FindTextByOCR(ocrText string, options ...DataOption) (x, y, width, height float64, err error) { +func (dExt *DriverExt) GetTextsByOCR() (texts OCRTexts, err error) { var bufSource *bytes.Buffer if bufSource, err = dExt.takeScreenShot(); err != nil { err = fmt.Errorf("takeScreenShot error: %v", err) return } - service, err := newVEDEMOCRService() + ocrTexts, err := dExt.ocrService.GetTexts(bufSource.Bytes()) if err != nil { + log.Error().Err(err).Msg("GetTexts failed") + return + } + + return ocrTexts, nil +} + +func (dExt *DriverExt) FindTextByOCR(ocrText string, options ...DataOption) (x, y, width, height float64, err error) { + var bufSource *bytes.Buffer + if bufSource, err = dExt.takeScreenShot(); err != nil { + err = fmt.Errorf("takeScreenShot error: %v", err) return } - rect, err := service.FindText(ocrText, bufSource.Bytes(), options...) + + rect, err := dExt.ocrService.FindText(ocrText, bufSource.Bytes(), options...) if err != nil { log.Warn().Msgf("FindText failed: %s", err.Error()) return @@ -335,11 +348,7 @@ func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string, options ...DataOption) return } - service, err := newVEDEMOCRService() - if err != nil { - return - } - rects, err := service.FindTexts(ocrTexts, bufSource.Bytes(), options...) + rects, err := dExt.ocrService.FindTexts(ocrTexts, bufSource.Bytes(), options...) if err != nil { log.Warn().Msgf("FindTexts failed: %s", err.Error()) return diff --git a/hrp/pkg/uixt/swipe.go b/hrp/pkg/uixt/swipe.go index 4c4e865f2..b1f8f6d03 100644 --- a/hrp/pkg/uixt/swipe.go +++ b/hrp/pkg/uixt/swipe.go @@ -69,9 +69,9 @@ type Action func(driver *DriverExt) error // findCondition indicates the condition to find a UI element // foundAction indicates the action to do after a UI element is found func (dExt *DriverExt) SwipeUntil(direction interface{}, findCondition Action, foundAction Action, options ...DataOption) error { - d := NewData(nil, options...) - maxRetryTimes := d.MaxRetryTimes - interval := d.Interval + dataOptions := NewDataOptions(options...) + maxRetryTimes := dataOptions.MaxRetryTimes + interval := dataOptions.Interval for i := 0; i < maxRetryTimes; i++ { if err := findCondition(dExt); err == nil { @@ -103,9 +103,9 @@ func (dExt *DriverExt) SwipeUntil(direction interface{}, findCondition Action, f } func (dExt *DriverExt) LoopUntil(findAction, findCondition, foundAction Action, options ...DataOption) error { - d := NewData(nil, options...) - maxRetryTimes := d.MaxRetryTimes - interval := d.Interval + dataOptions := NewDataOptions(options...) + maxRetryTimes := dataOptions.MaxRetryTimes + interval := dataOptions.Interval for i := 0; i < maxRetryTimes; i++ { if err := findCondition(dExt); err == nil { diff --git a/hrp/pkg/uixt/tap.go b/hrp/pkg/uixt/tap.go index 1629f1379..3d4b584aa 100644 --- a/hrp/pkg/uixt/tap.go +++ b/hrp/pkg/uixt/tap.go @@ -65,11 +65,11 @@ func (dExt *DriverExt) GetImageXY(imagePath string, options ...DataOption) (poin } func (dExt *DriverExt) TapByOCR(ocrText string, options ...DataOption) error { - data := NewData(map[string]interface{}{}, options...) + dataOptions := NewDataOptions(options...) point, err := dExt.GetTextXY(ocrText, options...) if err != nil { - if data.IgnoreNotFoundError { + if dataOptions.IgnoreNotFoundError { return nil } return err @@ -79,11 +79,11 @@ func (dExt *DriverExt) TapByOCR(ocrText string, options ...DataOption) error { } func (dExt *DriverExt) TapByCV(imagePath string, options ...DataOption) error { - data := NewData(map[string]interface{}{}, options...) + dataOptions := NewDataOptions(options...) point, err := dExt.GetImageXY(imagePath, options...) if err != nil { - if data.IgnoreNotFoundError { + if dataOptions.IgnoreNotFoundError { return nil } return err @@ -103,11 +103,11 @@ func (dExt *DriverExt) TapOffset(param string, xOffset, yOffset float64, options return ele.Click() } - data := NewData(map[string]interface{}{}, options...) + dataOptions := NewDataOptions(options...) x, y, width, height, err := dExt.FindUIRectInUIKit(param, options...) if err != nil { - if data.IgnoreNotFoundError { + if dataOptions.IgnoreNotFoundError { return nil } return err From 5cb7218146d3493d285328f750ec88f464aa1285 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 23 Nov 2022 23:48:55 +0800 Subject: [PATCH 05/22] change: add options for FindTexts --- hrp/pkg/uixt/ocr_vedem.go | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/hrp/pkg/uixt/ocr_vedem.go b/hrp/pkg/uixt/ocr_vedem.go index 06a6dec1f..d4233b517 100644 --- a/hrp/pkg/uixt/ocr_vedem.go +++ b/hrp/pkg/uixt/ocr_vedem.go @@ -170,6 +170,8 @@ func (s *veDEMOCRService) GetTexts(imageBuf []byte, options ...DataOption) ( return } + dataOptions := NewDataOptions(options...) + for _, ocrResult := range ocrResults { rect := image.Rectangle{ // ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下 @@ -182,6 +184,14 @@ func (s *veDEMOCRService) GetTexts(imageBuf []byte, options ...DataOption) ( Y: int(ocrResult.Points[2].Y), }, } + + // check if text in scope + if rect.Min.X < dataOptions.Scope[0] || rect.Max.X > dataOptions.Scope[2] || + rect.Min.Y < dataOptions.Scope[1] || rect.Max.Y > dataOptions.Scope[3] { + // not in scope + continue + } + ocrTexts = append(ocrTexts, OCRText{ Text: ocrResult.Text, Rect: rect, @@ -193,7 +203,7 @@ func (s *veDEMOCRService) GetTexts(imageBuf []byte, options ...DataOption) ( func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...DataOption) ( rect image.Rectangle, err error) { - ocrTexts, err := s.GetTexts(imageBuf) + ocrTexts, err := s.GetTexts(imageBuf, options...) if err != nil { log.Error().Err(err).Msg("GetTexts failed") return @@ -205,13 +215,6 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data for _, ocrText := range ocrTexts { rect = ocrText.Rect - // check if text in scope - if rect.Min.X < dataOptions.Scope[0] || rect.Max.X > dataOptions.Scope[2] || - rect.Min.Y < dataOptions.Scope[1] || rect.Max.Y > dataOptions.Scope[3] { - // not in scope - continue - } - // not contains text if !strings.Contains(ocrText.Text, text) { continue @@ -256,27 +259,18 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...DataOption) ( rects []image.Rectangle, err error) { - ocrTexts, err := s.GetTexts(imageBuf) + ocrTexts, err := s.GetTexts(imageBuf, options...) if err != nil { log.Error().Err(err).Msg("GetTexts failed") return } - dataOptions := NewDataOptions(options...) - var success bool for _, text := range texts { var found bool for _, ocrText := range ocrTexts { rect := ocrText.Rect - // check if text in scope - if rect.Min.X < dataOptions.Scope[0] || rect.Max.X > dataOptions.Scope[2] || - rect.Min.Y < dataOptions.Scope[1] || rect.Max.Y > dataOptions.Scope[3] { - // not in scope - continue - } - // not contains text if !strings.Contains(ocrText.Text, text) { continue @@ -306,14 +300,14 @@ type OCRService interface { FindTexts(texts []string, imageBuf []byte, options ...DataOption) (rects []image.Rectangle, err error) } -func (dExt *DriverExt) GetTextsByOCR() (texts OCRTexts, err error) { +func (dExt *DriverExt) GetTextsByOCR(options ...DataOption) (texts OCRTexts, err error) { var bufSource *bytes.Buffer if bufSource, err = dExt.takeScreenShot(); err != nil { err = fmt.Errorf("takeScreenShot error: %v", err) return } - ocrTexts, err := dExt.ocrService.GetTexts(bufSource.Bytes()) + ocrTexts, err := dExt.ocrService.GetTexts(bufSource.Bytes(), options...) if err != nil { log.Error().Err(err).Msg("GetTexts failed") return From ce0959022bcc90ca5f6a451cfc52b3e6703a2c72 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 24 Nov 2022 20:23:09 +0800 Subject: [PATCH 06/22] feat: add option WithScreenShot --- hrp/pkg/uixt/demo/main_test.go | 28 ---------------------------- hrp/pkg/uixt/ext.go | 4 ++-- hrp/pkg/uixt/interface.go | 11 +++++++++++ hrp/pkg/uixt/ocr_vedem.go | 30 +++++++++++++++++++----------- hrp/pkg/uixt/ocr_vedem_test.go | 11 ++++++++--- 5 files changed, 40 insertions(+), 44 deletions(-) diff --git a/hrp/pkg/uixt/demo/main_test.go b/hrp/pkg/uixt/demo/main_test.go index de1698144..1ff036cef 100644 --- a/hrp/pkg/uixt/demo/main_test.go +++ b/hrp/pkg/uixt/demo/main_test.go @@ -3,7 +3,6 @@ package demo import ( - "fmt" "testing" "time" @@ -45,30 +44,3 @@ func TestIOSDemo(t *testing.T) { } } } - -func TestIOSGetOCRTexts(t *testing.T) { - device, err := uixt.NewIOSDevice( - uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), - uixt.WithResetHomeOnStartup(false), // not reset home on startup - ) - if err != nil { - t.Fatal(err) - } - - capabilities := uixt.NewCapabilities() - capabilities.WithDefaultAlertAction(uixt.AlertActionAccept) // or uixt.AlertActionDismiss - driverExt, err := device.NewDriver(capabilities) - if err != nil { - t.Fatal(err) - } - - for { - texts, err := driverExt.GetTextsByOCR() - if err != nil { - t.Fatal(err) - } - - fmt.Println(texts) - time.Sleep(3 * time.Second) - } -} diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index 88f6ab50f..b544cd19d 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -260,7 +260,7 @@ func (dExt *DriverExt) takeScreenShot() (raw *bytes.Buffer, err error) { } // saveScreenShot saves image file to $CWD/screenshots/ folder -func (dExt *DriverExt) saveScreenShot(raw *bytes.Buffer, fileName string) (string, error) { +func saveScreenShot(raw *bytes.Buffer, fileName string) (string, error) { img, format, err := image.Decode(raw) if err != nil { return "", errors.Wrap(err, "decode screenshot image failed") @@ -304,7 +304,7 @@ func (dExt *DriverExt) ScreenShot(fileName string) (string, error) { return "", errors.Wrap(err, "screenshot failed") } - path, err := dExt.saveScreenShot(raw, fileName) + path, err := saveScreenShot(raw, fileName) if err != nil { return "", errors.Wrap(err, "save screenshot failed") } diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index 5dfba32a6..cd837ff16 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -784,6 +784,7 @@ type DataOptions struct { IgnoreNotFoundError bool // ignore error if target element not found MaxRetryTimes int // max retry times if target element not found Interval float64 // interval between retries in seconds + ScreenShotFilename string // turn on screenshot and specify file name } type DataOption func(data *DataOptions) @@ -860,6 +861,16 @@ func WithDataWaitTime(sec float64) DataOption { } } +func WithScreenShot(fileName ...string) DataOption { + return func(data *DataOptions) { + if len(fileName) > 0 { + data.ScreenShotFilename = fileName[0] + } else { + data.ScreenShotFilename = fmt.Sprintf("screenshot_%d", time.Now().Unix()) + } + } +} + func NewDataOptions(options ...DataOption) *DataOptions { dataOptions := &DataOptions{} for _, option := range options { diff --git a/hrp/pkg/uixt/ocr_vedem.go b/hrp/pkg/uixt/ocr_vedem.go index d4233b517..9d1bd2291 100644 --- a/hrp/pkg/uixt/ocr_vedem.go +++ b/hrp/pkg/uixt/ocr_vedem.go @@ -56,7 +56,7 @@ func checkEnv() error { return nil } -func (s *veDEMOCRService) getOCRResult(imageBuf []byte) ([]OCRResult, error) { +func (s *veDEMOCRService) getOCRResult(imageBuf *bytes.Buffer) ([]OCRResult, error) { bodyBuf := &bytes.Buffer{} bodyWriter := multipart.NewWriter(bodyBuf) bodyWriter.WriteField("withDet", "true") @@ -67,7 +67,7 @@ func (s *veDEMOCRService) getOCRResult(imageBuf []byte) ([]OCRResult, error) { return nil, errors.Wrap(code.OCRRequestError, fmt.Sprintf("create form file error: %v", err)) } - size, err := formWriter.Write(imageBuf) + size, err := formWriter.Write(imageBuf.Bytes()) if err != nil { return nil, errors.Wrap(code.OCRRequestError, fmt.Sprintf("write form error: %v", err)) @@ -161,7 +161,7 @@ func (t OCRTexts) Texts() (texts []string) { return texts } -func (s *veDEMOCRService) GetTexts(imageBuf []byte, options ...DataOption) ( +func (s *veDEMOCRService) GetTexts(imageBuf *bytes.Buffer, options ...DataOption) ( ocrTexts OCRTexts, err error) { ocrResults, err := s.getOCRResult(imageBuf) @@ -172,6 +172,14 @@ func (s *veDEMOCRService) GetTexts(imageBuf []byte, options ...DataOption) ( dataOptions := NewDataOptions(options...) + if dataOptions.ScreenShotFilename != "" { + path, err := saveScreenShot(imageBuf, dataOptions.ScreenShotFilename) + if err != nil { + return nil, errors.Wrap(err, "save screenshot failed") + } + log.Info().Str("path", path).Msg("save screenshot") + } + for _, ocrResult := range ocrResults { rect := image.Rectangle{ // ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下 @@ -200,7 +208,7 @@ func (s *veDEMOCRService) GetTexts(imageBuf []byte, options ...DataOption) ( return } -func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...DataOption) ( +func (s *veDEMOCRService) FindText(text string, imageBuf *bytes.Buffer, options ...DataOption) ( rect image.Rectangle, err error) { ocrTexts, err := s.GetTexts(imageBuf, options...) @@ -256,7 +264,7 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data return rects[idx], nil } -func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...DataOption) ( +func (s *veDEMOCRService) FindTexts(texts []string, imageBuf *bytes.Buffer, options ...DataOption) ( rects []image.Rectangle, err error) { ocrTexts, err := s.GetTexts(imageBuf, options...) @@ -295,9 +303,9 @@ func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ... } type OCRService interface { - GetTexts(imageBuf []byte, options ...DataOption) (ocrTexts OCRTexts, err error) - FindText(text string, imageBuf []byte, options ...DataOption) (rect image.Rectangle, err error) - FindTexts(texts []string, imageBuf []byte, options ...DataOption) (rects []image.Rectangle, err error) + GetTexts(imageBuf *bytes.Buffer, options ...DataOption) (ocrTexts OCRTexts, err error) + FindText(text string, imageBuf *bytes.Buffer, options ...DataOption) (rect image.Rectangle, err error) + FindTexts(texts []string, imageBuf *bytes.Buffer, options ...DataOption) (rects []image.Rectangle, err error) } func (dExt *DriverExt) GetTextsByOCR(options ...DataOption) (texts OCRTexts, err error) { @@ -307,7 +315,7 @@ func (dExt *DriverExt) GetTextsByOCR(options ...DataOption) (texts OCRTexts, err return } - ocrTexts, err := dExt.ocrService.GetTexts(bufSource.Bytes(), options...) + ocrTexts, err := dExt.ocrService.GetTexts(bufSource, options...) if err != nil { log.Error().Err(err).Msg("GetTexts failed") return @@ -323,7 +331,7 @@ func (dExt *DriverExt) FindTextByOCR(ocrText string, options ...DataOption) (x, return } - rect, err := dExt.ocrService.FindText(ocrText, bufSource.Bytes(), options...) + rect, err := dExt.ocrService.FindText(ocrText, bufSource, options...) if err != nil { log.Warn().Msgf("FindText failed: %s", err.Error()) return @@ -342,7 +350,7 @@ func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string, options ...DataOption) return } - rects, err := dExt.ocrService.FindTexts(ocrTexts, bufSource.Bytes(), options...) + rects, err := dExt.ocrService.FindTexts(ocrTexts, bufSource, options...) if err != nil { log.Warn().Msgf("FindTexts failed: %s", err.Error()) return diff --git a/hrp/pkg/uixt/ocr_vedem_test.go b/hrp/pkg/uixt/ocr_vedem_test.go index 2bb650b7d..f3fe1d32b 100644 --- a/hrp/pkg/uixt/ocr_vedem_test.go +++ b/hrp/pkg/uixt/ocr_vedem_test.go @@ -3,12 +3,13 @@ package uixt import ( + "bytes" "fmt" "os" "testing" ) -func checkOCR(buff []byte) error { +func checkOCR(buff *bytes.Buffer) error { service, err := newVEDEMOCRService() if err != nil { return err @@ -33,19 +34,23 @@ func TestOCRWithScreenshot(t *testing.T) { t.Fatal(err) } - if err := checkOCR(raw.Bytes()); err != nil { + if err := checkOCR(raw); err != nil { t.Fatal(err) } } func TestOCRWithLocalFile(t *testing.T) { imagePath := "/Users/debugtalk/Downloads/s1.png" + file, err := os.ReadFile(imagePath) if err != nil { t.Fatal(err) } - if err := checkOCR(file); err != nil { + buf := new(bytes.Buffer) + buf.Read(file) + + if err := checkOCR(buf); err != nil { t.Fatal(err) } } From 1dee4131b0211019168fe592e2aedebfc46275de Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 24 Nov 2022 23:10:19 +0800 Subject: [PATCH 07/22] docs: add examples for API --- examples/worldcup/cli.go | 71 +++++++++++ examples/worldcup/main.go | 208 +++++++++++++++++++++++++++++++++ examples/worldcup/main_test.go | 31 +++++ hrp/pkg/uixt/ext.go | 17 ++- hrp/pkg/uixt/ocr_vedem.go | 2 +- 5 files changed, 319 insertions(+), 10 deletions(-) create mode 100644 examples/worldcup/cli.go create mode 100644 examples/worldcup/main.go create mode 100644 examples/worldcup/main_test.go diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go new file mode 100644 index 000000000..5ce17812e --- /dev/null +++ b/examples/worldcup/cli.go @@ -0,0 +1,71 @@ +package main + +import ( + "os" + "strings" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" +) + +var rootCmd = &cobra.Command{ + Use: "wcl", + Short: "Monitor FIFA World Cup Live", + Version: "0.1", + 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 { + wc := NewWorldCupLive(matchName, osType, duration, interval) + wc.Start() + wc.DumpResult() + return nil + }, +} + +var ( + uuid string + osType string + duration int + interval int + logLevel string + matchName 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().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") + + 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) + } +} diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go new file mode 100644 index 000000000..cde07c79a --- /dev/null +++ b/examples/worldcup/main.go @@ -0,0 +1,208 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "math" + "os" + "path/filepath" + "strconv" + "strings" + "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() uixt.Device { + device, err := uixt.NewIOSDevice( + uixt.WithUDID(uuid), + uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), + uixt.WithResetHomeOnStartup(false), // not reset home on startup + // uixt.WithPerfOptions(), + ) + if err != nil { + log.Fatal().Err(err).Msg("failed to init ios device") + } + return device +} + +func initAndroidDevice() 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 { + UTCTime int64 `json:"utc_time"` + LiveTime string `json:"live_time"` + LiveTimeSeconds int `json:"live_time_seconds"` +} + +type WorldCupLive struct { + driver *uixt.DriverExt + done chan bool + file *os.File + resultDir string + UUID string `json:"uuid"` + MatchName string `json:"matchName"` + StartTime string `json:"startTime"` + EndTime string `json:"endTime"` + Interval int `json:"interval"` // seconds + Duration int `json:"duration"` // seconds + Summary []timeLog `json:"summary"` +} + +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() + } + + 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("worldcup-archives", 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("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(), + 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 + } + + var liveTimeSeconds int + for _, ocrText := range ocrTexts { + seconds, err := convertTimeToSeconds(ocrText.Text) + if err == nil { + liveTimeSeconds = seconds + line := fmt.Sprintf("%s\t%d\t%s\t%d\n", + utcTimeStr, utcTime.Unix(), ocrText.Text, liveTimeSeconds) + fmt.Print(line) + if _, err := wc.file.WriteString(line); err != nil { + log.Error().Err(err).Str("line", line).Msg("write timeseries failed") + } + wc.Summary = append(wc.Summary, timeLog{ + UTCTime: utcTime.Unix(), + LiveTime: ocrText.Text, + LiveTimeSeconds: liveTimeSeconds, + }) + break + } + } + return nil +} + +func (wc *WorldCupLive) Start() { + wc.done = make(chan bool) + go func() { + for { + select { + case <-wc.done: + return + default: + utcTime := time.Now() + if utcTime.Unix()%int64(wc.Interval) == 0 { + wc.getCurrentLiveTime(utcTime) + } else { + time.Sleep(500 * time.Millisecond) + } + } + } + }() + time.Sleep(time.Duration(wc.Duration) * time.Second) + wc.Stop() +} + +func (wc *WorldCupLive) Stop() { + wc.EndTime = time.Now().Format("2006-01-02 15:04:05") + wc.done <- true +} + +func (wc *WorldCupLive) DumpResult() error { + // init json encoder + buffer := new(bytes.Buffer) + encoder := json.NewEncoder(buffer) + encoder.SetEscapeHTML(false) + encoder.SetIndent("", " ") + + err := encoder.Encode(wc) + if err != nil { + return err + } + + filename := filepath.Join(wc.resultDir, "summary.json") + err = os.WriteFile(filename, buffer.Bytes(), 0o755) + if err != nil { + log.Error().Err(err).Msg("dump json path failed") + return err + } + return nil +} diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go new file mode 100644 index 000000000..cbc75f4e0 --- /dev/null +++ b/examples/worldcup/main_test.go @@ -0,0 +1,31 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +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 TestMain(t *testing.T) { + main() +} diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index b544cd19d..903c3558b 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -259,21 +259,14 @@ func (dExt *DriverExt) takeScreenShot() (raw *bytes.Buffer, err error) { return raw, nil } -// saveScreenShot saves image file to $CWD/screenshots/ folder +// saveScreenShot saves image file with file name func saveScreenShot(raw *bytes.Buffer, fileName string) (string, error) { img, format, err := image.Decode(raw) if err != nil { return "", errors.Wrap(err, "decode screenshot image failed") } - dir, _ := os.Getwd() - screenshotsDir := filepath.Join(dir, "screenshots") - if err = os.MkdirAll(screenshotsDir, os.ModePerm); err != nil { - return "", errors.Wrap(err, "create screenshots directory failed") - } - screenshotPath := filepath.Join(screenshotsDir, - fmt.Sprintf("%s.%s", fileName, format)) - + screenshotPath := filepath.Join(fmt.Sprintf("%s.%s", fileName, format)) file, err := os.Create(screenshotPath) if err != nil { return "", errors.Wrap(err, "create screenshot image file failed") @@ -304,6 +297,12 @@ func (dExt *DriverExt) ScreenShot(fileName string) (string, error) { return "", errors.Wrap(err, "screenshot failed") } + dir, _ := os.Getwd() + screenshotsDir := filepath.Join(dir, "screenshots") + if err = os.MkdirAll(screenshotsDir, os.ModePerm); err != nil { + return "", errors.Wrap(err, "create screenshots directory failed") + } + fileName = filepath.Join(screenshotsDir, fileName) path, err := saveScreenShot(raw, fileName) if err != nil { return "", errors.Wrap(err, "save screenshot failed") diff --git a/hrp/pkg/uixt/ocr_vedem.go b/hrp/pkg/uixt/ocr_vedem.go index 9d1bd2291..03d8a2717 100644 --- a/hrp/pkg/uixt/ocr_vedem.go +++ b/hrp/pkg/uixt/ocr_vedem.go @@ -177,7 +177,7 @@ func (s *veDEMOCRService) GetTexts(imageBuf *bytes.Buffer, options ...DataOption if err != nil { return nil, errors.Wrap(err, "save screenshot failed") } - log.Info().Str("path", path).Msg("save screenshot") + log.Debug().Str("path", path).Msg("save screenshot") } for _, ocrResult := range ocrResults { From 037b7f0190d2a4bc00e5083004fce5c706ba1e41 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 25 Nov 2022 22:36:39 +0800 Subject: [PATCH 08/22] docs: add perf monitor in example --- examples/worldcup/cli.go | 3 +++ examples/worldcup/main.go | 28 ++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go index 5ce17812e..544ca2a35 100644 --- a/examples/worldcup/cli.go +++ b/examples/worldcup/cli.go @@ -35,6 +35,7 @@ var ( interval int logLevel string matchName string + perf []string ) func main() { @@ -44,6 +45,8 @@ func main() { 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 { diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index cde07c79a..d647f678b 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -13,6 +13,8 @@ import ( "github.com/rs/zerolog/log" + "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/gidevice" "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) @@ -35,11 +37,31 @@ func convertTimeToSeconds(timeStr string) (int, error) { } func initIOSDevice() uixt.Device { + perfOptions := []gidevice.PerfOption{} + for _, p := range perf { + switch p { + case "sys_cpu": + perfOptions = append(perfOptions, hrp.WithPerfSystemCPU(true)) + case "sys_mem": + perfOptions = append(perfOptions, hrp.WithPerfSystemMem(true)) + case "sys_net": + perfOptions = append(perfOptions, hrp.WithPerfSystemNetwork(true)) + case "sys_disk": + perfOptions = append(perfOptions, hrp.WithPerfSystemDisk(true)) + case "network": + perfOptions = append(perfOptions, hrp.WithPerfNetwork(true)) + case "fps": + perfOptions = append(perfOptions, hrp.WithPerfFPS(true)) + case "gpu": + perfOptions = append(perfOptions, hrp.WithPerfGPU(true)) + } + } + device, err := uixt.NewIOSDevice( uixt.WithUDID(uuid), uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), uixt.WithResetHomeOnStartup(false), // not reset home on startup - // uixt.WithPerfOptions(), + uixt.WithPerfOptions(perfOptions...), ) if err != nil { log.Fatal().Err(err).Msg("failed to init ios device") @@ -73,6 +95,7 @@ type WorldCupLive struct { Interval int `json:"interval"` // seconds Duration int `json:"duration"` // seconds Summary []timeLog `json:"summary"` + PerfData []string `json:"perfData"` } func NewWorldCupLive(matchName, osType string, duration, interval int) *WorldCupLive { @@ -145,7 +168,7 @@ func (wc *WorldCupLive) getCurrentLiveTime(utcTime time.Time) error { liveTimeSeconds = seconds line := fmt.Sprintf("%s\t%d\t%s\t%d\n", utcTimeStr, utcTime.Unix(), ocrText.Text, liveTimeSeconds) - fmt.Print(line) + log.Info().Str("utcTime", utcTimeStr).Str("liveTime", ocrText.Text).Msg("log live time") if _, err := wc.file.WriteString(line); err != nil { log.Error().Err(err).Str("line", line).Msg("write timeseries failed") } @@ -193,6 +216,7 @@ func (wc *WorldCupLive) DumpResult() error { encoder.SetEscapeHTML(false) encoder.SetIndent("", " ") + wc.PerfData = wc.driver.GetPerfData() err := encoder.Encode(wc) if err != nil { return err From 21b57ac70a4a4e3bf6a533356606f8c3da07ade4 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 26 Nov 2022 21:27:49 +0800 Subject: [PATCH 09/22] change: rename DeviceLogcat to AdbLogcat --- hrp/pkg/uixt/android_device.go | 20 +++++++++++--------- hrp/pkg/uixt/android_driver.go | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/hrp/pkg/uixt/android_device.go b/hrp/pkg/uixt/android_device.go index e86206c65..6391dfac9 100644 --- a/hrp/pkg/uixt/android_device.go +++ b/hrp/pkg/uixt/android_device.go @@ -68,6 +68,8 @@ func GetAndroidDeviceOptions(dev *AndroidDevice) (deviceOptions []AndroidDeviceO return } +// uiautomator2 server must be started before +// adb shell am instrument -w io.appium.uiautomator2.server.test/androidx.test.runner.AndroidJUnitRunner func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, err error) { deviceList, err := DeviceList() if err != nil { @@ -114,7 +116,7 @@ func DeviceList() (devices []gadb.Device, err error) { type AndroidDevice struct { d gadb.Device - logcat *DeviceLogcat + logcat *AdbLogcat SerialNumber string `json:"serial,omitempty" yaml:"serial,omitempty"` IP string `json:"ip,omitempty" yaml:"ip,omitempty"` Port int `json:"port,omitempty" yaml:"port,omitempty"` @@ -201,7 +203,7 @@ func getFreePort() (int, error) { return l.Addr().(*net.TCPAddr).Port, nil } -type DeviceLogcat struct { +type AdbLogcat struct { serial string logBuffer *bytes.Buffer errs []error @@ -210,8 +212,8 @@ type DeviceLogcat struct { cmd *exec.Cmd } -func NewAdbLogcat(serial string) *DeviceLogcat { - return &DeviceLogcat{ +func NewAdbLogcat(serial string) *AdbLogcat { + return &AdbLogcat{ serial: serial, logBuffer: new(bytes.Buffer), stopping: make(chan struct{}), @@ -220,7 +222,7 @@ func NewAdbLogcat(serial string) *DeviceLogcat { } // CatchLogcatContext starts logcat with timeout context -func (l *DeviceLogcat) CatchLogcatContext(timeoutCtx context.Context) (err error) { +func (l *AdbLogcat) CatchLogcatContext(timeoutCtx context.Context) (err error) { if err = l.CatchLogcat(); err != nil { return } @@ -234,7 +236,7 @@ func (l *DeviceLogcat) CatchLogcatContext(timeoutCtx context.Context) (err error return } -func (l *DeviceLogcat) Stop() error { +func (l *AdbLogcat) Stop() error { select { case <-l.stopping: default: @@ -245,7 +247,7 @@ func (l *DeviceLogcat) Stop() error { return l.Errors() } -func (l *DeviceLogcat) Errors() (err error) { +func (l *AdbLogcat) Errors() (err error) { for _, e := range l.errs { if err != nil { err = fmt.Errorf("%v |[DeviceLogcatErr] %v", err, e) @@ -256,7 +258,7 @@ func (l *DeviceLogcat) Errors() (err error) { return } -func (l *DeviceLogcat) CatchLogcat() (err error) { +func (l *AdbLogcat) CatchLogcat() (err error) { if l.cmd != nil { log.Warn().Msg("logcat already start") return nil @@ -284,7 +286,7 @@ func (l *DeviceLogcat) CatchLogcat() (err error) { return } -func (l *DeviceLogcat) BufferedLogcat() (err error) { +func (l *AdbLogcat) BufferedLogcat() (err error) { // -d: dump the current buffered logcat result and exits cmd := myexec.Command("adb", "-s", l.serial, "logcat", "-d") cmd.Stdout = l.logBuffer diff --git a/hrp/pkg/uixt/android_driver.go b/hrp/pkg/uixt/android_driver.go index 3bbc37c4a..31d43c074 100644 --- a/hrp/pkg/uixt/android_driver.go +++ b/hrp/pkg/uixt/android_driver.go @@ -35,7 +35,7 @@ type uiaDriver struct { Driver adbDevice gadb.Device - logcat *DeviceLogcat + logcat *AdbLogcat localPort int } @@ -478,7 +478,7 @@ func (ud *uiaDriver) AppTerminate(bundleId string) (successful bool, err error) return false, err } - _, err = ud.adbDevice.RunShellCommand("am force-stop", bundleId) + _, err = ud.adbDevice.RunShellCommand("am", "force-stop", bundleId) return err == nil, err } From 446e37236aa839ed577903e5c81978d93dd6f324 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 27 Nov 2022 00:16:02 +0800 Subject: [PATCH 10/22] 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 000000000..a8bdc6b49 --- /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 544ca2a35..69ca9601a 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 d647f678b..8cf6eaf3b 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 cbc75f4e0..d1ad2ecf1 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -1,10 +1,6 @@ -package main - -import ( - "testing" +//go:build localtest - "github.com/stretchr/testify/assert" -) +package main func TestConvertTimeToSeconds(t *testing.T) { testData := []struct { @@ -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 000000000..cd0709cdd --- /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 000000000..f23df857b --- /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) +} From c9bbc7788bc6cabc9f21e5f5f69e177d68fa9669 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 27 Nov 2022 20:32:20 +0800 Subject: [PATCH 11/22] change: perf monitor not set cpu/mem as default --- hrp/pkg/gidevice/perfd.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hrp/pkg/gidevice/perfd.go b/hrp/pkg/gidevice/perfd.go index 334e879e4..e33b1c17d 100644 --- a/hrp/pkg/gidevice/perfd.go +++ b/hrp/pkg/gidevice/perfd.go @@ -32,8 +32,8 @@ type PerfOptions struct { func defaulPerfOption() *PerfOptions { return &PerfOptions{ - SysCPU: true, // default on - SysMem: true, // default on + SysCPU: false, + SysMem: false, SysDisk: false, SysNetwork: false, gpu: false, From ecdbd3afce3396b29d68aabe994ee1671dbb33d3 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 27 Nov 2022 22:36:44 +0800 Subject: [PATCH 12/22] refactor: enter live --- examples/worldcup/README.md | 36 ++++++++++++++++++++++++++-------- examples/worldcup/cli.go | 6 +++--- examples/worldcup/main.go | 21 ++++++-------------- examples/worldcup/main_test.go | 4 ++-- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/examples/worldcup/README.md b/examples/worldcup/README.md index a8bdc6b49..ff9fc2fd9 100644 --- a/examples/worldcup/README.md +++ b/examples/worldcup/README.md @@ -1,15 +1,35 @@ # World Cup Live -```bash -$ wcl -n "法国vs丹麦" --android com.ss.android.ugc.aweme -d 120 -``` +```text +$ wcl -h +Monitor FIFA World Cup Live + +Usage: + wcl [flags] +Flags: + --android string run android app + -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 +``` -抖音: -- com.ss.iphone.ugc.Aweme -- com.ss.android.ugc.aweme +```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 | diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go index 69ca9601a..51a2bc43f 100644 --- a/examples/worldcup/cli.go +++ b/examples/worldcup/cli.go @@ -15,7 +15,7 @@ import ( var rootCmd = &cobra.Command{ Use: "wcl", Short: "Monitor FIFA World Cup Live", - Version: "0.1", + Version: "2022.11.27.2240", PreRun: func(cmd *cobra.Command, args []string) { log.Logger = zerolog.New( zerolog.ConsoleWriter{NoColor: false, Out: os.Stderr}, @@ -28,11 +28,11 @@ var rootCmd = &cobra.Command{ var bundleID string if iosApp != "" { log.Info().Str("bundleID", iosApp).Msg("init ios device") - device = initIOSDevice() + device = initIOSDevice(uuid) bundleID = iosApp } else if androidApp != "" { log.Info().Str("bundleID", androidApp).Msg("init android device") - device = initAndroidDevice() + device = initAndroidDevice(uuid) bundleID = androidApp } else { return errors.New("android or ios app bundldID is required") diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index 8cf6eaf3b..4b06a9724 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -36,7 +36,7 @@ func convertTimeToSeconds(timeStr string) (int, error) { return seconds, nil } -func initIOSDevice() uixt.Device { +func initIOSDevice(uuid string) uixt.Device { perfOptions := []gidevice.PerfOption{} for _, p := range perf { switch p { @@ -69,7 +69,7 @@ func initIOSDevice() uixt.Device { return device } -func initAndroidDevice() uixt.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") @@ -204,21 +204,12 @@ func (wc *WorldCupLive) EnterLive(bundleID string) error { _ = 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") + // 进入世界杯 tab + if err = wc.driver.TapByOCR("世界杯"); err != nil { + log.Error().Err(err).Msg("enter 直播中 failed") return err } - time.Sleep(2 * time.Second) + time.Sleep(3 * time.Second) // 进入世界杯直播 if err = wc.driver.TapByOCR("直播中"); err != nil { diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index d1ad2ecf1..cb02c51c2 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -23,14 +23,14 @@ func TestConvertTimeToSeconds(t *testing.T) { } func TestMainIOS(t *testing.T) { - device := initIOSDevice() + device := initIOSDevice(uuid) wc := NewWorldCupLive(device, "", "com.ss.iphone.ugc.Aweme", 30, 10) wc.Start() wc.DumpResult() } func TestMainAndroid(t *testing.T) { - device := initAndroidDevice() + device := initAndroidDevice(uuid) wc := NewWorldCupLive(device, "", "com.ss.android.ugc.aweme", 30, 10) wc.Start() wc.DumpResult() From 5aeb1960c77036aa74ad10912ddf4d0f642872ac Mon Sep 17 00:00:00 2001 From: debugtalk Date: Mon, 28 Nov 2022 18:10:58 +0800 Subject: [PATCH 13/22] docs: add auto for wcl --- examples/worldcup/README.md | 3 ++- examples/worldcup/cli.go | 7 +++++++ examples/worldcup/main.go | 12 ++++++------ examples/worldcup/main_test.go | 14 ++++++++++++-- hrp/cmd/ios/ios_test.go | 2 ++ 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/examples/worldcup/README.md b/examples/worldcup/README.md index ff9fc2fd9..62a2d5c49 100644 --- a/examples/worldcup/README.md +++ b/examples/worldcup/README.md @@ -9,6 +9,7 @@ Usage: 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) @@ -22,7 +23,7 @@ Flags: ```bash -$ wcl -n "法国vs丹麦" --android com.ss.android.ugc.aweme -d 300 -i 15 -u caf0cd51 +$ 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 ``` diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go index 51a2bc43f..d2431aa31 100644 --- a/examples/worldcup/cli.go +++ b/examples/worldcup/cli.go @@ -39,6 +39,11 @@ var rootCmd = &cobra.Command{ } wc := NewWorldCupLive(device, matchName, bundleID, duration, interval) + + if auto { + wc.EnterLive(bundleID) + } + wc.Start() wc.DumpResult() return nil @@ -49,6 +54,7 @@ var ( uuid string iosApp string androidApp string + auto bool duration int interval int logLevel string @@ -61,6 +67,7 @@ func main() { 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") diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index 4b06a9724..f49bc5c5f 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -133,7 +133,7 @@ func NewWorldCupLive(device uixt.Device, matchName, bundleID string, duration, i duration = 30 } - wc := &WorldCupLive{ + return &WorldCupLive{ driver: driverExt, file: f, resultDir: resultDir, @@ -144,11 +144,6 @@ func NewWorldCupLive(device uixt.Device, matchName, bundleID string, duration, i 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 { @@ -198,12 +193,17 @@ func (wc *WorldCupLive) EnterLive(bundleID string) error { 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) } + if err = wc.driver.SwipeRelative(0.5, 0.1, 0.8, 0.1); err != nil { + log.Error().Err(err).Msg("swipe failed") + } + // 进入世界杯 tab if err = wc.driver.TapByOCR("世界杯"); err != nil { log.Error().Err(err).Msg("enter 直播中 failed") diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index cb02c51c2..436799b50 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -2,6 +2,12 @@ package main +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + func TestConvertTimeToSeconds(t *testing.T) { testData := []struct { timeStr string @@ -24,14 +30,18 @@ func TestConvertTimeToSeconds(t *testing.T) { func TestMainIOS(t *testing.T) { device := initIOSDevice(uuid) - wc := NewWorldCupLive(device, "", "com.ss.iphone.ugc.Aweme", 30, 10) + bundleID := "com.ss.iphone.ugc.Aweme" + wc := NewWorldCupLive(device, "", bundleID, 30, 10) + wc.EnterLive(bundleID) wc.Start() wc.DumpResult() } func TestMainAndroid(t *testing.T) { device := initAndroidDevice(uuid) - wc := NewWorldCupLive(device, "", "com.ss.android.ugc.aweme", 30, 10) + bundleID := "com.ss.android.ugc.aweme" + wc := NewWorldCupLive(device, "", bundleID, 30, 10) + wc.EnterLive(bundleID) wc.Start() wc.DumpResult() } diff --git a/hrp/cmd/ios/ios_test.go b/hrp/cmd/ios/ios_test.go index cd0709cdd..43f20b7b4 100644 --- a/hrp/cmd/ios/ios_test.go +++ b/hrp/cmd/ios/ios_test.go @@ -2,6 +2,8 @@ package ios +import "testing" + func TestGetDevice(t *testing.T) { device, err := getDevice(udid) if err != nil { From 9d474d895e67c2599cc936c01ff39aa4d3d05a58 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 29 Nov 2022 21:59:52 +0800 Subject: [PATCH 14/22] fix: set perf monitor interval for Sysmontap --- examples/worldcup/main.go | 6 +++++- hrp/pkg/gidevice/perfd.go | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index f49bc5c5f..e770a9d28 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -56,6 +56,7 @@ func initIOSDevice(uuid string) uixt.Device { perfOptions = append(perfOptions, hrp.WithPerfGPU(true)) } } + perfOptions = append(perfOptions, hrp.WithPerfOutputInterval(interval*1000)) device, err := uixt.NewIOSDevice( uixt.WithUDID(uuid), @@ -78,6 +79,7 @@ func initAndroidDevice(uuid string) uixt.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"` @@ -111,7 +113,7 @@ func NewWorldCupLive(device uixt.Device, matchName, bundleID string, duration, i startTime := time.Now() matchName = fmt.Sprintf("%s-%s", startTime.Format("2006-01-02"), matchName) - resultDir := filepath.Join("worldcup-archives", matchName, startTime.Format("15:04:05")) + 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") @@ -168,6 +170,7 @@ func (wc *WorldCupLive) getCurrentLiveTime(utcTime time.Time) error { log.Error().Err(err).Str("line", line).Msg("write timeseries failed") } wc.Summary = append(wc.Summary, timeLog{ + UTCTimeStr: utcTimeStr, UTCTime: utcTime.Unix(), LiveTime: ocrText.Text, LiveTimeSeconds: liveTimeSeconds, @@ -256,6 +259,7 @@ func (wc *WorldCupLive) DumpResult() error { wc.PerfData = wc.driver.GetPerfData() err := encoder.Encode(wc) if err != nil { + log.Error().Err(err).Msg("encode json failed") return err } diff --git a/hrp/pkg/gidevice/perfd.go b/hrp/pkg/gidevice/perfd.go index e33b1c17d..f4f1a6ff5 100644 --- a/hrp/pkg/gidevice/perfd.go +++ b/hrp/pkg/gidevice/perfd.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "encoding/json" "fmt" + "log" "net" "strconv" "time" @@ -179,10 +180,13 @@ type perfdSysmontap struct { func (c *perfdSysmontap) Start() (data <-chan []byte, err error) { // set config + interval := time.Millisecond * time.Duration(c.options.OutputInterval) + log.Printf("set sysmontap sample interval: %dms\n", c.options.OutputInterval) + config := map[string]interface{}{ "bm": 0, "cpuUsage": true, - "sampleInterval": time.Second * 1, // 1s + "sampleInterval": interval, // time.Duration "ur": c.options.OutputInterval, // 输出频率 "procAttrs": c.options.ProcessAttributes, // process performance "sysAttrs": c.options.SystemAttributes, // system performance @@ -740,7 +744,7 @@ func (c *perfdGraphicsOpengl) Start() (data <-chan []byte, err error) { if _, err = c.i.call( instrumentsServiceGraphicsOpengl, "setSamplingRate:", - float64(c.options.OutputInterval)/100, + float64(c.options.OutputInterval)/100, // FIXME: unable to set sampling rate, always 1.0 ); err != nil { return nil, err } From 8affbe2e9c399150e77c9a2de80e6da2f819e626 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 29 Nov 2022 23:34:56 +0800 Subject: [PATCH 15/22] feat: catch kill signals --- examples/worldcup/cli.go | 1 - examples/worldcup/main.go | 47 +++++++++++++++++----------------- examples/worldcup/main_test.go | 2 -- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go index d2431aa31..d94854147 100644 --- a/examples/worldcup/cli.go +++ b/examples/worldcup/cli.go @@ -45,7 +45,6 @@ var rootCmd = &cobra.Command{ } wc.Start() - wc.DumpResult() return nil }, } diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index e770a9d28..9ec189a1f 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -6,9 +6,11 @@ import ( "fmt" "math" "os" + "os/signal" "path/filepath" "strconv" "strings" + "syscall" "time" "github.com/rs/zerolog/log" @@ -87,7 +89,6 @@ type timeLog struct { type WorldCupLive struct { driver *uixt.DriverExt - done chan bool file *os.File resultDir string UUID string `json:"uuid"` @@ -224,32 +225,31 @@ func (wc *WorldCupLive) EnterLive(bundleID string) error { } func (wc *WorldCupLive) Start() { - wc.done = make(chan bool) - go func() { - for { - select { - case <-wc.done: - return - default: - utcTime := time.Now() - if utcTime.Unix()%int64(wc.Interval) == 0 { - wc.getCurrentLiveTime(utcTime) - } else { - time.Sleep(500 * time.Millisecond) - } + 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) } } - }() - time.Sleep(time.Duration(wc.Duration) * time.Second) - wc.Stop() + } } -func (wc *WorldCupLive) Stop() { +func (wc *WorldCupLive) dumpResult() error { wc.EndTime = time.Now().Format("2006-01-02 15:04:05") - wc.done <- true -} -func (wc *WorldCupLive) DumpResult() error { // init json encoder buffer := new(bytes.Buffer) encoder := json.NewEncoder(buffer) @@ -263,11 +263,12 @@ func (wc *WorldCupLive) DumpResult() error { return err } - filename := filepath.Join(wc.resultDir, "summary.json") - err = os.WriteFile(filename, buffer.Bytes(), 0o755) + 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 } diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index 436799b50..36a53fd8a 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -34,7 +34,6 @@ func TestMainIOS(t *testing.T) { wc := NewWorldCupLive(device, "", bundleID, 30, 10) wc.EnterLive(bundleID) wc.Start() - wc.DumpResult() } func TestMainAndroid(t *testing.T) { @@ -43,5 +42,4 @@ func TestMainAndroid(t *testing.T) { wc := NewWorldCupLive(device, "", bundleID, 30, 10) wc.EnterLive(bundleID) wc.Start() - wc.DumpResult() } From 78349c717be77f13a2314eb225ab01ed8bebfec0 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 30 Nov 2022 23:42:28 +0800 Subject: [PATCH 16/22] docs: update wcl version --- examples/worldcup/cli.go | 2 +- examples/worldcup/main.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go index d94854147..e21972876 100644 --- a/examples/worldcup/cli.go +++ b/examples/worldcup/cli.go @@ -15,7 +15,7 @@ import ( var rootCmd = &cobra.Command{ Use: "wcl", Short: "Monitor FIFA World Cup Live", - Version: "2022.11.27.2240", + Version: "2022.11.30.2341", PreRun: func(cmd *cobra.Command, args []string) { log.Logger = zerolog.New( zerolog.ConsoleWriter{NoColor: false, Out: os.Stderr}, diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index 9ec189a1f..54549eba3 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -98,7 +98,7 @@ type WorldCupLive struct { EndTime string `json:"endTime"` Interval int `json:"interval"` // seconds Duration int `json:"duration"` // seconds - Summary []timeLog `json:"summary"` + Timelines []timeLog `json:"timelines"` PerfData []string `json:"perfData"` } @@ -170,7 +170,7 @@ func (wc *WorldCupLive) getCurrentLiveTime(utcTime time.Time) error { if _, err := wc.file.WriteString(line); err != nil { log.Error().Err(err).Str("line", line).Msg("write timeseries failed") } - wc.Summary = append(wc.Summary, timeLog{ + wc.Timelines = append(wc.Timelines, timeLog{ UTCTimeStr: utcTimeStr, UTCTime: utcTime.Unix(), LiveTime: ocrText.Text, From 77acfd2f0365335e515bdb736215ef9c5834e996 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 1 Dec 2022 22:34:34 +0800 Subject: [PATCH 17/22] feat: run xctest before start ios automation --- examples/worldcup/cli.go | 2 +- examples/worldcup/main.go | 1 + examples/worldcup/main_test.go | 1 + hrp/pkg/uixt/ios_device.go | 65 +++++++++++++++++++++++++++++----- 4 files changed, 59 insertions(+), 10 deletions(-) diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go index e21972876..4ca59b60e 100644 --- a/examples/worldcup/cli.go +++ b/examples/worldcup/cli.go @@ -15,7 +15,7 @@ import ( var rootCmd = &cobra.Command{ Use: "wcl", Short: "Monitor FIFA World Cup Live", - Version: "2022.11.30.2341", + Version: "2022.12.01.2236", PreRun: func(cmd *cobra.Command, args []string) { log.Logger = zerolog.New( zerolog.ConsoleWriter{NoColor: false, Out: os.Stderr}, diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index 54549eba3..3d38c6c99 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -65,6 +65,7 @@ func initIOSDevice(uuid string) uixt.Device { uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), uixt.WithResetHomeOnStartup(false), // not reset home on startup uixt.WithPerfOptions(perfOptions...), + uixt.WithXCTest("com.gtf.wda.runner.xctrunner"), ) if err != nil { log.Fatal().Err(err).Msg("failed to init ios device") diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index 36a53fd8a..0bb65a14b 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -29,6 +29,7 @@ func TestConvertTimeToSeconds(t *testing.T) { } func TestMainIOS(t *testing.T) { + uuid := "00008030-00194DA421C1802E" device := initIOSDevice(uuid) bundleID := "com.ss.iphone.ugc.Aweme" wc := NewWorldCupLive(device, "", bundleID, 30, 10) diff --git a/hrp/pkg/uixt/ios_device.go b/hrp/pkg/uixt/ios_device.go index 1cb0c8203..7dc105232 100644 --- a/hrp/pkg/uixt/ios_device.go +++ b/hrp/pkg/uixt/ios_device.go @@ -2,10 +2,12 @@ package uixt import ( "bytes" + "context" "encoding/base64" builtinJSON "encoding/json" "fmt" "io" + builtinLog "log" "mime" "mime/multipart" "net" @@ -96,6 +98,12 @@ func WithDismissAlertButtonSelector(selector string) IOSDeviceOption { } } +func WithXCTest(bundleID string) IOSDeviceOption { + return func(device *IOSDevice) { + device.XCTestBundleID = bundleID + } +} + func WithPerfOptions(options ...gidevice.PerfOption) IOSDeviceOption { return func(device *IOSDevice) { device.PerfOptions = &gidevice.PerfOptions{} @@ -186,10 +194,21 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { return nil, err } - if len(deviceList) > 0 { - device.UDID = deviceList[0].Properties().SerialNumber + for _, dev := range deviceList { + udid := dev.Properties().SerialNumber + device.UDID = udid + device.d = dev + + // run xctest if XCTestBundleID is set + if device.XCTestBundleID != "" { + _, err = device.RunXCTest(device.XCTestBundleID) + if err != nil { + log.Error().Err(err).Str("udid", udid).Msg("failed to init XCTest") + continue + } + } + log.Info().Str("udid", device.UDID).Msg("select device") - device.d = deviceList[0] return device, nil } @@ -198,12 +217,13 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { } type IOSDevice struct { - d gidevice.Device - PerfOptions *gidevice.PerfOptions `json:"perf_options,omitempty" yaml:"perf_options,omitempty"` - UDID string `json:"udid,omitempty" yaml:"udid,omitempty"` - Port int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port - MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` // WDA remote MJPEG port - LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` + d gidevice.Device + PerfOptions *gidevice.PerfOptions `json:"perf_options,omitempty" yaml:"perf_options,omitempty"` + UDID string `json:"udid,omitempty" yaml:"udid,omitempty"` + Port int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port + MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` // WDA remote MJPEG port + LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` + XCTestBundleID string `json:"xctest_bundle_id,omitempty" yaml:"xctest_bundle_id,omitempty"` // switch to iOS springboard before init WDA session ResetHomeOnStartup bool `json:"reset_home_on_startup,omitempty" yaml:"reset_home_on_startup,omitempty"` @@ -479,6 +499,33 @@ func (dev *IOSDevice) NewUSBDriver(capabilities Capabilities) (driver WebDriver, return wd, nil } +func (dev *IOSDevice) RunXCTest(bundleID string) (cancel context.CancelFunc, err error) { + log.Info().Str("bundleID", bundleID).Msg("run xctest") + out, cancel, err := dev.d.XCTest(bundleID) + if err != nil { + return nil, errors.Wrap(err, "run xctest failed") + } + // wait for xctest to start + time.Sleep(5 * time.Second) + + f, err := os.OpenFile(fmt.Sprintf("xctest_%s.log", dev.UDID), + os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o666) + if err != nil { + return nil, err + } + defer builtinLog.SetOutput(f) + + // print xctest running logs + go func() { + for s := range out { + builtinLog.Print(s) + } + f.Close() + }() + + return cancel, nil +} + func (dExt *DriverExt) ConnectMjpegStream(httpClient *http.Client) (err error) { if httpClient == nil { return errors.New(`'httpClient' can't be nil`) From e5d937c9c35042302167b60af3235ad5c6c907c6 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sat, 3 Dec 2022 00:18:10 +0800 Subject: [PATCH 18/22] docs: update wcl, select match time --- examples/worldcup/cli.go | 2 +- examples/worldcup/main.go | 53 ++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go index 4ca59b60e..f28eed1d9 100644 --- a/examples/worldcup/cli.go +++ b/examples/worldcup/cli.go @@ -15,7 +15,7 @@ import ( var rootCmd = &cobra.Command{ Use: "wcl", Short: "Monitor FIFA World Cup Live", - Version: "2022.12.01.2236", + Version: "2022.12.03.0018", PreRun: func(cmd *cobra.Command, args []string) { log.Logger = zerolog.New( zerolog.ConsoleWriter{NoColor: false, Out: os.Stderr}, diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index 3d38c6c99..f91b65771 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -154,32 +154,49 @@ 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)) + ocrTexts, err := wc.driver.GetTextsByOCR( + uixt.WithScreenShot(fileName), + ) if err != nil { log.Error().Err(err).Msg("get ocr texts failed") return err } - var liveTimeSeconds int + // 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 { - liveTimeSeconds = seconds - line := fmt.Sprintf("%s\t%d\t%s\t%d\n", - utcTimeStr, utcTime.Unix(), ocrText.Text, liveTimeSeconds) - log.Info().Str("utcTime", utcTimeStr).Str("liveTime", ocrText.Text).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: ocrText.Text, - LiveTimeSeconds: liveTimeSeconds, - }) - break + 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 } @@ -205,10 +222,6 @@ func (wc *WorldCupLive) EnterLive(bundleID string) error { _ = wc.driver.TapAbsXY(points[1].X, points[1].Y) } - if err = wc.driver.SwipeRelative(0.5, 0.1, 0.8, 0.1); err != nil { - log.Error().Err(err).Msg("swipe failed") - } - // 进入世界杯 tab if err = wc.driver.TapByOCR("世界杯"); err != nil { log.Error().Err(err).Msg("enter 直播中 failed") From dcc2f651d49c9d9b3918f369912fa174aa7cf2c8 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 13 Dec 2022 21:30:05 +0800 Subject: [PATCH 19/22] refactor: run step with specified times --- examples/worldcup/main_test.go | 70 ++++++++++++++++++++++++++++++++++ hrp/pkg/uixt/interface.go | 4 +- hrp/pkg/uixt/ios_device.go | 3 ++ hrp/step_mobile_ui.go | 61 +++++++++++++++-------------- hrp/step_mobile_ui_test.go | 12 +++--- 5 files changed, 113 insertions(+), 37 deletions(-) diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index 0bb65a14b..cf684e93c 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -3,9 +3,12 @@ package main import ( + "os" "testing" "github.com/stretchr/testify/assert" + + "github.com/httprunner/httprunner/v4/hrp" ) func TestConvertTimeToSeconds(t *testing.T) { @@ -44,3 +47,70 @@ func TestMainAndroid(t *testing.T) { wc.EnterLive(bundleID) wc.Start() } + +func init() { + os.Setenv("UDID", "00008030-00194DA421C1802E") +} + +func TestIOSDouyinWorldCupLive(t *testing.T) { + testCase := &hrp.TestCase{ + Config: hrp.NewConfig("直播_抖音_世界杯_ios"). + WithVariables(map[string]interface{}{ + "device": "${ENV(UDID)}", + }). + SetIOS( + hrp.WithUDID("$device"), + hrp.WithLogOn(true), + hrp.WithWDAPort(8700), + hrp.WithWDAMjpegPort(8800), + hrp.WithXCTest("com.gtf.wda.runner.xctrunner"), + ), + TestSteps: []hrp.IStep{ + hrp.NewStep("启动抖音"). + IOS(). + Home(). + AppTerminate("com.ss.iphone.ugc.Aweme"). // 关闭已运行的抖音 + AppLaunch("com.ss.iphone.ugc.Aweme"). + Validate(). + AssertOCRExists("首页", "抖音启动失败,「首页」不存在"), + hrp.NewStep("处理青少年弹窗"). + IOS(). + TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)), + hrp.NewStep("点击首页"). + IOS(). + TapByOCR("首页", hrp.WithIndex(-1)).Sleep(5), + hrp.NewStep("点击世界杯页"). + IOS(). + SwipeToTapText("世界杯", + hrp.WithMaxRetryTimes(5), + hrp.WithCustomDirection(0.4, 0.07, 0.6, 0.07), // 滑动 tab,从左到右,解决「世界杯」被遮挡的问题 + hrp.WithScope(0, 0, 1, 0.15), // 限定 tab 区域 + hrp.WithWaitTime(1), + ). + Swipe(0.5, 0.3, 0.5, 0.2), // 少量上划,解决「直播中」未展示的问题 + hrp.NewStep("点击进入直播间"). + IOS(). + LoopTimes(30). // 重复执行 30 次 + TapByOCR("直播中", hrp.WithIdentifier("click_live"), hrp.WithIndex(-1)). + Sleep(30).Back().Sleep(30), + hrp.NewStep("关闭抖音"). + IOS(). + AppTerminate("com.ss.iphone.ugc.Aweme"), + hrp.NewStep("返回主界面,并打开本地时间戳"). + IOS(). + Home().SwipeToTapApp("local", hrp.WithMaxRetryTimes(5)).Sleep(10). + Validate(). + AssertOCRExists("16", "打开本地时间戳失败"), + }, + } + + if err := testCase.Dump2JSON("ios_worldcup_live_douyin_test.json"); err != nil { + t.Fatal(err) + } + + runner := hrp.NewRunner(t).SetSaveTests(true) + err := runner.Run(testCase) + if err != nil { + t.Fatal(err) + } +} diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index cd837ff16..59c0b643a 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -872,7 +872,9 @@ func WithScreenShot(fileName ...string) DataOption { } func NewDataOptions(options ...DataOption) *DataOptions { - dataOptions := &DataOptions{} + dataOptions := &DataOptions{ + Data: make(map[string]interface{}), + } for _, option := range options { option(dataOptions) } diff --git a/hrp/pkg/uixt/ios_device.go b/hrp/pkg/uixt/ios_device.go index 7dc105232..d5a19a602 100644 --- a/hrp/pkg/uixt/ios_device.go +++ b/hrp/pkg/uixt/ios_device.go @@ -159,6 +159,9 @@ func GetIOSDeviceOptions(dev *IOSDevice) (deviceOptions []IOSDeviceOption) { if dev.PerfOptions != nil { deviceOptions = append(deviceOptions, WithPerfOptions(dev.perfOpitons()...)) } + if dev.XCTestBundleID != "" { + deviceOptions = append(deviceOptions, WithXCTest(dev.XCTestBundleID)) + } if dev.ResetHomeOnStartup { deviceOptions = append(deviceOptions, WithResetHomeOnStartup(true)) } diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index 4dcf2be84..ebd65728c 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -22,6 +22,7 @@ var ( WithAcceptAlertButtonSelector = uixt.WithAcceptAlertButtonSelector WithDismissAlertButtonSelector = uixt.WithDismissAlertButtonSelector WithPerfOptions = uixt.WithPerfOptions + WithXCTest = uixt.WithXCTest ) // android setting options @@ -34,6 +35,7 @@ var ( type MobileStep struct { Serial string `json:"serial,omitempty" yaml:"serial,omitempty"` + Times int `json:"times,omitempty" yaml:"times,omitempty"` uixt.MobileAction `yaml:",inline"` Actions []uixt.MobileAction `json:"actions,omitempty" yaml:"actions,omitempty"` } @@ -301,25 +303,9 @@ func (s *StepMobile) Input(text string, options ...uixt.ActionOption) *StepMobil return &StepMobile{step: s.step} } -// Times specify running times for run last action -func (s *StepMobile) Times(n int) *StepMobile { - if n <= 0 { - log.Warn().Int("n", n).Msg("times should be positive, set to 1") - n = 1 - } - - mobileStep := s.mobileStep() - actionsTotal := len(mobileStep.Actions) - if actionsTotal == 0 { - return s - } - - // actionsTotal >=1 && n >= 1 - lastAction := mobileStep.Actions[actionsTotal-1 : actionsTotal][0] - for i := 0; i < n-1; i++ { - // duplicate last action n-1 times - mobileStep.Actions = append(mobileStep.Actions, lastAction) - } +// LoopTimes specify running times for the current step +func (s *StepMobile) LoopTimes(n int) *StepMobile { + s.mobileStep().Times = n return &StepMobile{step: s.step} } @@ -622,20 +608,33 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err actions = mobileStep.Actions } - // run actions - for _, action := range actions { - if action.Params, err = s.caseRunner.parser.Parse(action.Params, stepVariables); err != nil { - if !code.IsErrorPredefined(err) { - err = errors.Wrap(code.ParseError, - fmt.Sprintf("parse action params failed: %v", err)) + // run times + times := mobileStep.Times + if times < 0 { + log.Warn().Int("times", times).Msg("times should be positive, set to 1") + times = 1 + } else if times == 0 { + times = 1 + } else if times > 1 { + log.Info().Int("times", times).Msg("run actions with specified times") + } + + // run actions with specified times + for i := 0; i < times; i++ { + for _, action := range actions { + if action.Params, err = s.caseRunner.parser.Parse(action.Params, stepVariables); err != nil { + if !code.IsErrorPredefined(err) { + err = errors.Wrap(code.ParseError, + fmt.Sprintf("parse action params failed: %v", err)) + } + return stepResult, err } - return stepResult, err - } - if err := uiDriver.DoAction(action); err != nil { - if !code.IsErrorPredefined(err) { - err = errors.Wrap(code.MobileUIDriverError, err.Error()) + if err := uiDriver.DoAction(action); err != nil { + if !code.IsErrorPredefined(err) { + err = errors.Wrap(code.MobileUIDriverError, err.Error()) + } + return stepResult, err } - return stepResult, err } } diff --git a/hrp/step_mobile_ui_test.go b/hrp/step_mobile_ui_test.go index c9c7dae30..75f4cfe71 100644 --- a/hrp/step_mobile_ui_test.go +++ b/hrp/step_mobile_ui_test.go @@ -32,7 +32,7 @@ func TestIOSSearchApp(t *testing.T) { Config: NewConfig("ios ui action on Search App 资源库"), TestSteps: []IStep{ NewStep("进入 App 资源库 搜索框"). - IOS().Home().SwipeLeft().Times(2).Tap("dewey-search-field"). + IOS().Home().SwipeLeft().SwipeLeft().Tap("dewey-search-field"). Validate(). AssertLabelExists("取消"), NewStep("搜索抖音"). @@ -84,10 +84,10 @@ func TestIOSWeixinLive(t *testing.T) { TapByOCR("直播"). // 通过 OCR 识别「直播」 Validate(). AssertLabelExists("直播"), - NewStep("向上滑动 5 次"). + NewStep("向上滑动 3 次,截图保存"). IOS(). - SwipeUp().Times(3).ScreenShot(). // 上划 3 次,截图保存 - SwipeUp().Times(2).ScreenShot(), // 再上划 2 次,截图保存 + LoopTimes(3). // 整体循环 3 次 + SwipeUp().SwipeUp().ScreenShot(), // 上划 2 次,截图保存 }, } err := NewRunner(t).Run(testCase) @@ -154,7 +154,9 @@ func TestIOSDouyinAction(t *testing.T) { AssertLabelExists("首页", "首页 tab 不存在"). AssertLabelExists("消息", "消息 tab 不存在"), NewStep("swipe up and down"). - IOS().SwipeUp().Times(3).SwipeDown(), + IOS(). + LoopTimes(3). + SwipeUp().SwipeDown(), }, } err := NewRunner(t).Run(testCase) From 11abdf07afc12d8657a0424fa89bb53de0ea6035 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 13 Dec 2022 22:34:11 +0800 Subject: [PATCH 20/22] change: add logs for loop index --- examples/worldcup/main_test.go | 31 ++++++++----------------------- hrp/step_mobile_ui.go | 31 +++++++++++++++++-------------- hrp/step_mobile_ui_test.go | 4 ++-- 3 files changed, 27 insertions(+), 39 deletions(-) diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index cf684e93c..e7cecf8ff 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -3,7 +3,6 @@ package main import ( - "os" "testing" "github.com/stretchr/testify/assert" @@ -48,18 +47,14 @@ func TestMainAndroid(t *testing.T) { wc.Start() } -func init() { - os.Setenv("UDID", "00008030-00194DA421C1802E") -} - func TestIOSDouyinWorldCupLive(t *testing.T) { testCase := &hrp.TestCase{ Config: hrp.NewConfig("直播_抖音_世界杯_ios"). WithVariables(map[string]interface{}{ - "device": "${ENV(UDID)}", + "appBundleID": "com.ss.iphone.ugc.Aweme", }). SetIOS( - hrp.WithUDID("$device"), + hrp.WithUDID(uuid), hrp.WithLogOn(true), hrp.WithWDAPort(8700), hrp.WithWDAMjpegPort(8800), @@ -69,8 +64,8 @@ func TestIOSDouyinWorldCupLive(t *testing.T) { hrp.NewStep("启动抖音"). IOS(). Home(). - AppTerminate("com.ss.iphone.ugc.Aweme"). // 关闭已运行的抖音 - AppLaunch("com.ss.iphone.ugc.Aweme"). + AppTerminate("$appBundleID"). // 关闭已运行的抖音 + AppLaunch("$appBundleID"). Validate(). AssertOCRExists("首页", "抖音启动失败,「首页」不存在"), hrp.NewStep("处理青少年弹窗"). @@ -86,28 +81,18 @@ func TestIOSDouyinWorldCupLive(t *testing.T) { hrp.WithCustomDirection(0.4, 0.07, 0.6, 0.07), // 滑动 tab,从左到右,解决「世界杯」被遮挡的问题 hrp.WithScope(0, 0, 1, 0.15), // 限定 tab 区域 hrp.WithWaitTime(1), - ). - Swipe(0.5, 0.3, 0.5, 0.2), // 少量上划,解决「直播中」未展示的问题 + ), hrp.NewStep("点击进入直播间"). IOS(). - LoopTimes(30). // 重复执行 30 次 + Loop(5). // 重复执行 5 次 TapByOCR("直播中", hrp.WithIdentifier("click_live"), hrp.WithIndex(-1)). - Sleep(30).Back().Sleep(30), + Sleep(3).Back().Sleep(3), hrp.NewStep("关闭抖音"). IOS(). - AppTerminate("com.ss.iphone.ugc.Aweme"), - hrp.NewStep("返回主界面,并打开本地时间戳"). - IOS(). - Home().SwipeToTapApp("local", hrp.WithMaxRetryTimes(5)).Sleep(10). - Validate(). - AssertOCRExists("16", "打开本地时间戳失败"), + AppTerminate("$appBundleID"), }, } - if err := testCase.Dump2JSON("ios_worldcup_live_douyin_test.json"); err != nil { - t.Fatal(err) - } - runner := hrp.NewRunner(t).SetSaveTests(true) err := runner.Run(testCase) if err != nil { diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index ebd65728c..de42bcb4f 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -35,7 +35,7 @@ var ( type MobileStep struct { Serial string `json:"serial,omitempty" yaml:"serial,omitempty"` - Times int `json:"times,omitempty" yaml:"times,omitempty"` + Loops int `json:"loops,omitempty" yaml:"loops,omitempty"` uixt.MobileAction `yaml:",inline"` Actions []uixt.MobileAction `json:"actions,omitempty" yaml:"actions,omitempty"` } @@ -303,9 +303,9 @@ func (s *StepMobile) Input(text string, options ...uixt.ActionOption) *StepMobil return &StepMobile{step: s.step} } -// LoopTimes specify running times for the current step -func (s *StepMobile) LoopTimes(n int) *StepMobile { - s.mobileStep().Times = n +// Loop specify running times for the current step +func (s *StepMobile) Loop(times int) *StepMobile { + s.mobileStep().Loops = times return &StepMobile{step: s.step} } @@ -608,19 +608,22 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err actions = mobileStep.Actions } - // run times - times := mobileStep.Times - if times < 0 { - log.Warn().Int("times", times).Msg("times should be positive, set to 1") - times = 1 - } else if times == 0 { - times = 1 - } else if times > 1 { - log.Info().Int("times", times).Msg("run actions with specified times") + // run times of actions + loopTimes := mobileStep.Loops + if loopTimes < 0 { + log.Warn().Int("loopTimes", loopTimes).Msg("times should be positive, set to 1") + loopTimes = 1 + } else if loopTimes == 0 { + loopTimes = 1 + } else if loopTimes > 1 { + log.Info().Int("loopTimes", loopTimes).Msg("run actions with specified loop times") } // run actions with specified times - for i := 0; i < times; i++ { + for i := 0; i < loopTimes; i++ { + if i > 0 { + log.Info().Int("index", i+1).Msg("start running actions in loop") + } for _, action := range actions { if action.Params, err = s.caseRunner.parser.Parse(action.Params, stepVariables); err != nil { if !code.IsErrorPredefined(err) { diff --git a/hrp/step_mobile_ui_test.go b/hrp/step_mobile_ui_test.go index 75f4cfe71..00b1a4b96 100644 --- a/hrp/step_mobile_ui_test.go +++ b/hrp/step_mobile_ui_test.go @@ -86,7 +86,7 @@ func TestIOSWeixinLive(t *testing.T) { AssertLabelExists("直播"), NewStep("向上滑动 3 次,截图保存"). IOS(). - LoopTimes(3). // 整体循环 3 次 + Loop(3). // 整体循环 3 次 SwipeUp().SwipeUp().ScreenShot(), // 上划 2 次,截图保存 }, } @@ -155,7 +155,7 @@ func TestIOSDouyinAction(t *testing.T) { AssertLabelExists("消息", "消息 tab 不存在"), NewStep("swipe up and down"). IOS(). - LoopTimes(3). + Loop(3). SwipeUp().SwipeDown(), }, } From 5786ff59a24b8d83e8c2813ff2615d6e60004af8 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 13 Dec 2022 23:31:02 +0800 Subject: [PATCH 21/22] refactor: move uixt API --- examples/uitest/demo_android_douyin_test.go | 13 +-- examples/uitest/demo_douyin_follow_live.yaml | 83 ------------------- .../uitest/demo_douyin_follow_live_test.go | 21 ++--- examples/uitest/demo_douyin_live.yaml | 57 ------------- examples/uitest/demo_douyin_test.go | 23 ++--- examples/uitest/demo_kuaishou_test.go | 69 +++++++++++++++ examples/uitest/demo_weixin_live.yaml | 58 ------------- examples/uitest/demo_weixin_test.go | 17 ++-- .../uitest/ios_kuaishou_follow_live_test.go | 19 +++-- .../uitest/ios_kuaishou_follow_live_test.yaml | 83 ------------------- examples/uitest/wda_log_test.go | 30 ++++--- examples/worldcup/main.go | 22 +++-- examples/worldcup/main_test.go | 25 +++--- hrp/pkg/uixt/ios_device.go | 25 +++++- hrp/step.go | 38 --------- hrp/step_mobile_ui.go | 22 ----- hrp/step_mobile_ui_test.go | 8 +- 17 files changed, 189 insertions(+), 424 deletions(-) delete mode 100644 examples/uitest/demo_douyin_follow_live.yaml delete mode 100644 examples/uitest/demo_douyin_live.yaml create mode 100644 examples/uitest/demo_kuaishou_test.go delete mode 100644 examples/uitest/demo_weixin_live.yaml delete mode 100644 examples/uitest/ios_kuaishou_follow_live_test.yaml diff --git a/examples/uitest/demo_android_douyin_test.go b/examples/uitest/demo_android_douyin_test.go index 01fa3f949..bbfa63916 100644 --- a/examples/uitest/demo_android_douyin_test.go +++ b/examples/uitest/demo_android_douyin_test.go @@ -6,32 +6,33 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestAndroidDouYinLive(t *testing.T) { testCase := &hrp.TestCase{ Config: hrp.NewConfig("通过 feed 头像进入抖音直播间"). - SetAndroid(hrp.WithAdbLogOn(true), hrp.WithSerialNumber("2d06bf70")), + SetAndroid(uixt.WithAdbLogOn(true), uixt.WithSerialNumber("2d06bf70")), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). Android(). Home(). AppTerminate("com.ss.android.ugc.aweme"). // 关闭已运行的抖音,确保启动抖音后在「抖音」首页 - SwipeToTapApp("抖音", hrp.WithMaxRetryTimes(5)). + SwipeToTapApp("抖音", uixt.WithMaxRetryTimes(5)). Sleep(10), hrp.NewStep("处理青少年弹窗"). Android(). Tap("推荐"). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)). + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)). Validate(). AssertOCRExists("首页", "抖音启动失败,「首页」不存在"), hrp.NewStep("在推荐页上划,直到出现 feed 头像「直播」"). Android(). - SwipeToTapText("直播", hrp.WithMaxRetryTimes(10), hrp.WithIdentifier("进入直播间")), + SwipeToTapText("直播", uixt.WithMaxRetryTimes(10), uixt.WithIdentifier("进入直播间")), hrp.NewStep("向上滑动,等待 10s"). Android(). - SwipeUp(hrp.WithIdentifier("第一次上划")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 - SwipeUp(hrp.WithIdentifier("第二次上划")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 + SwipeUp(uixt.WithIdentifier("第一次上划")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 + SwipeUp(uixt.WithIdentifier("第二次上划")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 }, } diff --git a/examples/uitest/demo_douyin_follow_live.yaml b/examples/uitest/demo_douyin_follow_live.yaml deleted file mode 100644 index f6c34c3fc..000000000 --- a/examples/uitest/demo_douyin_follow_live.yaml +++ /dev/null @@ -1,83 +0,0 @@ -config: - name: 通过 关注天窗 进入指定主播抖音直播间 - variables: - app_name: 抖音 - ios: - - port: 8700 - mjpeg_port: 8800 - log_on: true -teststeps: - - name: 启动抖音 - ios: - actions: - - method: home - - method: app_terminate - params: com.ss.iphone.ugc.Aweme - - method: swipe_to_tap_app - params: $app_name - identifier: 启动抖音 - max_retry_times: 5 - - method: sleep - params: 5 - validate: - - check: ui_ocr - assert: exists - expect: 推荐 - msg: 抖音启动失败,「推荐」不存在 - - name: 处理青少年弹窗 - ios: - actions: - - method: tap_ocr - params: 我知道了 - ignore_NotFoundError: true - - name: 点击首页 - ios: - actions: - - method: tap_ocr - params: 首页 - index: -1 - - method: sleep - params: 10 - - name: 点击关注页 - ios: - actions: - - method: tap_ocr - params: 关注 - index: 1 - - method: sleep - params: 10 - - name: 向上滑动 2 次 - ios: - actions: - - method: swipe_to_tap_texts - params: - - 理肤泉 - - 婉宝 - identifier: click_live - direction: - - 0.6 - - 0.2 - - 0.2 - - 0.2 - - method: sleep - params: 10 - - method: swipe - params: - - 0.9 - - 0.7 - - 0.9 - - 0.3 - identifier: slide_in_live - - method: sleep - params: 10 - - method: screenshot - - method: swipe - params: - - 0.9 - - 0.7 - - 0.9 - - 0.3 - identifier: slide_in_live - - method: sleep - params: 10 - - method: screenshot diff --git a/examples/uitest/demo_douyin_follow_live_test.go b/examples/uitest/demo_douyin_follow_live_test.go index 1eb42f93f..59b223ef3 100644 --- a/examples/uitest/demo_douyin_follow_live_test.go +++ b/examples/uitest/demo_douyin_follow_live_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestIOSDouyinFollowLive(t *testing.T) { @@ -15,31 +16,31 @@ func TestIOSDouyinFollowLive(t *testing.T) { "app_name": "抖音", }). SetIOS( - hrp.WithLogOn(true), - hrp.WithWDAPort(8700), - hrp.WithWDAMjpegPort(8800), + uixt.WithWDALogOn(true), + uixt.WithWDAPort(8700), + uixt.WithWDAMjpegPort(8800), ), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). IOS(). Home(). AppTerminate("com.ss.iphone.ugc.Aweme"). // 关闭已运行的抖音 - SwipeToTapApp("$app_name", hrp.WithMaxRetryTimes(5), hrp.WithIdentifier("启动抖音")).Sleep(5). + SwipeToTapApp("$app_name", uixt.WithMaxRetryTimes(5), uixt.WithIdentifier("启动抖音")).Sleep(5). Validate(). AssertOCRExists("推荐", "抖音启动失败,「推荐」不存在"), hrp.NewStep("处理青少年弹窗"). IOS(). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)), + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)), hrp.NewStep("点击首页"). IOS(). - TapByOCR("首页", hrp.WithIndex(-1)).Sleep(10), + TapByOCR("首页", uixt.WithIndex(-1)).Sleep(10), hrp.NewStep("点击关注页"). IOS(). - TapByOCR("关注", hrp.WithIndex(1)).Sleep(10), + TapByOCR("关注", uixt.WithIndex(1)).Sleep(10), hrp.NewStep("向上滑动 2 次"). - IOS().SwipeToTapTexts([]string{"理肤泉", "婉宝"}, hrp.WithCustomDirection(0.6, 0.2, 0.2, 0.2), hrp.WithIdentifier("click_live")).Sleep(10). - Swipe(0.9, 0.7, 0.9, 0.3, hrp.WithIdentifier("slide_in_live")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 - Swipe(0.9, 0.7, 0.9, 0.3, hrp.WithIdentifier("slide_in_live")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 + IOS().SwipeToTapTexts([]string{"理肤泉", "婉宝"}, uixt.WithCustomDirection(0.6, 0.2, 0.2, 0.2), uixt.WithIdentifier("click_live")).Sleep(10). + Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 + Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 }, } diff --git a/examples/uitest/demo_douyin_live.yaml b/examples/uitest/demo_douyin_live.yaml deleted file mode 100644 index e20f426c6..000000000 --- a/examples/uitest/demo_douyin_live.yaml +++ /dev/null @@ -1,57 +0,0 @@ -config: - name: 通过 feed 卡片进入抖音直播间 - variables: - app_name: 抖音 - ios: - - perf_options: - sys_cpu: true - sys_mem: true - port: 8700 - mjpeg_port: 8800 - log_on: true -teststeps: - - name: 启动抖音 - ios: - actions: - - method: home - - method: app_terminate - params: com.ss.iphone.ugc.Aweme - - method: swipe_to_tap_app - params: $app_name - identifier: 启动抖音 - max_retry_times: 5 - - method: sleep - params: 5 - validate: - - check: ui_ocr - assert: exists - expect: 推荐 - msg: 抖音启动失败,「推荐」不存在 - - name: 处理青少年弹窗 - ios: - actions: - - method: tap_ocr - params: 我知道了 - ignore_NotFoundError: true - - name: 向上滑动 2 次 - ios: - actions: - - method: swipe - params: up - identifier: 第一次上划 - - method: sleep - params: 2 - - method: screenshot - - method: swipe - params: up - identifier: 第二次上划 - - method: sleep - params: 2 - - method: screenshot - - name: 在推荐页上划,直到出现「点击进入直播间」 - ios: - actions: - - method: swipe_to_tap_text - params: 点击进入直播间 - identifier: 进入直播间 - max_retry_times: 10 diff --git a/examples/uitest/demo_douyin_test.go b/examples/uitest/demo_douyin_test.go index 4d3215bb6..def2e9d3b 100644 --- a/examples/uitest/demo_douyin_test.go +++ b/examples/uitest/demo_douyin_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestIOSDouyinLive(t *testing.T) { @@ -15,12 +16,12 @@ func TestIOSDouyinLive(t *testing.T) { "app_name": "抖音", }). SetIOS( - hrp.WithLogOn(true), - hrp.WithWDAPort(8700), - hrp.WithWDAMjpegPort(8800), - hrp.WithPerfOptions( - hrp.WithPerfSystemCPU(true), - hrp.WithPerfSystemMem(true), + uixt.WithWDALogOn(true), + uixt.WithWDAPort(8700), + uixt.WithWDAMjpegPort(8800), + uixt.WithIOSPerfOptions( + uixt.WithIOSPerfSystemCPU(true), + uixt.WithIOSPerfSystemMem(true), ), ), TestSteps: []hrp.IStep{ @@ -28,19 +29,19 @@ func TestIOSDouyinLive(t *testing.T) { IOS(). Home(). AppTerminate("com.ss.iphone.ugc.Aweme"). // 关闭已运行的抖音 - SwipeToTapApp("$app_name", hrp.WithMaxRetryTimes(5), hrp.WithIdentifier("启动抖音")).Sleep(5). + SwipeToTapApp("$app_name", uixt.WithMaxRetryTimes(5), uixt.WithIdentifier("启动抖音")).Sleep(5). Validate(). AssertOCRExists("推荐", "抖音启动失败,「推荐」不存在"), hrp.NewStep("处理青少年弹窗"). IOS(). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)), + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)), hrp.NewStep("向上滑动 2 次"). IOS(). - SwipeUp(hrp.WithIdentifier("第一次上划")).Sleep(2).ScreenShot(). // 上划 1 次,等待 2s,截图保存 - SwipeUp(hrp.WithIdentifier("第二次上划")).Sleep(2).ScreenShot(), // 再上划 1 次,等待 2s,截图保存 + SwipeUp(uixt.WithIdentifier("第一次上划")).Sleep(2).ScreenShot(). // 上划 1 次,等待 2s,截图保存 + SwipeUp(uixt.WithIdentifier("第二次上划")).Sleep(2).ScreenShot(), // 再上划 1 次,等待 2s,截图保存 hrp.NewStep("在推荐页上划,直到出现「点击进入直播间」"). IOS(). - SwipeToTapText("点击进入直播间", hrp.WithMaxRetryTimes(10), hrp.WithIdentifier("进入直播间")), + SwipeToTapText("点击进入直播间", uixt.WithMaxRetryTimes(10), uixt.WithIdentifier("进入直播间")), }, } diff --git a/examples/uitest/demo_kuaishou_test.go b/examples/uitest/demo_kuaishou_test.go new file mode 100644 index 000000000..639a6422d --- /dev/null +++ b/examples/uitest/demo_kuaishou_test.go @@ -0,0 +1,69 @@ +package uitest + +import ( + "testing" + + "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" +) + +func TestAndroidKuaiShouFeedCardLive(t *testing.T) { + testCase := &hrp.TestCase{ + Config: hrp.NewConfig("直播_快手_Feed卡片_android"). + WithVariables(map[string]interface{}{ + "device": "${ENV(SerialNumber)}", + }). + SetAndroid( + uixt.WithSerialNumber("$device"), + uixt.WithAdbLogOn(true)), + TestSteps: []hrp.IStep{ + hrp.NewStep("启动快手"). + Android(). + AppTerminate("com.smile.gifmaker"). + AppLaunch("com.smile.gifmaker"). + Home(). + SwipeToTapApp("快手", uixt.WithMaxRetryTimes(5)).Sleep(10), + hrp.NewStep("处理青少年弹窗"). + Android(). + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)). + Validate(). + AssertOCRExists("精选", "进入快手失败"), + hrp.NewStep("点击精选"). + Android(). + TapByOCR("精选", uixt.WithIndex(-1), uixt.WithOffset(0, -50)).Sleep(10), + hrp.NewStep("点击直播标签,进入直播间"). + Android(). + SwipeToTapText("点击进入直播间", + uixt.WithCustomDirection(0.9, 0.7, 0.9, 0.3), + uixt.WithScope(0.2, 0.5, 0.8, 0.8), + uixt.WithMaxRetryTimes(20), + uixt.WithWaitTime(60), + uixt.WithIdentifier("click_live"), + ), + hrp.NewStep("等待1分钟"). + Android(). + Sleep(60), + hrp.NewStep("上滑进入下一个直播间"). + Android(). + Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live")).Sleep(60), + hrp.NewStep("返回主界面,并打开本地时间戳"). + Android(). + Home().SwipeToTapApp("local", uixt.WithMaxRetryTimes(5)).Sleep(10). + Validate(). + AssertOCRExists("16", "打开本地时间戳失败"), + }, + } + + if err := testCase.Dump2JSON("android_feed_card_live_test.json"); err != nil { + t.Fatal(err) + } + if err := testCase.Dump2YAML("android_feed_card_live_test.yaml"); err != nil { + t.Fatal(err) + } + + runner := hrp.NewRunner(t).SetSaveTests(true) + err := runner.Run(testCase) + if err != nil { + t.Fatal(err) + } +} diff --git a/examples/uitest/demo_weixin_live.yaml b/examples/uitest/demo_weixin_live.yaml deleted file mode 100644 index 3b064f09f..000000000 --- a/examples/uitest/demo_weixin_live.yaml +++ /dev/null @@ -1,58 +0,0 @@ -config: - name: 通过 feed 卡片进入微信直播间 - ios: - - port: 8700 - mjpeg_port: 8800 - log_on: true -teststeps: - - name: 启动微信 - ios: - actions: - - method: home - - method: app_terminate - params: com.tencent.xin - - method: swipe_to_tap_app - params: 微信 - max_retry_times: 5 - validate: - - check: ui_label - assert: exists - expect: 通讯录 - msg: 微信启动失败,「通讯录」不存在 - - name: 进入直播页 - ios: - actions: - - method: tap - params: 发现 - - method: tap_ocr - params: 视频号 - identifier: 进入视频号 - index: -1 - - name: 处理青少年弹窗 - ios: - actions: - - method: tap_ocr - params: 我知道了 - ignore_NotFoundError: true - - name: 在推荐页上划,直到出现「轻触进入直播间」 - ios: - actions: - - method: swipe_to_tap_text - params: 轻触进入直播间 - identifier: 进入直播间 - max_retry_times: 10 - - name: 向上滑动,等待 10s - ios: - actions: - - method: swipe - params: up - identifier: 第一次上划 - - method: sleep - params: 10 - - method: screenshot - - method: swipe - params: up - identifier: 第二次上划 - - method: sleep - params: 10 - - method: screenshot diff --git a/examples/uitest/demo_weixin_test.go b/examples/uitest/demo_weixin_test.go index 835411c5d..133ae18cb 100644 --- a/examples/uitest/demo_weixin_test.go +++ b/examples/uitest/demo_weixin_test.go @@ -6,34 +6,35 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestIOSWeixinLive(t *testing.T) { testCase := &hrp.TestCase{ Config: hrp.NewConfig("通过 feed 卡片进入微信直播间"). - SetIOS(hrp.WithLogOn(true), hrp.WithWDAPort(8700), hrp.WithWDAMjpegPort(8800)), + SetIOS(uixt.WithWDALogOn(true), uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)), TestSteps: []hrp.IStep{ hrp.NewStep("启动微信"). IOS(). Home(). AppTerminate("com.tencent.xin"). // 关闭已运行的微信,确保启动微信后在「微信」首页 - SwipeToTapApp("微信", hrp.WithMaxRetryTimes(5)). + SwipeToTapApp("微信", uixt.WithMaxRetryTimes(5)). Validate(). AssertLabelExists("通讯录", "微信启动失败,「通讯录」不存在"), hrp.NewStep("进入直播页"). IOS(). - Tap("发现"). // 进入「发现页」 - TapByOCR("视频号", hrp.WithIdentifier("进入视频号"), hrp.WithIndex(-1)), // 通过 OCR 识别「视频号」 + Tap("发现"). // 进入「发现页」 + TapByOCR("视频号", uixt.WithIdentifier("进入视频号"), uixt.WithIndex(-1)), // 通过 OCR 识别「视频号」 hrp.NewStep("处理青少年弹窗"). IOS(). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)), + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)), hrp.NewStep("在推荐页上划,直到出现「轻触进入直播间」"). IOS(). - SwipeToTapText("轻触进入直播间", hrp.WithMaxRetryTimes(10), hrp.WithIdentifier("进入直播间")), + SwipeToTapText("轻触进入直播间", uixt.WithMaxRetryTimes(10), uixt.WithIdentifier("进入直播间")), hrp.NewStep("向上滑动,等待 10s"). IOS(). - SwipeUp(hrp.WithIdentifier("第一次上划")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 - SwipeUp(hrp.WithIdentifier("第二次上划")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 + SwipeUp(uixt.WithIdentifier("第一次上划")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 + SwipeUp(uixt.WithIdentifier("第二次上划")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 }, } diff --git a/examples/uitest/ios_kuaishou_follow_live_test.go b/examples/uitest/ios_kuaishou_follow_live_test.go index e16b5be17..4fdf837de 100644 --- a/examples/uitest/ios_kuaishou_follow_live_test.go +++ b/examples/uitest/ios_kuaishou_follow_live_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestIOSKuaiShouLive(t *testing.T) { @@ -15,33 +16,37 @@ func TestIOSKuaiShouLive(t *testing.T) { "device": "${ENV(UDID)}", "ups": "${ENV(LIVEUPLIST)}", }). - SetIOS(hrp.WithUDID("$device"), hrp.WithLogOn(true), hrp.WithWDAPort(8100), hrp.WithWDAMjpegPort(9100)), + SetIOS( + uixt.WithUDID("$device"), + uixt.WithWDALogOn(true), + uixt.WithWDAPort(8100), + uixt.WithWDAMjpegPort(9100)), TestSteps: []hrp.IStep{ hrp.NewStep("启动快手"). IOS(). AppTerminate("com.jiangjia.gif"). AppLaunch("com.jiangjia.gif"). Home(). - SwipeToTapApp("快手", hrp.WithMaxRetryTimes(5)).Sleep(10). + SwipeToTapApp("快手", uixt.WithMaxRetryTimes(5)).Sleep(10). Validate(). AssertOCRExists("精选", "进入快手失败"), hrp.NewStep("点击首页"). IOS(). - TapByOCR("首页", hrp.WithIndex(-1)).Sleep(10), + TapByOCR("首页", uixt.WithIndex(-1)).Sleep(10), hrp.NewStep("点击发现页"). IOS(). - TapByOCR("发现", hrp.WithIndex(1)).Sleep(10), + TapByOCR("发现", uixt.WithIndex(1)).Sleep(10), hrp.NewStep("点击关注页"). IOS(). - TapByOCR("关注", hrp.WithIndex(1)).Sleep(10), + TapByOCR("关注", uixt.WithIndex(1)).Sleep(10), hrp.NewStep("点击直播标签,进入直播间"). IOS(). - SwipeToTapTexts("${split_by_comma($ups)}", hrp.WithCustomDirection(0.6, 0.2, 0.2, 0.2), hrp.WithIdentifier("click_live")).Sleep(60). + SwipeToTapTexts("${split_by_comma($ups)}", uixt.WithCustomDirection(0.6, 0.2, 0.2, 0.2), uixt.WithIdentifier("click_live")).Sleep(60). Validate(). AssertOCRExists("说点什么", "进入直播间失败"), hrp.NewStep("下滑进入下一个直播间"). IOS(). - Swipe(0.9, 0.7, 0.9, 0.3, hrp.WithIdentifier("slide_in_live")).Sleep(60), + Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live")).Sleep(60), }, } diff --git a/examples/uitest/ios_kuaishou_follow_live_test.yaml b/examples/uitest/ios_kuaishou_follow_live_test.yaml deleted file mode 100644 index 484f08152..000000000 --- a/examples/uitest/ios_kuaishou_follow_live_test.yaml +++ /dev/null @@ -1,83 +0,0 @@ -config: - name: 直播_快手_关注天窗_ios - variables: - device: ${ENV(UDID)} - ups: ${ENV(LIVEUPLIST)} - ios: - - udid: $device - port: 8100 - mjpeg_port: 9100 - log_on: true -teststeps: - - name: 启动快手 - ios: - actions: - - method: app_terminate - params: com.jiangjia.gif - - method: app_launch - params: com.jiangjia.gif - - method: home - - method: swipe_to_tap_app - params: 快手 - max_retry_times: 5 - - method: sleep - params: 10 - validate: - - check: ui_ocr - assert: exists - expect: 精选 - msg: 进入快手失败 - - name: 点击首页 - ios: - actions: - - method: tap_ocr - params: 首页 - index: -1 - - method: sleep - params: 10 - - name: 点击发现页 - ios: - actions: - - method: tap_ocr - params: 发现 - index: 1 - - method: sleep - params: 10 - - name: 点击关注页 - ios: - actions: - - method: tap_ocr - params: 关注 - index: 1 - - method: sleep - params: 10 - - name: 点击直播标签,进入直播间 - ios: - actions: - - method: swipe_to_tap_texts - params: ${split_by_comma($ups)} - identifier: click_live - direction: - - 0.6 - - 0.2 - - 0.2 - - 0.2 - - method: sleep - params: 60 - validate: - - check: ui_ocr - assert: exists - expect: 说点什么 - msg: 进入直播间失败 - - name: 下滑进入下一个直播间 - ios: - actions: - - method: swipe - params: - - 0.9 - - 0.7 - - 0.9 - - 0.3 - identifier: slide_in_live - - method: sleep - params: 60 diff --git a/examples/uitest/wda_log_test.go b/examples/uitest/wda_log_test.go index 54e7a0273..3c03a458b 100644 --- a/examples/uitest/wda_log_test.go +++ b/examples/uitest/wda_log_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestWDALog(t *testing.T) { @@ -14,29 +15,38 @@ func TestWDALog(t *testing.T) { WithVariables(map[string]interface{}{ "app_name": "抖音", }). - SetIOS(hrp.WithLogOn(true), hrp.WithWDAPort(8700), hrp.WithWDAMjpegPort(8800)), + SetIOS( + uixt.WithWDALogOn(true), + uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), + uixt.WithIOSPerfOptions( + uixt.WithIOSPerfSystemCPU(true), + uixt.WithIOSPerfSystemMem(true), + uixt.WithIOSPerfNetwork(true), + uixt.WithIOSPerfFPS(true), + ), + ), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). IOS(). Home(). AppTerminate("com.ss.iphone.ugc.Aweme"). // 关闭已运行的抖音 - SwipeToTapApp("$app_name", hrp.WithMaxRetryTimes(5), hrp.WithIdentifier("启动抖音")).Sleep(5). + SwipeToTapApp("$app_name", uixt.WithMaxRetryTimes(5), uixt.WithIdentifier("启动抖音")).Sleep(5). Validate(). AssertOCRExists("推荐", "抖音启动失败,「推荐」不存在"), hrp.NewStep("处理青少年弹窗"). IOS(). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)), + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)), hrp.NewStep("进入购物页"). - IOS().TapByOCR("购物", hrp.WithIdentifier("点击购物")).Sleep(5), + IOS().TapByOCR("商城", uixt.WithIdentifier("点击商城")).Sleep(5), hrp.NewStep("进入推荐页"). - IOS().TapByOCR("推荐", hrp.WithIdentifier("点击推荐")).Sleep(5), + IOS().TapByOCR("推荐", uixt.WithIdentifier("点击推荐")).Sleep(5), hrp.NewStep("向上滑动 2 次"). IOS(). - SwipeUp(hrp.WithIdentifier("第 1 次上划")).Sleep(2). - SwipeUp(hrp.WithIdentifier("第 2 次上划")).Sleep(2). - SwipeUp(hrp.WithIdentifier("第 3 次上划")).Sleep(2). - TapXY(0.9, 0.1, hrp.WithIdentifier("点击进入搜索框")).Sleep(2). - Input("httprunner", hrp.WithIdentifier("输入搜索关键词")), + SwipeUp(uixt.WithIdentifier("第 1 次上划")).Sleep(2). + SwipeUp(uixt.WithIdentifier("第 2 次上划")).Sleep(2). + SwipeUp(uixt.WithIdentifier("第 3 次上划")).Sleep(2). + TapXY(0.9, 0.1, uixt.WithIdentifier("点击进入搜索框")).Sleep(2). + Input("httprunner", uixt.WithIdentifier("输入搜索关键词")), }, } diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go index f91b65771..3217a293b 100644 --- a/examples/worldcup/main.go +++ b/examples/worldcup/main.go @@ -15,8 +15,6 @@ import ( "github.com/rs/zerolog/log" - "github.com/httprunner/httprunner/v4/hrp" - "github.com/httprunner/httprunner/v4/hrp/pkg/gidevice" "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) @@ -39,32 +37,32 @@ func convertTimeToSeconds(timeStr string) (int, error) { } func initIOSDevice(uuid string) uixt.Device { - perfOptions := []gidevice.PerfOption{} + perfOptions := []uixt.IOSPerfOption{} for _, p := range perf { switch p { case "sys_cpu": - perfOptions = append(perfOptions, hrp.WithPerfSystemCPU(true)) + perfOptions = append(perfOptions, uixt.WithIOSPerfSystemCPU(true)) case "sys_mem": - perfOptions = append(perfOptions, hrp.WithPerfSystemMem(true)) + perfOptions = append(perfOptions, uixt.WithIOSPerfSystemMem(true)) case "sys_net": - perfOptions = append(perfOptions, hrp.WithPerfSystemNetwork(true)) + perfOptions = append(perfOptions, uixt.WithIOSPerfSystemNetwork(true)) case "sys_disk": - perfOptions = append(perfOptions, hrp.WithPerfSystemDisk(true)) + perfOptions = append(perfOptions, uixt.WithIOSPerfSystemDisk(true)) case "network": - perfOptions = append(perfOptions, hrp.WithPerfNetwork(true)) + perfOptions = append(perfOptions, uixt.WithIOSPerfNetwork(true)) case "fps": - perfOptions = append(perfOptions, hrp.WithPerfFPS(true)) + perfOptions = append(perfOptions, uixt.WithIOSPerfFPS(true)) case "gpu": - perfOptions = append(perfOptions, hrp.WithPerfGPU(true)) + perfOptions = append(perfOptions, uixt.WithIOSPerfGPU(true)) } } - perfOptions = append(perfOptions, hrp.WithPerfOutputInterval(interval*1000)) + 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.WithPerfOptions(perfOptions...), + uixt.WithIOSPerfOptions(perfOptions...), uixt.WithXCTest("com.gtf.wda.runner.xctrunner"), ) if err != nil { diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go index e7cecf8ff..03024b05f 100644 --- a/examples/worldcup/main_test.go +++ b/examples/worldcup/main_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestConvertTimeToSeconds(t *testing.T) { @@ -54,11 +55,11 @@ func TestIOSDouyinWorldCupLive(t *testing.T) { "appBundleID": "com.ss.iphone.ugc.Aweme", }). SetIOS( - hrp.WithUDID(uuid), - hrp.WithLogOn(true), - hrp.WithWDAPort(8700), - hrp.WithWDAMjpegPort(8800), - hrp.WithXCTest("com.gtf.wda.runner.xctrunner"), + uixt.WithUDID(uuid), + uixt.WithWDALogOn(true), + uixt.WithWDAPort(8700), + uixt.WithWDAMjpegPort(8800), + uixt.WithXCTest("com.gtf.wda.runner.xctrunner"), ), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). @@ -70,22 +71,22 @@ func TestIOSDouyinWorldCupLive(t *testing.T) { AssertOCRExists("首页", "抖音启动失败,「首页」不存在"), hrp.NewStep("处理青少年弹窗"). IOS(). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)), + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)), hrp.NewStep("点击首页"). IOS(). - TapByOCR("首页", hrp.WithIndex(-1)).Sleep(5), + TapByOCR("首页", uixt.WithIndex(-1)).Sleep(5), hrp.NewStep("点击世界杯页"). IOS(). SwipeToTapText("世界杯", - hrp.WithMaxRetryTimes(5), - hrp.WithCustomDirection(0.4, 0.07, 0.6, 0.07), // 滑动 tab,从左到右,解决「世界杯」被遮挡的问题 - hrp.WithScope(0, 0, 1, 0.15), // 限定 tab 区域 - hrp.WithWaitTime(1), + 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("点击进入直播间"). IOS(). Loop(5). // 重复执行 5 次 - TapByOCR("直播中", hrp.WithIdentifier("click_live"), hrp.WithIndex(-1)). + TapByOCR("直播中", uixt.WithIdentifier("click_live"), uixt.WithIndex(-1)). Sleep(3).Back().Sleep(3), hrp.NewStep("关闭抖音"). IOS(). diff --git a/hrp/pkg/uixt/ios_device.go b/hrp/pkg/uixt/ios_device.go index d5a19a602..6236a3eee 100644 --- a/hrp/pkg/uixt/ios_device.go +++ b/hrp/pkg/uixt/ios_device.go @@ -48,6 +48,23 @@ const ( dismissAlertButtonSelector = "**/XCUIElementTypeButton[`label IN {'不允许','暂不'}`]" ) +type IOSPerfOption = gidevice.PerfOption + +var ( + WithIOSPerfSystemCPU = gidevice.WithPerfSystemCPU + WithIOSPerfSystemMem = gidevice.WithPerfSystemMem + WithIOSPerfSystemDisk = gidevice.WithPerfSystemDisk + WithIOSPerfSystemNetwork = gidevice.WithPerfSystemNetwork + WithIOSPerfGPU = gidevice.WithPerfGPU + WithIOSPerfFPS = gidevice.WithPerfFPS + WithIOSPerfNetwork = gidevice.WithPerfNetwork + WithIOSPerfBundleID = gidevice.WithPerfBundleID + WithIOSPerfPID = gidevice.WithPerfPID + WithIOSPerfOutputInterval = gidevice.WithPerfOutputInterval + WithIOSPerfProcessAttributes = gidevice.WithPerfProcessAttributes + WithIOSPerfSystemAttributes = gidevice.WithPerfSystemAttributes +) + type IOSDeviceOption func(*IOSDevice) func WithUDID(udid string) IOSDeviceOption { @@ -68,7 +85,7 @@ func WithWDAMjpegPort(port int) IOSDeviceOption { } } -func WithLogOn(logOn bool) IOSDeviceOption { +func WithWDALogOn(logOn bool) IOSDeviceOption { return func(device *IOSDevice) { device.LogOn = logOn } @@ -104,7 +121,7 @@ func WithXCTest(bundleID string) IOSDeviceOption { } } -func WithPerfOptions(options ...gidevice.PerfOption) IOSDeviceOption { +func WithIOSPerfOptions(options ...gidevice.PerfOption) IOSDeviceOption { return func(device *IOSDevice) { device.PerfOptions = &gidevice.PerfOptions{} for _, option := range options { @@ -154,10 +171,10 @@ func GetIOSDeviceOptions(dev *IOSDevice) (deviceOptions []IOSDeviceOption) { deviceOptions = append(deviceOptions, WithWDAMjpegPort(dev.MjpegPort)) } if dev.LogOn { - deviceOptions = append(deviceOptions, WithLogOn(true)) + deviceOptions = append(deviceOptions, WithWDALogOn(true)) } if dev.PerfOptions != nil { - deviceOptions = append(deviceOptions, WithPerfOptions(dev.perfOpitons()...)) + deviceOptions = append(deviceOptions, WithIOSPerfOptions(dev.perfOpitons()...)) } if dev.XCTestBundleID != "" { deviceOptions = append(deviceOptions, WithXCTest(dev.XCTestBundleID)) diff --git a/hrp/step.go b/hrp/step.go index 0df0347a8..e1c35aa25 100644 --- a/hrp/step.go +++ b/hrp/step.go @@ -1,10 +1,5 @@ package hrp -import ( - "github.com/httprunner/httprunner/v4/hrp/pkg/gidevice" - "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" -) - type StepType string const ( @@ -19,39 +14,6 @@ const ( stepTypeIOS StepType = "ios" ) -var ( - WithIdentifier = uixt.WithIdentifier - WithMaxRetryTimes = uixt.WithMaxRetryTimes - WithWaitTime = uixt.WithWaitTime // only applicable to SwipeToTap* action - WithIndex = uixt.WithIndex // index of the target element, should start from 1, only applicable to ocr actions - WithTimeout = uixt.WithTimeout - WithIgnoreNotFoundError = uixt.WithIgnoreNotFoundError - WithText = uixt.WithText - WithID = uixt.WithID - WithDescription = uixt.WithDescription - WithDuration = uixt.WithDuration // only applicable to ios swipe action - WithSteps = uixt.WithSteps // only applicable to android swipe action - WithDirection = uixt.WithDirection - WithCustomDirection = uixt.WithCustomDirection - WithScope = uixt.WithScope // only applicable to ocr actions - WithOffset = uixt.WithOffset -) - -var ( - WithPerfSystemCPU = gidevice.WithPerfSystemCPU - WithPerfSystemMem = gidevice.WithPerfSystemMem - WithPerfSystemDisk = gidevice.WithPerfSystemDisk - WithPerfSystemNetwork = gidevice.WithPerfSystemNetwork - WithPerfGPU = gidevice.WithPerfGPU - WithPerfFPS = gidevice.WithPerfFPS - WithPerfNetwork = gidevice.WithPerfNetwork - WithPerfBundleID = gidevice.WithPerfBundleID - WithPerfPID = gidevice.WithPerfPID - WithPerfOutputInterval = gidevice.WithPerfOutputInterval - WithPerfProcessAttributes = gidevice.WithPerfProcessAttributes - WithPerfSystemAttributes = gidevice.WithPerfSystemAttributes -) - type StepResult struct { Name string `json:"name" yaml:"name"` // step name StepType StepType `json:"step_type" yaml:"step_type"` // step type, testcase/request/transaction/rendezvous diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index de42bcb4f..e34b3f47c 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -11,28 +11,6 @@ import ( "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) -// ios setting options -var ( - WithUDID = uixt.WithUDID - WithWDAPort = uixt.WithWDAPort - WithWDAMjpegPort = uixt.WithWDAMjpegPort - WithLogOn = uixt.WithLogOn - WithResetHomeOnStartup = uixt.WithResetHomeOnStartup - WithSnapshotMaxDepth = uixt.WithSnapshotMaxDepth - WithAcceptAlertButtonSelector = uixt.WithAcceptAlertButtonSelector - WithDismissAlertButtonSelector = uixt.WithDismissAlertButtonSelector - WithPerfOptions = uixt.WithPerfOptions - WithXCTest = uixt.WithXCTest -) - -// android setting options -var ( - WithSerialNumber = uixt.WithSerialNumber - WithAdbIP = uixt.WithAdbIP - WithAdbPort = uixt.WithAdbPort - WithAdbLogOn = uixt.WithAdbLogOn -) - type MobileStep struct { Serial string `json:"serial,omitempty" yaml:"serial,omitempty"` Loops int `json:"loops,omitempty" yaml:"loops,omitempty"` diff --git a/hrp/step_mobile_ui_test.go b/hrp/step_mobile_ui_test.go index 00b1a4b96..5afe43c0a 100644 --- a/hrp/step_mobile_ui_test.go +++ b/hrp/step_mobile_ui_test.go @@ -4,12 +4,14 @@ package hrp import ( "testing" + + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestIOSSettingsAction(t *testing.T) { testCase := &TestCase{ Config: NewConfig("ios ui action on Settings"). - SetIOS(WithWDAPort(8700), WithWDAMjpegPort(8800)), + SetIOS(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)), TestSteps: []IStep{ NewStep("launch Settings"). IOS().Home().Tap("设置"). @@ -48,7 +50,7 @@ func TestIOSSearchApp(t *testing.T) { func TestIOSAppLaunch(t *testing.T) { testCase := &TestCase{ Config: NewConfig("启动 & 关闭 App"). - SetIOS(WithWDAPort(8700), WithWDAMjpegPort(8800)), + SetIOS(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)), TestSteps: []IStep{ NewStep("终止今日头条"). IOS().AppTerminate("com.ss.iphone.article.News"), @@ -69,7 +71,7 @@ func TestIOSAppLaunch(t *testing.T) { func TestIOSWeixinLive(t *testing.T) { testCase := &TestCase{ Config: NewConfig("ios ui action on 微信直播"). - SetIOS(WithLogOn(true), WithWDAPort(8100), WithWDAMjpegPort(9100)), + SetIOS(uixt.WithWDALogOn(true), uixt.WithWDAPort(8100), uixt.WithWDAMjpegPort(9100)), TestSteps: []IStep{ NewStep("启动微信"). IOS(). From c66f77195b0895a817678a80703940c32f275bbf Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 14 Dec 2022 00:30:00 +0800 Subject: [PATCH 22/22] bump version to v4.3.1 --- docs/CHANGELOG.md | 11 +++++++++++ hrp/internal/version/VERSION | 2 +- httprunner/__init__.py | 2 +- pyproject.toml | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index f000201d2..341974ce9 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,16 @@ # Release History +## v4.3.1 (2022-12-14) + +**go version** + +- 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 + ## v4.3.0 (2022-10-27) Release hrp sub package `uixt` to support iOS/Android UI automation testing 🎉 diff --git a/hrp/internal/version/VERSION b/hrp/internal/version/VERSION index 1ddc0f604..9198ad674 100644 --- a/hrp/internal/version/VERSION +++ b/hrp/internal/version/VERSION @@ -1 +1 @@ -v4.3.0 \ No newline at end of file +v4.3.1 \ No newline at end of file diff --git a/httprunner/__init__.py b/httprunner/__init__.py index 1f96dea8c..e501aa0c1 100644 --- a/httprunner/__init__.py +++ b/httprunner/__init__.py @@ -1,4 +1,4 @@ -__version__ = "v4.3.0" +__version__ = "v4.3.1" __description__ = "One-stop solution for HTTP(S) testing." diff --git a/pyproject.toml b/pyproject.toml index 33947c0f4..3893b00e8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "httprunner" -version = "v4.3.0" +version = "v4.3.1" description = "One-stop solution for HTTP(S) testing." license = "Apache-2.0" readme = "README.md"