refactor: handle text popups

This commit is contained in:
lilong.129
2023-08-22 14:17:43 +08:00
parent 75158b4e58
commit cc3aedb810
3 changed files with 35 additions and 18 deletions

View File

@@ -24,7 +24,7 @@ var popups = [][]string{
{"管理使用时间", ".*忽略.*"},
}
func (dExt *DriverExt) AutoPopupHandler(screenTexts OCRTexts) error {
func findTextPopup(screenTexts OCRTexts) (closePoint *OCRText) {
for _, popup := range popups {
if len(popup) != 2 {
continue
@@ -34,17 +34,38 @@ func (dExt *DriverExt) AutoPopupHandler(screenTexts OCRTexts) error {
if err == nil {
log.Warn().Interface("popup", popup).
Interface("texts", screenTexts).Msg("text popup found")
point := points[1].Center()
log.Info().Str("text", points[1].Text).Msg("close popup")
if err := dExt.TapAbsXY(point.X, point.Y); err != nil {
log.Error().Err(err).Msg("tap popup failed")
return errors.Wrap(code.MobileUIPopupError, err.Error())
}
// tap popup success
return nil
closePoint = &points[1]
break
}
}
return
}
// no popup found
func (dExt *DriverExt) handleTextPopup(screenTexts OCRTexts) error {
closePoint := findTextPopup(screenTexts)
if closePoint == nil {
// no popup found
return nil
}
log.Info().Str("text", closePoint.Text).Msg("close popup")
pointCenter := closePoint.Center()
if err := dExt.TapAbsXY(pointCenter.X, pointCenter.Y); err != nil {
log.Error().Err(err).Msg("tap popup failed")
return errors.Wrap(code.MobileUIPopupError, err.Error())
}
// tap popup success
return nil
}
func (dExt *DriverExt) AutoPopupHandler() error {
// TODO: check popup by activity type
// check popup by screenshot
screenResult, err := dExt.GetScreenResult()
if err != nil {
return errors.Wrap(err, "get screen result failed for popup handler")
}
return dExt.handleTextPopup(screenResult.Texts)
}

View File

@@ -143,7 +143,7 @@ func (dExt *DriverExt) swipeToTapTexts(texts []string, options ...ActionOption)
if err != nil {
log.Error().Err(err).Msg("swipeToTapTexts failed")
// target texts not found, try to auto handle popup
if e := dExt.AutoPopupHandler(screenTexts); e != nil {
if e := dExt.handleTextPopup(screenTexts); e != nil {
log.Error().Err(e).Msg("auto handle popup failed")
}
return err

View File

@@ -603,13 +603,9 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err
}
}
// take screenshot and get screen texts by OCR
screenTexts, err2 := uiDriver.GetScreenTexts()
if err2 != nil {
log.Error().Err(err2).Str("step", step.Name).Msg("take screenshot failed on step finished")
} else if err3 := uiDriver.AutoPopupHandler(screenTexts); err3 != nil {
// automatic handling of pop-up windows on each step finished
log.Error().Err(err3).Msg("auto handle popup failed")
// automatic handling of pop-up windows on each step finished
if err = uiDriver.AutoPopupHandler(); err != nil {
log.Error().Err(err).Str("step", step.Name).Msg("auto handle popup failed")
}
// save attachments