mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
Merge branch 'fix-popup' into 'master'
Fix popup See merge request iesqa/httprunner!11
This commit is contained in:
+1
-13
@@ -668,19 +668,7 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) {
|
|||||||
}
|
}
|
||||||
return dExt.VideoCrawler(configs)
|
return dExt.VideoCrawler(configs)
|
||||||
case ACTION_ClosePopups:
|
case ACTION_ClosePopups:
|
||||||
options := action.GetOptions()
|
return dExt.ClosePopupsHandler()
|
||||||
actionOptions := NewActionOptions(options...)
|
|
||||||
|
|
||||||
// default to retry 3 times
|
|
||||||
if actionOptions.MaxRetryTimes == 0 {
|
|
||||||
options = append(options, WithMaxRetryTimes(3))
|
|
||||||
}
|
|
||||||
// set default swipe interval to 1 second
|
|
||||||
if builtin.IsZeroFloat64(actionOptions.Interval) {
|
|
||||||
options = append(options, WithInterval(1))
|
|
||||||
}
|
|
||||||
|
|
||||||
return dExt.ClosePopupsHandler(options...)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -116,6 +116,9 @@ 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, plugin funplugin.IPlugin) (dExt *DriverExt, err error) {
|
func newDriverExt(device Device, driver WebDriver, plugin funplugin.IPlugin) (dExt *DriverExt, err error) {
|
||||||
|
|||||||
+79
-62
@@ -88,97 +88,114 @@ type ClosePopupsResult struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PopupInfo struct {
|
type PopupInfo struct {
|
||||||
|
*ClosePopupsResult
|
||||||
CloseStatus string `json:"close_status"` // found/success/fail
|
CloseStatus string `json:"close_status"` // found/success/fail
|
||||||
RetryCount int `json:"retry_count"`
|
|
||||||
CloseBox Box `json:"close_box"` // CV 识别的弹窗关闭按钮(弹窗存在 && 关闭按钮存在)
|
|
||||||
ClosePoints []PointF `json:"close_points,omitempty"` // CV 识别的所有关闭按钮(仅关闭按钮,可能存在多个)
|
ClosePoints []PointF `json:"close_points,omitempty"` // CV 识别的所有关闭按钮(仅关闭按钮,可能存在多个)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PopupInfo) isIdentical(lastPopup *PopupInfo) bool {
|
func (p *PopupInfo) getClosePoint(lastPopup *PopupInfo) (*PointF, error) {
|
||||||
if lastPopup == nil {
|
closeResult := p.ClosePopupsResult
|
||||||
return false
|
if closeResult == nil {
|
||||||
}
|
return nil, nil
|
||||||
if lastPopup.CloseBox.IsEmpty() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if !p.CloseBox.IsIdentical(lastPopup.CloseBox) {
|
|
||||||
lastPopup.CloseStatus = CloseStatusSuccess
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p.CloseStatus = CloseStatusFail
|
// 弹框不存在 & 关闭按钮不存在
|
||||||
lastPopup.CloseStatus = CloseStatusFail
|
if closeResult.PopupArea.IsEmpty() && closeResult.CloseArea.IsEmpty() {
|
||||||
return true
|
if p.ClosePoints == nil {
|
||||||
|
// 关闭图标不存在 => 100% 确定不存在弹窗
|
||||||
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dExt *DriverExt) ClosePopupsHandler(options ...ActionOption) error {
|
// 存在关闭按钮,结合上一次的 popup 进行判断
|
||||||
actionOptions := NewActionOptions(options...)
|
if lastPopup == nil || lastPopup.ClosePoints == nil {
|
||||||
log.Info().Interface("actionOptions", actionOptions).Msg("try to find and close popups")
|
// 当前关闭图标为首次出现,确定是弹窗关闭按钮的概率较小
|
||||||
maxRetryTimes := actionOptions.MaxRetryTimes
|
log.Debug().Interface("closePoints", p.ClosePoints).
|
||||||
interval := actionOptions.Interval
|
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 &p.ClosePoints[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连续两次图标位置不同 => 可能不是弹窗 => skip
|
||||||
|
log.Debug().Interface("closePoints", p.ClosePoints).
|
||||||
|
Interface("lastClosePoints", lastPopup.ClosePoints).
|
||||||
|
Msg("skip close points for not sure")
|
||||||
|
return nil, 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.New("popup close area not found")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用关闭图标作为关闭按钮
|
||||||
|
return &p.ClosePoints[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
closePoint := closeResult.CloseArea.Center()
|
||||||
|
|
||||||
|
// 弹窗不存在 & 关闭按钮存在 => 可能是文字弹窗 => 基于关闭按钮关闭弹窗
|
||||||
|
if closeResult.PopupArea.IsEmpty() && !closeResult.CloseArea.IsEmpty() {
|
||||||
|
return &closePoint, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 弹窗存在 & 关闭按钮存在 => 检测到弹窗存在 => 基于关闭按钮关闭弹窗
|
||||||
|
return &closePoint, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (dExt *DriverExt) ClosePopupsHandler() (err error) {
|
||||||
|
log.Info().Msg("try to find and close popups")
|
||||||
|
|
||||||
var lastPopup *PopupInfo
|
|
||||||
for retryCount := 0; retryCount < maxRetryTimes; retryCount++ {
|
|
||||||
screenResult, err := dExt.GetScreenResult(
|
screenResult, err := dExt.GetScreenResult(
|
||||||
WithScreenShotUpload(true),
|
WithScreenShotUpload(true),
|
||||||
WithScreenShotClosePopups(true),
|
WithScreenShotClosePopups(true), // get popup area and close area
|
||||||
WithScreenShotUITypes("close"), // get all close buttons
|
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")
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
popup := screenResult.Popup
|
|
||||||
if popup == nil || popup.CloseBox.IsEmpty() {
|
|
||||||
log.Debug().Interface("popup", popup).Msg("no popup found")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
popup.CloseStatus = CloseStatusFound
|
|
||||||
popup.RetryCount = retryCount
|
|
||||||
|
|
||||||
// check if the current popup equals to the last popup
|
|
||||||
if popup.isIdentical(lastPopup) {
|
|
||||||
return errors.Wrap(code.MobileUIPopupError, "handle popup failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = dExt.tapPopupHandler(popup); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// sleep for another popup (if existed) to pop
|
popup := screenResult.Popup
|
||||||
time.Sleep(time.Duration(1000*interval) * time.Millisecond)
|
|
||||||
lastPopup = popup
|
defer func() {
|
||||||
|
dExt.lastPopup = popup
|
||||||
|
}()
|
||||||
|
|
||||||
|
closePoint, err := popup.getClosePoint(dExt.lastPopup)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if closePoint == nil {
|
||||||
|
// close point not found
|
||||||
|
log.Debug().Msg("close point not found")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dExt *DriverExt) tapPopupHandler(popup *PopupInfo) error {
|
|
||||||
if popup == nil || popup.CloseBox.IsEmpty() {
|
|
||||||
log.Debug().Interface("popup", popup).Msg("no popup found")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
popup.CloseStatus = CloseStatusFound
|
popup.CloseStatus = CloseStatusFound
|
||||||
|
|
||||||
popupClose := popup.CloseBox
|
|
||||||
if popupClose.IsEmpty() {
|
|
||||||
log.Error().
|
|
||||||
Interface("popup", popup).
|
|
||||||
Msg("popup close area not found")
|
|
||||||
return errors.Wrap(code.MobileUIPopupError,
|
|
||||||
"popup close area not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
closePoint := popupClose.Center()
|
|
||||||
log.Info().
|
log.Info().
|
||||||
Interface("popup", popup).
|
|
||||||
Interface("closePoint", closePoint).
|
Interface("closePoint", closePoint).
|
||||||
|
Interface("popup", popup).
|
||||||
Msg("tap to close popup")
|
Msg("tap to close popup")
|
||||||
if err := dExt.TapAbsXY(closePoint.X, closePoint.Y); err != nil {
|
if err := dExt.TapAbsXY(closePoint.X, closePoint.Y); err != nil {
|
||||||
log.Error().Err(err).Msg("tap popup failed")
|
log.Error().Err(err).Msg("tap popup failed")
|
||||||
return errors.Wrap(code.MobileUIPopupError, err.Error())
|
return errors.Wrap(code.MobileUIPopupError, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
// tap popup success
|
// wait 1s and check if popup still exists
|
||||||
return nil
|
log.Info().Msg("tap close point success, check if popup still exists")
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
return dExt.ClosePopupsHandler()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -408,11 +408,8 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
|
|||||||
screenResult.Icons = imageResult.UIResult
|
screenResult.Icons = imageResult.UIResult
|
||||||
|
|
||||||
if actionOptions.ScreenShotWithClosePopups {
|
if actionOptions.ScreenShotWithClosePopups {
|
||||||
popup := &PopupInfo{}
|
popup := &PopupInfo{
|
||||||
|
ClosePopupsResult: imageResult.ClosePopupsResult,
|
||||||
closeResult := imageResult.ClosePopupsResult
|
|
||||||
if closeResult != nil && !closeResult.PopupArea.IsEmpty() && !closeResult.CloseArea.IsEmpty() {
|
|
||||||
popup.CloseBox = closeResult.CloseArea
|
|
||||||
}
|
}
|
||||||
|
|
||||||
closeAreas, _ := imageResult.UIResult.FilterUIResults([]string{"close"})
|
closeAreas, _ := imageResult.UIResult.FilterUIResults([]string{"close"})
|
||||||
|
|||||||
@@ -102,21 +102,7 @@ func TestDriverExtOCR(t *testing.T) {
|
|||||||
func TestClosePopup(t *testing.T) {
|
func TestClosePopup(t *testing.T) {
|
||||||
setupAndroid(t)
|
setupAndroid(t)
|
||||||
|
|
||||||
screenResult, err := driverExt.GetScreenResult(
|
if err := driverExt.ClosePopupsHandler(); err != nil {
|
||||||
WithScreenShotClosePopups(true), WithScreenShotUpload(true))
|
|
||||||
if err != nil {
|
|
||||||
t.Logf("get screen result failed for popup handler: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
t.Logf("screen result: %v", screenResult)
|
|
||||||
|
|
||||||
if screenResult.Popup == nil {
|
|
||||||
t.Log("there are no popups here")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
t.Logf("popup info: %v", screenResult.Popup)
|
|
||||||
|
|
||||||
if err = driverExt.tapPopupHandler(screenResult.Popup); err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -143,7 +143,6 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption)
|
|||||||
screenResult, err := d.GetScreenResult(
|
screenResult, err := d.GetScreenResult(
|
||||||
WithScreenShotOCR(true),
|
WithScreenShotOCR(true),
|
||||||
WithScreenShotUpload(true),
|
WithScreenShotUpload(true),
|
||||||
WithScreenShotClosePopups(true),
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -153,8 +152,8 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Strs("texts", texts).Msg("find texts failed")
|
log.Error().Err(err).Strs("texts", texts).Msg("find texts failed")
|
||||||
// target texts not found, try to auto handle popup
|
// target texts not found, try to auto handle popup
|
||||||
if e := dExt.tapPopupHandler(screenResult.Popup); e != nil {
|
if e := dExt.ClosePopupsHandler(); e != nil {
|
||||||
log.Error().Err(e).Msg("tap popup handler failed")
|
log.Error().Err(e).Msg("run popup handler failed")
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -178,20 +178,22 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
|||||||
// retry 10 times if get feed failed, abort if fail 10 consecutive times
|
// retry 10 times if get feed failed, abort if fail 10 consecutive times
|
||||||
currentVideo, err := crawler.getCurrentVideo()
|
currentVideo, err := crawler.getCurrentVideo()
|
||||||
if err != nil || currentVideo.Type == "" {
|
if err != nil || currentVideo.Type == "" {
|
||||||
|
crawler.failedCount++
|
||||||
if crawler.failedCount >= 10 {
|
if crawler.failedCount >= 10 {
|
||||||
// failed 10 consecutive times
|
// failed 10 consecutive times
|
||||||
return errors.Wrap(code.TrackingGetError,
|
return errors.Wrap(code.TrackingGetError,
|
||||||
"get current feed video failed 10 consecutive times")
|
"get current feed video failed 10 consecutive times")
|
||||||
}
|
}
|
||||||
log.Warn().Msg("get current feed video failed")
|
log.Warn().
|
||||||
|
Int64("failedCount", crawler.failedCount).
|
||||||
|
Msg("get current feed video failed")
|
||||||
|
|
||||||
// check and handle popups
|
// check and handle popups
|
||||||
if err := crawler.driverExt.ClosePopupsHandler(WithMaxRetryTimes(1)); err != nil {
|
if err := crawler.driverExt.ClosePopupsHandler(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// retry
|
// retry
|
||||||
crawler.failedCount++
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,17 +244,12 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
|||||||
WithScreenShotOCR(true),
|
WithScreenShotOCR(true),
|
||||||
WithScreenShotUpload(true),
|
WithScreenShotUpload(true),
|
||||||
WithScreenShotLiveType(true),
|
WithScreenShotLiveType(true),
|
||||||
WithScreenShotClosePopups(true),
|
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("get screen result failed")
|
log.Error().Err(err).Msg("get screen result failed")
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if e := crawler.driverExt.tapPopupHandler(screenResult.Popup); e != nil {
|
|
||||||
log.Error().Err(e).Msg("close live popup failed")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// add live type
|
// add live type
|
||||||
if screenResult.imageResult != nil &&
|
if screenResult.imageResult != nil &&
|
||||||
|
|||||||
@@ -624,7 +624,7 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// automatic handling of pop-up windows on each step finished
|
// automatic handling of pop-up windows on each step finished
|
||||||
if err2 := uiDriver.ClosePopupsHandler(uixt.WithMaxRetryTimes(3), uixt.WithInterval(1)); err2 != nil {
|
if err2 := uiDriver.ClosePopupsHandler(); err2 != nil {
|
||||||
log.Error().Err(err2).Str("step", step.Name).Msg("handle popup failed on step finished")
|
log.Error().Err(err2).Str("step", step.Name).Msg("handle popup failed on step finished")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user