mirror of
https://github.com/httprunner/httprunner.git
synced 2026-08-28 11:36:56 +08:00
feat: support new action: close_popups
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
)
|
||||
|
||||
@@ -70,3 +73,75 @@ func (dExt *DriverExt) AutoPopupHandler() error {
|
||||
|
||||
return dExt.handleTextPopup(screenResult.Texts)
|
||||
}
|
||||
|
||||
// CRResult represents the result of recognized popup to close
|
||||
type CPResult struct {
|
||||
Type string `json:"type"`
|
||||
PopupArea Box `json:"popupArea"`
|
||||
CloseArea Box `json:"closeArea"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type PopupInfo struct {
|
||||
// TODO: support more types of popup report info
|
||||
// Status bool `json:"status"`
|
||||
Type string `json:"type"`
|
||||
RetryCount int `json:"retry_count"`
|
||||
PicName string `json:"pic_name"`
|
||||
PicURL string `json:"pic_url"`
|
||||
Point PointF `json:"point"`
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) ClosePopup(options ...ActionOption) error {
|
||||
actionOptions := NewActionOptions(options...)
|
||||
|
||||
// default to retry 5 times
|
||||
if actionOptions.MaxRetryTimes == 0 {
|
||||
options = append(options, WithMaxRetryTimes(5))
|
||||
}
|
||||
// set default swipe interval to 1 second
|
||||
if builtin.IsZeroFloat64(actionOptions.Interval) {
|
||||
options = append(options, WithInterval(1))
|
||||
}
|
||||
return dExt.ClosePopupHandler(options...)
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) ClosePopupHandler(options ...ActionOption) error {
|
||||
actionOptions := NewActionOptions(options...)
|
||||
maxRetryTimes := actionOptions.MaxRetryTimes
|
||||
interval := actionOptions.Interval
|
||||
|
||||
for retryCount := 1; retryCount <= maxRetryTimes; retryCount++ {
|
||||
screenResult, err := dExt.GetScreenResult(
|
||||
WithScreenShotClose(true), WithScreenShotUpload(true))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "get screen result failed for popup handler")
|
||||
}
|
||||
|
||||
// not popup, fast return
|
||||
if screenResult.Popup == nil {
|
||||
break
|
||||
}
|
||||
|
||||
if err = dExt.tapPopupHandler(screenResult.Popup); err != nil {
|
||||
return err
|
||||
}
|
||||
// sleep for another popup (if existed) to pop
|
||||
time.Sleep(time.Duration(1000*interval) * time.Millisecond)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) tapPopupHandler(cpResult *CPResult) error {
|
||||
if cpResult == nil {
|
||||
return nil
|
||||
}
|
||||
log.Info().Str("type", cpResult.Type).Str("text", cpResult.Text).Msg("close popup")
|
||||
popupCenter := cpResult.CloseArea.Center()
|
||||
if err := dExt.TapAbsXY(popupCenter.X, popupCenter.Y); err != nil {
|
||||
log.Error().Err(err).Msg("tap popup failed")
|
||||
return errors.Wrap(code.MobileUIPopupError, err.Error())
|
||||
}
|
||||
// tap popup success
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user