mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-28 11:01:30 +08:00
fix: handle popup close
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -121,9 +121,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) {
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -651,7 +651,7 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err
|
||||
|
||||
// automatic handling of pop-up windows on each step finished
|
||||
if !step.IgnorePopup && !s.IgnorePopup() {
|
||||
if err2 := uiDriver.ClosePopups(); err2 != nil {
|
||||
if err2 := uiDriver.ClosePopupsHandler(); err2 != nil {
|
||||
log.Error().Err(err2).Str("step", step.Name).Msg("auto handle popup failed")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user