From bdc8c30c1872cb738ef20d322189844b58fef7ae Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 21 Sep 2023 14:25:06 +0800 Subject: [PATCH 1/7] change: remove unused local popup handler --- hrp/pkg/uixt/popups.go | 64 ------------------------------ hrp/pkg/uixt/service_vedem_test.go | 22 ---------- 2 files changed, 86 deletions(-) diff --git a/hrp/pkg/uixt/popups.go b/hrp/pkg/uixt/popups.go index 689efa5a..f9acbd9a 100644 --- a/hrp/pkg/uixt/popups.go +++ b/hrp/pkg/uixt/popups.go @@ -10,76 +10,12 @@ import ( "github.com/httprunner/httprunner/v4/hrp/internal/code" ) -// TODO: add more popup texts -var popups = [][]string{ - {".*青少年.*", "我知道了"}, // 青少年弹窗 - {".*个人信息保护.*", "同意"}, - {".*通讯录.*", "拒绝"}, - {".*更新.*", "以后再说|稍后|取消"}, - {".*升级.*", "以后再说|稍后|取消"}, - {".*定位.*", "仅.*允许"}, - {".*拍照.*", "仅.*允许"}, - {".*录音.*", "仅.*允许"}, - {".*位置.*", "仅.*允许"}, - {".*权限.*", "仅.*允许|始终允许"}, - {".*允许.*", "仅.*允许|始终允许"}, - {".*风险.*", "继续使用"}, - {"管理使用时间", ".*忽略.*"}, -} - const ( CloseStatusFound = "found" CloseStatusSuccess = "success" CloseStatusFail = "fail" ) -func findTextPopup(screenTexts OCRTexts) (closePoint *OCRText) { - for _, popup := range popups { - if len(popup) != 2 { - continue - } - - points, err := screenTexts.FindTexts([]string{popup[0], popup[1]}, WithRegex(true)) - if err == nil { - log.Warn().Interface("popup", popup). - Interface("texts", screenTexts).Msg("text popup found") - closePoint = &points[1] - break - } - } - return -} - -func (dExt *DriverExt) handleTextPopup(screenTexts OCRTexts) error { - closePoint := findTextPopup(screenTexts) - if closePoint == nil { - // no popup found - return nil - } - - log.Info().Str("text", closePoint.Text).Msg("close popup") - pointCenter := closePoint.Center() - if err := dExt.TapAbsXY(pointCenter.X, pointCenter.Y); err != nil { - log.Error().Err(err).Msg("tap popup failed") - return errors.Wrap(code.MobileUIPopupError, err.Error()) - } - // tap popup success - return nil -} - -func (dExt *DriverExt) AutoPopupHandler() error { - // TODO: check popup by activity type - - // check popup by screenshot - screenResult, err := dExt.GetScreenResult( - WithScreenShotOCR(true), WithScreenShotUpload(true)) - if err != nil { - return errors.Wrap(err, "get screen result failed for popup handler") - } - - return dExt.handleTextPopup(screenResult.Texts) -} - // ClosePopupsResult represents the result of recognized popup to close type ClosePopupsResult struct { Type string `json:"type"` diff --git a/hrp/pkg/uixt/service_vedem_test.go b/hrp/pkg/uixt/service_vedem_test.go index bc215bee..730c22a9 100644 --- a/hrp/pkg/uixt/service_vedem_test.go +++ b/hrp/pkg/uixt/service_vedem_test.go @@ -6,7 +6,6 @@ import ( "bytes" "fmt" "os" - "regexp" "testing" ) @@ -52,27 +51,6 @@ func TestOCRWithLocalFile(t *testing.T) { } } -func matchPopup(text string) bool { - for _, popup := range popups { - if regexp.MustCompile(popup[1]).MatchString(text) { - return true - } - } - return false -} - -func TestMatchRegex(t *testing.T) { - testData := []string{ - "以后再说", "我知道了", "同意", "拒绝", "稍后", - "始终允许", "继续使用", "仅在使用中允许", - } - for _, text := range testData { - if !matchPopup(text) { - t.Fatal(text) - } - } -} - func TestTapUIWithScreenshot(t *testing.T) { serialNumber := os.Getenv("SERIAL_NUMBER") device, _ := NewAndroidDevice(WithSerialNumber(serialNumber)) From f68b644548290d85537ad8f9ebbebdc873701d10 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 21 Sep 2023 15:26:27 +0800 Subject: [PATCH 2/7] change: update logs for popup handler --- hrp/pkg/uixt/popups.go | 24 ++++++++++++++++++------ hrp/pkg/uixt/swipe.go | 2 +- hrp/pkg/uixt/video_crawler.go | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/hrp/pkg/uixt/popups.go b/hrp/pkg/uixt/popups.go index f9acbd9a..2f9fcc10 100644 --- a/hrp/pkg/uixt/popups.go +++ b/hrp/pkg/uixt/popups.go @@ -25,7 +25,7 @@ type ClosePopupsResult struct { } type PopupInfo struct { - CloseStatus string `json:"close_status"` + CloseStatus string `json:"close_status"` // found/success/fail Type string `json:"type"` Text string `json:"text"` RetryCount int `json:"retry_count"` @@ -50,8 +50,8 @@ func (dExt *DriverExt) ClosePopups(options ...ActionOption) error { } func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { - log.Info().Msg("try to find and close popups") actionOptions := NewActionOptions(options...) + log.Info().Interface("actionOptions", actionOptions).Msg("try to find and close popups") maxRetryTimes := actionOptions.MaxRetryTimes interval := actionOptions.Interval @@ -62,17 +62,22 @@ func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { log.Error().Err(err).Msg("get screen result failed for popup handler") continue } + screenResult.Popup.RetryCount = retryCount + // 1. there are no popups here (fast return normally) // 2. failed to close popup (maybe tap error, return error) // 3. successful to close popup (sleep and wait for next retry if existed) if screenResult.Popup == nil { + log.Debug().Msg("no popup found") break } - screenResult.Popup.RetryCount = retryCount + + // popup found if !screenResult.Popup.PopupArea.IsEmpty() { screenResult.Popup.CloseStatus = CloseStatusFound } if screenResult.Popup.CloseArea.IsEmpty() { + log.Warn().Msg("popup close area not found") break } screenResult.Popup.CloseStatus = CloseStatusFound @@ -80,6 +85,8 @@ func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { if err = dExt.tapPopupHandler(screenResult.Popup); err != nil { return err } + + log.Info().Interface("popup", screenResult.Popup).Msg("close popup success") // sleep for another popup (if existed) to pop time.Sleep(time.Duration(1000*interval) * time.Millisecond) } @@ -91,11 +98,16 @@ func (dExt *DriverExt) tapPopupHandler(popup *PopupInfo) error { return nil } if popup.CloseArea.IsEmpty() { + log.Warn().Msg("popup close area not found") return nil } - log.Info().Str("type", popup.Type).Str("text", popup.Text).Msg("close popup") - popupCenter := popup.CloseArea.Center() - if err := dExt.TapAbsXY(popupCenter.X, popupCenter.Y); err != nil { + + closePoint := popup.CloseArea.Center() + log.Info(). + Interface("popup", popup). + Interface("closePoint", closePoint). + Msg("tap to close popup") + if err := dExt.TapAbsXY(closePoint.X, closePoint.Y); err != nil { log.Error().Err(err).Msg("tap popup failed") return errors.Wrap(code.MobileUIPopupError, err.Error()) } diff --git a/hrp/pkg/uixt/swipe.go b/hrp/pkg/uixt/swipe.go index 0e34e23b..704d6daa 100644 --- a/hrp/pkg/uixt/swipe.go +++ b/hrp/pkg/uixt/swipe.go @@ -150,7 +150,7 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption) } points, err := screenResult.Texts.FindTexts(texts, dExt.ParseActionOptions(optionsWithoutIdentifier...)...) if err != nil { - log.Error().Err(err).Msg("swipeToTapTexts failed") + log.Error().Err(err).Strs("texts", texts).Msg("find texts failed") // target texts not found, try to auto handle popup if e := dExt.tapPopupHandler(screenResult.Popup); e != nil { log.Error().Err(e).Msg("auto handle popup failed") diff --git a/hrp/pkg/uixt/video_crawler.go b/hrp/pkg/uixt/video_crawler.go index 6df0cbe1..f50ec0c3 100644 --- a/hrp/pkg/uixt/video_crawler.go +++ b/hrp/pkg/uixt/video_crawler.go @@ -251,7 +251,7 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { continue } if e := crawler.driverExt.tapPopupHandler(screenResult.Popup); e != nil { - log.Error().Err(e).Msg("auto handle popup failed") + log.Error().Err(e).Msg("close live popup failed") continue } From 1fd170236c8f75a375985eae7e6e7afe2beda1f4 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 21 Sep 2023 16:07:13 +0800 Subject: [PATCH 3/7] fix: raise error if close area not found to popup --- hrp/pkg/uixt/popups.go | 35 ++++++++++++++--------------------- hrp/pkg/uixt/swipe.go | 3 ++- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/hrp/pkg/uixt/popups.go b/hrp/pkg/uixt/popups.go index 2f9fcc10..a6546109 100644 --- a/hrp/pkg/uixt/popups.go +++ b/hrp/pkg/uixt/popups.go @@ -62,31 +62,18 @@ func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { log.Error().Err(err).Msg("get screen result failed for popup handler") continue } - screenResult.Popup.RetryCount = retryCount - // 1. there are no popups here (fast return normally) - // 2. failed to close popup (maybe tap error, return error) - // 3. successful to close popup (sleep and wait for next retry if existed) - if screenResult.Popup == nil { + popup := screenResult.Popup + if popup == nil { log.Debug().Msg("no popup found") break } + popup.RetryCount = retryCount - // popup found - if !screenResult.Popup.PopupArea.IsEmpty() { - screenResult.Popup.CloseStatus = CloseStatusFound - } - if screenResult.Popup.CloseArea.IsEmpty() { - log.Warn().Msg("popup close area not found") - break - } - screenResult.Popup.CloseStatus = CloseStatusFound - - if err = dExt.tapPopupHandler(screenResult.Popup); err != nil { + if err = dExt.tapPopupHandler(popup); err != nil { return err } - log.Info().Interface("popup", screenResult.Popup).Msg("close popup success") // sleep for another popup (if existed) to pop time.Sleep(time.Duration(1000*interval) * time.Millisecond) } @@ -97,12 +84,17 @@ func (dExt *DriverExt) tapPopupHandler(popup *PopupInfo) error { if popup == nil { return nil } - if popup.CloseArea.IsEmpty() { - log.Warn().Msg("popup close area not found") - return nil + popup.CloseStatus = CloseStatusFound + + popupClose := popup.CloseArea + if popupClose.IsEmpty() { + log.Error(). + Interface("popup", popup). + Msg("popup close area not found") + return errors.New("popup close area not found") } - closePoint := popup.CloseArea.Center() + closePoint := popupClose.Center() log.Info(). Interface("popup", popup). Interface("closePoint", closePoint). @@ -111,6 +103,7 @@ func (dExt *DriverExt) tapPopupHandler(popup *PopupInfo) error { log.Error().Err(err).Msg("tap popup failed") return errors.Wrap(code.MobileUIPopupError, err.Error()) } + // tap popup success return nil } diff --git a/hrp/pkg/uixt/swipe.go b/hrp/pkg/uixt/swipe.go index 704d6daa..92c52f28 100644 --- a/hrp/pkg/uixt/swipe.go +++ b/hrp/pkg/uixt/swipe.go @@ -148,7 +148,8 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption) if err != nil { return err } - points, err := screenResult.Texts.FindTexts(texts, dExt.ParseActionOptions(optionsWithoutIdentifier...)...) + points, err := screenResult.Texts.FindTexts(texts, + dExt.ParseActionOptions(optionsWithoutIdentifier...)...) if err != nil { log.Error().Err(err).Strs("texts", texts).Msg("find texts failed") // target texts not found, try to auto handle popup From e83f7def5671a11aa09d9118020e9b55b9cf18ea Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 21 Sep 2023 16:15:57 +0800 Subject: [PATCH 4/7] change: revert local text popup handler --- hrp/pkg/uixt/popups.go | 64 ++++++++++++++++++++++++++++++ hrp/pkg/uixt/service_vedem_test.go | 22 ++++++++++ 2 files changed, 86 insertions(+) diff --git a/hrp/pkg/uixt/popups.go b/hrp/pkg/uixt/popups.go index a6546109..0821cc12 100644 --- a/hrp/pkg/uixt/popups.go +++ b/hrp/pkg/uixt/popups.go @@ -10,6 +10,70 @@ import ( "github.com/httprunner/httprunner/v4/hrp/internal/code" ) +// TODO: add more popup texts +var popups = [][]string{ + {".*青少年.*", "我知道了"}, // 青少年弹窗 + {".*个人信息保护.*", "同意"}, + {".*通讯录.*", "拒绝"}, + {".*更新.*", "以后再说|稍后|取消"}, + {".*升级.*", "以后再说|稍后|取消"}, + {".*定位.*", "仅.*允许"}, + {".*拍照.*", "仅.*允许"}, + {".*录音.*", "仅.*允许"}, + {".*位置.*", "仅.*允许"}, + {".*权限.*", "仅.*允许|始终允许"}, + {".*允许.*", "仅.*允许|始终允许"}, + {".*风险.*", "继续使用"}, + {"管理使用时间", ".*忽略.*"}, +} + +func findTextPopup(screenTexts OCRTexts) (closePoint *OCRText) { + for _, popup := range popups { + if len(popup) != 2 { + continue + } + + points, err := screenTexts.FindTexts([]string{popup[0], popup[1]}, WithRegex(true)) + if err == nil { + log.Warn().Interface("popup", popup). + Interface("texts", screenTexts).Msg("text popup found") + closePoint = &points[1] + break + } + } + return +} + +func (dExt *DriverExt) handleTextPopup(screenTexts OCRTexts) error { + closePoint := findTextPopup(screenTexts) + if closePoint == nil { + // no popup found + return nil + } + + log.Info().Str("text", closePoint.Text).Msg("close popup") + pointCenter := closePoint.Center() + if err := dExt.TapAbsXY(pointCenter.X, pointCenter.Y); err != nil { + log.Error().Err(err).Msg("tap popup failed") + return errors.Wrap(code.MobileUIPopupError, err.Error()) + } + // tap popup success + return nil +} + +func (dExt *DriverExt) AutoPopupHandler() error { + // TODO: check popup by activity type + + // check popup by screenshot + screenResult, err := dExt.GetScreenResult( + WithScreenShotOCR(true), WithScreenShotUpload(true)) + if err != nil { + return errors.Wrap(err, "get screen result failed for popup handler") + } + + return dExt.handleTextPopup(screenResult.Texts) +} + const ( CloseStatusFound = "found" CloseStatusSuccess = "success" diff --git a/hrp/pkg/uixt/service_vedem_test.go b/hrp/pkg/uixt/service_vedem_test.go index 730c22a9..bc215bee 100644 --- a/hrp/pkg/uixt/service_vedem_test.go +++ b/hrp/pkg/uixt/service_vedem_test.go @@ -6,6 +6,7 @@ import ( "bytes" "fmt" "os" + "regexp" "testing" ) @@ -51,6 +52,27 @@ func TestOCRWithLocalFile(t *testing.T) { } } +func matchPopup(text string) bool { + for _, popup := range popups { + if regexp.MustCompile(popup[1]).MatchString(text) { + return true + } + } + return false +} + +func TestMatchRegex(t *testing.T) { + testData := []string{ + "以后再说", "我知道了", "同意", "拒绝", "稍后", + "始终允许", "继续使用", "仅在使用中允许", + } + for _, text := range testData { + if !matchPopup(text) { + t.Fatal(text) + } + } +} + func TestTapUIWithScreenshot(t *testing.T) { serialNumber := os.Getenv("SERIAL_NUMBER") device, _ := NewAndroidDevice(WithSerialNumber(serialNumber)) From f477d8ae9b6dc4bd389542091bfa6a726d564e83 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 21 Sep 2023 16:42:58 +0800 Subject: [PATCH 5/7] change: update logs for popup handler --- hrp/pkg/uixt/popups.go | 8 +++++--- hrp/pkg/uixt/swipe.go | 2 +- hrp/pkg/uixt/video_crawler.go | 13 ++++--------- hrp/step_mobile_ui.go | 2 +- 4 files changed, 11 insertions(+), 14 deletions(-) diff --git a/hrp/pkg/uixt/popups.go b/hrp/pkg/uixt/popups.go index 0821cc12..d0e9f608 100644 --- a/hrp/pkg/uixt/popups.go +++ b/hrp/pkg/uixt/popups.go @@ -128,7 +128,7 @@ func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { } popup := screenResult.Popup - if popup == nil { + if popup == nil || popup.PopupArea.IsEmpty() { log.Debug().Msg("no popup found") break } @@ -145,7 +145,8 @@ func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { } func (dExt *DriverExt) tapPopupHandler(popup *PopupInfo) error { - if popup == nil { + if popup == nil || popup.PopupArea.IsEmpty() { + log.Debug().Msg("no popup found") return nil } popup.CloseStatus = CloseStatusFound @@ -155,7 +156,8 @@ func (dExt *DriverExt) tapPopupHandler(popup *PopupInfo) error { log.Error(). Interface("popup", popup). Msg("popup close area not found") - return errors.New("popup close area not found") + return errors.Wrap(code.MobileUIPopupError, + "popup close area not found") } closePoint := popupClose.Center() diff --git a/hrp/pkg/uixt/swipe.go b/hrp/pkg/uixt/swipe.go index 92c52f28..4ad1141f 100644 --- a/hrp/pkg/uixt/swipe.go +++ b/hrp/pkg/uixt/swipe.go @@ -154,7 +154,7 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption) log.Error().Err(err).Strs("texts", texts).Msg("find texts failed") // target texts not found, try to auto handle popup if e := dExt.tapPopupHandler(screenResult.Popup); e != nil { - log.Error().Err(e).Msg("auto handle popup failed") + log.Error().Err(e).Msg("tap popup handler failed") } return err } diff --git a/hrp/pkg/uixt/video_crawler.go b/hrp/pkg/uixt/video_crawler.go index f50ec0c3..faf31c76 100644 --- a/hrp/pkg/uixt/video_crawler.go +++ b/hrp/pkg/uixt/video_crawler.go @@ -186,7 +186,7 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { log.Warn().Msg("get current feed video failed") // check and handle popups - if err := crawler.driverExt.ClosePopupsHandler(WithMaxRetryTimes(1)); err != nil { + if err := crawler.driverExt.ClosePopupsHandler(WithMaxRetryTimes(3)); err != nil { return err } @@ -234,9 +234,8 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { case VideoType_Live: // 直播 - log.Info(). - Interface("video", currentVideo). - Msg(FOUND_LIVE_SUCCESS) + crawler.LiveCount++ + log.Info().Interface("video", currentVideo).Msg(FOUND_LIVE_SUCCESS) // take screenshot and get screen texts by OCR screenResult, err := crawler.driverExt.GetScreenResult( @@ -262,7 +261,6 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { currentVideo.LiveType = screenResult.imageResult.LiveType } - crawler.LiveCount++ // simulation watch feed video sleepStrict(swipeFinishTime, currentVideo.PlayDuration) @@ -295,6 +293,7 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { default: // 点播 || 图文 || 广告 || etc. crawler.FeedCount++ + log.Info().Interface("video", currentVideo).Msg(FOUND_FEED_SUCCESS) screenResult := &ScreenResult{ Resolution: dExt.windowSize, @@ -304,11 +303,7 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { SwipeStartTime: swipeStartTime.UnixMilli(), SwipeFinishTime: swipeFinishTime.UnixMilli(), } - dExt.cacheStepData.screenResults[time.Now().String()] = screenResult - log.Info(). - Interface("video", currentVideo). - Msg(FOUND_FEED_SUCCESS) // simulation watch feed video sleepStrict(swipeFinishTime, currentVideo.PlayDuration) diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index 09a3f194..66014878 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -625,7 +625,7 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err // automatic handling of pop-up windows on each step finished if err2 := uiDriver.ClosePopups(); err2 != nil { - log.Error().Err(err2).Str("step", step.Name).Msg("auto handle popup failed") + log.Error().Err(err2).Str("step", step.Name).Msg("handle popup failed on step finished") } // save attachments From 0d997e9785e7e85dd339987f6a619e23090a5e6f Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 21 Sep 2023 17:31:59 +0800 Subject: [PATCH 6/7] fix: check if the current popup equals to the last popup --- hrp/pkg/uixt/ext.go | 36 ------------------------------------ hrp/pkg/uixt/popups.go | 23 +++++++++++++++++++++++ 2 files changed, 23 insertions(+), 36 deletions(-) diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index 54009453..7deaaa3e 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -14,7 +14,6 @@ import ( "os" "os/signal" "path/filepath" - "sort" "strings" "syscall" "time" @@ -87,40 +86,6 @@ func (screenResults ScreenResultMap) getScreenShotUrls() map[string]string { return screenShotsUrls } -// updatePopupCloseStatus checks if popup closed normally in every screenResult with close_popups on: -func (screenResults ScreenResultMap) updatePopupCloseStatus() { - var popupScreenResultList []*ScreenResult - for _, screenResult := range screenResults { - if screenResult.Popup == nil { - continue - } - popupScreenResultList = append(popupScreenResultList, screenResult) - } - if len(popupScreenResultList) == 0 { - return - } - sort.Slice(popupScreenResultList, func(i, j int) bool { - return popupScreenResultList[i].Popup.RetryCount < popupScreenResultList[j].Popup.RetryCount - }) - - for i := 0; i < len(popupScreenResultList)-1; i++ { - curPopup := popupScreenResultList[i].Popup - nextPopup := popupScreenResultList[i+1].Popup - - // popup not existed, no need to close - if curPopup.CloseArea.IsEmpty() { - continue - } - // popup existed, but identical popups occurs during next retry - if nextPopup.CloseArea.IsIdentical(curPopup.CloseArea) { - popupScreenResultList[i].Popup.CloseStatus = CloseStatusFail - continue - } - // popup existed, but no popup or different popup occurs during next retry (IsClosed=true) - popupScreenResultList[i].Popup.CloseStatus = CloseStatusSuccess - } -} - type cacheStepData struct { // cache step screenshot paths screenShots []string @@ -273,7 +238,6 @@ func (dExt *DriverExt) GetStepCacheData() map[string]interface{} { cacheData["screenshots"] = dExt.cacheStepData.screenShots cacheData["screenshots_urls"] = dExt.cacheStepData.screenResults.getScreenShotUrls() - dExt.cacheStepData.screenResults.updatePopupCloseStatus() cacheData["screen_results"] = dExt.cacheStepData.screenResults // clear cache diff --git a/hrp/pkg/uixt/popups.go b/hrp/pkg/uixt/popups.go index d0e9f608..da2085b7 100644 --- a/hrp/pkg/uixt/popups.go +++ b/hrp/pkg/uixt/popups.go @@ -119,6 +119,7 @@ func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { maxRetryTimes := actionOptions.MaxRetryTimes interval := actionOptions.Interval + var lastPopup *PopupInfo for retryCount := 0; retryCount < maxRetryTimes; retryCount++ { screenResult, err := dExt.GetScreenResult( WithScreenShotClosePopups(true), WithScreenShotUpload(true)) @@ -132,18 +133,40 @@ func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { log.Debug().Msg("no popup found") break } + popup.CloseStatus = CloseStatusFound popup.RetryCount = retryCount + // check if the current popup equals to the last popup + if isPopupIdentical(popup, lastPopup) { + return errors.Wrap(code.MobileUIPopupError, "handle popup failed") + } + if err = dExt.tapPopupHandler(popup); err != nil { return err } // sleep for another popup (if existed) to pop time.Sleep(time.Duration(1000*interval) * time.Millisecond) + lastPopup = popup } return nil } +func isPopupIdentical(popup, lastPopup *PopupInfo) bool { + if lastPopup == nil || lastPopup.PopupArea.IsEmpty() { + return false + } + + if !popup.CloseArea.IsIdentical(lastPopup.CloseArea) { + lastPopup.CloseStatus = CloseStatusSuccess + return false + } + + popup.CloseStatus = CloseStatusFail + lastPopup.CloseStatus = CloseStatusFail + return true +} + func (dExt *DriverExt) tapPopupHandler(popup *PopupInfo) error { if popup == nil || popup.PopupArea.IsEmpty() { log.Debug().Msg("no popup found") From 4962d08fb27ddbe4cf5f916ec47dfe81206d8a98 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 21 Sep 2023 19:22:21 +0800 Subject: [PATCH 7/7] fix: check if popup exists --- hrp/pkg/uixt/popups.go | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/hrp/pkg/uixt/popups.go b/hrp/pkg/uixt/popups.go index da2085b7..a1d01e07 100644 --- a/hrp/pkg/uixt/popups.go +++ b/hrp/pkg/uixt/popups.go @@ -99,6 +99,25 @@ type PopupInfo struct { CloseArea Box `json:"close_area"` } +func (p *PopupInfo) isIdentical(lastPopup *PopupInfo) bool { + if lastPopup == nil || lastPopup.PopupArea.IsEmpty() { + return false + } + + if !p.CloseArea.IsIdentical(lastPopup.CloseArea) { + lastPopup.CloseStatus = CloseStatusSuccess + return false + } + + p.CloseStatus = CloseStatusFail + lastPopup.CloseStatus = CloseStatusFail + return true +} + +func (p *PopupInfo) exists() bool { + return p.PopupArea.IsEmpty() || p.CloseArea.IsEmpty() +} + func (dExt *DriverExt) ClosePopups(options ...ActionOption) error { actionOptions := NewActionOptions(options...) @@ -129,7 +148,7 @@ func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { } popup := screenResult.Popup - if popup == nil || popup.PopupArea.IsEmpty() { + if popup == nil || !popup.exists() { log.Debug().Msg("no popup found") break } @@ -137,7 +156,7 @@ func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { popup.RetryCount = retryCount // check if the current popup equals to the last popup - if isPopupIdentical(popup, lastPopup) { + if popup.isIdentical(lastPopup) { return errors.Wrap(code.MobileUIPopupError, "handle popup failed") } @@ -152,23 +171,8 @@ func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error { return nil } -func isPopupIdentical(popup, lastPopup *PopupInfo) bool { - if lastPopup == nil || lastPopup.PopupArea.IsEmpty() { - return false - } - - if !popup.CloseArea.IsIdentical(lastPopup.CloseArea) { - lastPopup.CloseStatus = CloseStatusSuccess - return false - } - - popup.CloseStatus = CloseStatusFail - lastPopup.CloseStatus = CloseStatusFail - return true -} - func (dExt *DriverExt) tapPopupHandler(popup *PopupInfo) error { - if popup == nil || popup.PopupArea.IsEmpty() { + if popup == nil || !popup.exists() { log.Debug().Msg("no popup found") return nil }