From 2b658f5f51d84657867d2a93f1eef26de3c63699 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Thu, 29 Aug 2024 21:00:02 +0800 Subject: [PATCH] refactor: check popup and close area --- hrp/pkg/uixt/ext.go | 3 -- hrp/pkg/uixt/popups.go | 83 ++++-------------------------------------- 2 files changed, 8 insertions(+), 78 deletions(-) diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index c24b9de9..4bb0072f 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -118,9 +118,6 @@ type DriverExt struct { // funplugin plugin funplugin.IPlugin - - // cache last popup to check if popup handle result - lastPopup *PopupInfo } func newDriverExt(device Device, driver WebDriver, options ...DriverOption) (dExt *DriverExt, err error) { diff --git a/hrp/pkg/uixt/popups.go b/hrp/pkg/uixt/popups.go index c2937c04..43b1717e 100644 --- a/hrp/pkg/uixt/popups.go +++ b/hrp/pkg/uixt/popups.go @@ -1,8 +1,6 @@ package uixt import ( - "math/rand" - "github.com/pkg/errors" "github.com/rs/zerolog/log" @@ -96,72 +94,19 @@ type PopupInfo struct { PicURL string `json:"pic_url"` } -func (p *PopupInfo) getClosePoint(lastPopup *PopupInfo) (*PointF, error) { +func (p *PopupInfo) getClosePoint() *PointF { closeResult := p.ClosePopupsResult if closeResult == nil { - return nil, nil + return nil } - // 弹框不存在 && 关闭按钮不存在 - if closeResult.PopupArea.IsEmpty() && closeResult.CloseArea.IsEmpty() { - if p.ClosePoints == nil { - // 关闭图标不存在 => 100% 确定不存在弹窗 - return nil, nil - } - - // 存在关闭按钮,结合上一次的 popup 进行判断 - if lastPopup == nil || lastPopup.ClosePoints == nil { - // 当前关闭图标为首次出现,确定是弹窗关闭按钮的概率较小 - log.Debug().Interface("closePoints", p.ClosePoints). - Msg("skip close points for the first time") - return nil, nil - } - - // 连续两次都存在关闭图标 - if p.ClosePoints[0].IsIdentical(lastPopup.ClosePoints[0]) { - // 连续两次图标位置相同 => 存在弹窗 => 点击关闭 - log.Warn(). - Interface("closePoint", p.ClosePoints[0]). - Interface("lastClosePoints", lastPopup.ClosePoints). - Msg("popup close point detected") - return getRandomClosePoint(p.ClosePoints), nil - } - - // 连续两次图标位置不同 => 可能不是弹窗 => skip - log.Debug().Interface("closePoints", p.ClosePoints). - Interface("lastClosePoints", lastPopup.ClosePoints). - Msg("skip close points for not sure") - return nil, nil + // 弹窗关闭按钮不存在 + if closeResult.CloseArea.IsEmpty() { + return nil } - // 弹窗存在 && 关闭按钮不存在 - if !closeResult.PopupArea.IsEmpty() && closeResult.CloseArea.IsEmpty() { - if p.ClosePoints == nil { - // 关闭图标不存在 => 无法处理,抛异常 - log.Error().Interface("popup", p).Msg("popup close area not found") - return nil, errors.Wrap(code.MobileUIPopupError, "popup close area not found") - } - - // 使用关闭图标作为关闭按钮(随机选择一个) - return getRandomClosePoint(p.ClosePoints), nil - } - - // 关闭按钮存在 && (弹框存在 || 不存在) - if closeResult.Type != "" || p.ClosePoints == nil { - // 弹窗类型存在 || 关闭图标不存在 => 基于关闭按钮关闭弹窗 - closePoint := closeResult.CloseArea.Center() - return &closePoint, nil - } else { - // 弹窗类型不存在 && 关闭图标存在,使用关闭图标作为关闭按钮(随机选择一个) - return getRandomClosePoint(p.ClosePoints), nil - } -} - -func getRandomClosePoint(closePoints []PointF) *PointF { - if len(closePoints) == 1 { - return &closePoints[0] - } - return &closePoints[rand.Intn(len(closePoints))] + closePoint := closeResult.CloseArea.Center() + return &closePoint } func (dExt *DriverExt) ClosePopupsHandler() (err error) { @@ -170,7 +115,6 @@ func (dExt *DriverExt) ClosePopupsHandler() (err error) { screenResult, err := dExt.GetScreenResult( WithScreenShotUpload(true), WithScreenShotClosePopups(true), // get popup area and close area - WithScreenShotUITypes("close"), // get all close buttons ) if err != nil { log.Error().Err(err).Msg("get screen result failed for popup handler") @@ -178,16 +122,7 @@ func (dExt *DriverExt) ClosePopupsHandler() (err error) { } popup := screenResult.Popup - - defer func() { - dExt.lastPopup = popup - }() - - closePoint, err := popup.getClosePoint(dExt.lastPopup) - if err != nil { - return err - } - + closePoint := popup.getClosePoint() if closePoint == nil { // close point not found log.Debug().Msg("close point not found") @@ -204,7 +139,5 @@ func (dExt *DriverExt) ClosePopupsHandler() (err error) { return errors.Wrap(code.MobileUIPopupError, err.Error()) } - // wait 1s and check if popup still exists - log.Info().Msg("tap close point success, check if popup still exists") return nil }