mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-07 08:27:35 +08:00
refactor: check popup and close area
This commit is contained in:
@@ -118,9 +118,6 @@ type DriverExt struct {
|
|||||||
|
|
||||||
// funplugin
|
// funplugin
|
||||||
plugin funplugin.IPlugin
|
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) {
|
func newDriverExt(device Device, driver WebDriver, options ...DriverOption) (dExt *DriverExt, err error) {
|
||||||
|
|||||||
+8
-75
@@ -1,8 +1,6 @@
|
|||||||
package uixt
|
package uixt
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
@@ -96,72 +94,19 @@ type PopupInfo struct {
|
|||||||
PicURL string `json:"pic_url"`
|
PicURL string `json:"pic_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PopupInfo) getClosePoint(lastPopup *PopupInfo) (*PointF, error) {
|
func (p *PopupInfo) getClosePoint() *PointF {
|
||||||
closeResult := p.ClosePopupsResult
|
closeResult := p.ClosePopupsResult
|
||||||
if closeResult == nil {
|
if closeResult == nil {
|
||||||
return nil, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 弹框不存在 && 关闭按钮不存在
|
// 弹窗关闭按钮不存在
|
||||||
if closeResult.PopupArea.IsEmpty() && closeResult.CloseArea.IsEmpty() {
|
if closeResult.CloseArea.IsEmpty() {
|
||||||
if p.ClosePoints == nil {
|
return 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 弹窗存在 && 关闭按钮不存在
|
closePoint := closeResult.CloseArea.Center()
|
||||||
if !closeResult.PopupArea.IsEmpty() && closeResult.CloseArea.IsEmpty() {
|
return &closePoint
|
||||||
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))]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dExt *DriverExt) ClosePopupsHandler() (err error) {
|
func (dExt *DriverExt) ClosePopupsHandler() (err error) {
|
||||||
@@ -170,7 +115,6 @@ func (dExt *DriverExt) ClosePopupsHandler() (err error) {
|
|||||||
screenResult, err := dExt.GetScreenResult(
|
screenResult, err := dExt.GetScreenResult(
|
||||||
WithScreenShotUpload(true),
|
WithScreenShotUpload(true),
|
||||||
WithScreenShotClosePopups(true), // get popup area and close area
|
WithScreenShotClosePopups(true), // get popup area and close area
|
||||||
WithScreenShotUITypes("close"), // get all close buttons
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("get screen result failed for popup handler")
|
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
|
popup := screenResult.Popup
|
||||||
|
closePoint := popup.getClosePoint()
|
||||||
defer func() {
|
|
||||||
dExt.lastPopup = popup
|
|
||||||
}()
|
|
||||||
|
|
||||||
closePoint, err := popup.getClosePoint(dExt.lastPopup)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if closePoint == nil {
|
if closePoint == nil {
|
||||||
// close point not found
|
// close point not found
|
||||||
log.Debug().Msg("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())
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user