fix: use log file when not found log content

This commit is contained in:
huangbin.beal
2023-11-29 21:08:06 +08:00
parent c690b86f2e
commit b41aa6fde6
5 changed files with 263 additions and 240 deletions

View File

@@ -22,15 +22,18 @@ var (
const (
ResultsDirName = "results"
ScreenshotsDirName = "screenshots"
ActionLogDireName = "action_log"
)
var (
RootDir string
ResultsDir string
ResultsPath string
ScreenShotsPath string
StartTime = time.Now()
StartTimeStr = StartTime.Format("20060102150405")
RootDir string
ResultsDir string
ResultsPath string
ScreenShotsPath string
StartTime = time.Now()
StartTimeStr = StartTime.Format("20060102150405")
ActionLogFilePath string
DeviceActionLogFilePath string
)
func init() {
@@ -43,4 +46,6 @@ func init() {
ResultsDir = filepath.Join(ResultsDirName, StartTimeStr)
ResultsPath = filepath.Join(RootDir, ResultsDir)
ScreenShotsPath = filepath.Join(ResultsPath, ScreenshotsDirName)
ActionLogFilePath = filepath.Join(ResultsDir, ActionLogDireName)
DeviceActionLogFilePath = "/sdcard/Android/data/io.appium.uiautomator2.server/files/hodor"
}

View File

@@ -3,6 +3,9 @@ package uixt
import (
"bytes"
"fmt"
"io/fs"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"time"
@@ -10,7 +13,9 @@ import (
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/httprunner/funplugin/myexec"
"github.com/httprunner/httprunner/v4/hrp/internal/code"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/pkg/gadb"
)
@@ -448,7 +453,40 @@ func (ad *adbDriver) StopCaptureLog() (result interface{}, err error) {
}
content := ad.logcat.logBuffer.String()
log.Info().Str("logcat content", content).Msg("display logcat content")
return ConvertPoints(content), nil
pointRes := ConvertPoints(content)
// 没有解析到打点日志,走兜底逻辑
if len(pointRes) == 0 {
logFilePathPrefix := fmt.Sprintf("%v/data", env.ActionLogFilePath)
files := []string{}
myexec.RunCommand("adb", "pull", env.DeviceActionLogFilePath, env.ActionLogFilePath)
err = filepath.Walk(env.ActionLogFilePath, func(path string, info fs.FileInfo, err error) error {
// 只是需要日志文件
if ok := strings.Contains(path, logFilePathPrefix); ok {
files = append(files, path)
}
return nil
})
// 先保持原有状态码不变这里不return error
if err != nil {
log.Error().Err(err).Msg("read log file fail")
return pointRes, nil
}
if len(files) != 1 {
log.Error().Err(err).Msg("log file count error")
return pointRes, nil
}
data, err := ioutil.ReadFile(files[0])
if err != nil {
log.Info().Msg("read File error")
return pointRes, nil
}
pointRes = ConvertPoints(string(data))
}
return pointRes, nil
}
func (ad *adbDriver) GetForegroundApp() (app AppInfo, err error) {

View File

@@ -18,6 +18,7 @@ import (
"github.com/gorilla/websocket"
"github.com/httprunner/funplugin"
"github.com/httprunner/funplugin/myexec"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
@@ -25,6 +26,7 @@ import (
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/httprunner/httprunner/v4/hrp/internal/code"
"github.com/httprunner/httprunner/v4/hrp/internal/env"
"github.com/httprunner/httprunner/v4/hrp/internal/sdk"
"github.com/httprunner/httprunner/v4/hrp/internal/version"
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
@@ -523,6 +525,11 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) error {
config := r.caseRunner.testCase.Config
log.Info().Str("testcase", config.Name).Msg("run testcase start")
// 安卓系统删除打点日志文件
if r.caseRunner.testCase.Config.Android != nil {
myexec.RunCommand("adb", "-s", r.caseRunner.testCase.Config.Android[0].SerialNumber, "shell", "rm", "-r", env.DeviceActionLogFilePath)
}
// update config variables with given variables
r.InitWithParameters(givenVars)