feat: add AIAsert

This commit is contained in:
lilong.129
2025-04-27 22:25:06 +08:00
parent 84ff75c3b1
commit 9bcdd5d19a
11 changed files with 523 additions and 62 deletions

View File

@@ -11,6 +11,7 @@ import (
"github.com/httprunner/httprunner/v5/internal/config"
"github.com/httprunner/httprunner/v5/uixt/ai"
"github.com/httprunner/httprunner/v5/uixt/option"
"github.com/httprunner/httprunner/v5/uixt/types"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
@@ -59,7 +60,40 @@ func (dExt *XTDriver) AIQuery(text string, opts ...option.ActionOption) (string,
return "", nil
}
func (dExt *XTDriver) AIAssert(text string, opts ...option.ActionOption) error {
func (dExt *XTDriver) AIAssert(assertion string, opts ...option.ActionOption) error {
if dExt.LLMService == nil {
return errors.New("LLM service is not initialized")
}
compressedBufSource, err := dExt.GetScreenShotBuffer()
if err != nil {
return err
}
// convert buffer to base64 string
screenShotBase64 := "data:image/jpeg;base64," +
base64.StdEncoding.EncodeToString(compressedBufSource.Bytes())
// get window size
size, err := dExt.IDriver.WindowSize()
if err != nil {
return errors.Wrap(err, "get window size for AI assertion failed")
}
// execute assertion
result, err := dExt.LLMService.Assert(&ai.AssertOptions{
Assertion: assertion,
Screenshot: screenShotBase64,
Size: types.Size{Width: size.Width, Height: size.Height},
})
if err != nil {
return errors.Wrap(err, "AI assertion failed")
}
if !result.Pass {
return errors.New(result.Thought)
}
return nil
}