From bfbd4ba6e609a5a6991517db85175cb3585749a8 Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Sat, 14 Sep 2024 10:17:16 +0800 Subject: [PATCH] fix: get screenshot info with retry --- hrp/pkg/uixt/screenshot.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/hrp/pkg/uixt/screenshot.go b/hrp/pkg/uixt/screenshot.go index f6a13890..d7ebb69f 100644 --- a/hrp/pkg/uixt/screenshot.go +++ b/hrp/pkg/uixt/screenshot.go @@ -50,15 +50,19 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S var imageResult *ImageResult var imagePath string var windowSize Size + var lastErr error + + // get screenshot info with retry for i := 0; i < actionOptions.MaxRetryTimes; i++ { bufSource, imagePath, err = dExt.GetScreenShot(fileName) if err != nil { + lastErr = err continue } windowSize, err = dExt.Driver.WindowSize() if err != nil { - err = errors.Wrap(code.MobileUIDriverError, err.Error()) + lastErr = errors.Wrap(code.MobileUIDriverError, err.Error()) continue } @@ -71,13 +75,15 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S imageResult, err = dExt.ImageService.GetImage(bufSource, options...) if err != nil { log.Error().Err(err).Msg("GetImage from ImageService failed") + lastErr = err continue } - // success + // success, break the loop + lastErr = nil break } - if err != nil { - return nil, err + if lastErr != nil { + return nil, lastErr } // cache screen result