refactor: NewXTDriver api, return error if init failed

This commit is contained in:
lilong.129
2025-04-30 14:27:37 +08:00
parent 2ae252b52a
commit 0e9389c796
21 changed files with 146 additions and 146 deletions

View File

@@ -1,13 +0,0 @@
package ai
import (
"testing"
)
func TestOption(t *testing.T) {
options := NewAIService(
WithCVService(CVServiceTypeOpenCV),
WithLLMService(LLMServiceTypeUITARS),
)
t.Log(options)
}

View File

@@ -14,6 +14,7 @@ import (
"github.com/getkin/kin-openapi/openapi3gen"
"github.com/httprunner/httprunner/v5/code"
"github.com/httprunner/httprunner/v5/internal/json"
"github.com/httprunner/httprunner/v5/uixt/option"
"github.com/httprunner/httprunner/v5/uixt/types"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
@@ -57,11 +58,11 @@ func NewAsserter(ctx context.Context) (*Asserter, error) {
return nil, err
}
if strings.Contains(EnvModelUse, string(LLMServiceTypeUITARS)) {
if strings.Contains(EnvModelUse, string(option.LLMServiceTypeUITARS)) {
asserter.systemPrompt += "\n\n" + uiTarsAssertionResponseFormat
} else if strings.Contains(EnvModelUse, string(LLMServiceTypeQwenVL)) {
} else if strings.Contains(EnvModelUse, string(option.LLMServiceTypeQwenVL)) {
asserter.systemPrompt += "\n\n" + defaultAssertionResponseJsonFormat
} else if strings.Contains(EnvModelUse, string(LLMServiceTypeGPT)) {
} else if strings.Contains(EnvModelUse, string(option.LLMServiceTypeGPT)) {
// define output format
type OutputFormat struct {
Thought string `json:"thought"`

View File

@@ -22,6 +22,13 @@ type ICVService interface {
ReadFromPath(imagePath string, opts ...option.ActionOption) (*CVResult, error)
}
func NewCVService(modelType option.CVServiceType) (ICVService, error) {
if modelType == option.CVServiceTypeVEDEM {
return NewVEDEMImageService()
}
return nil, errors.New("invalid cv service type")
}
type CVResult struct {
URL string `json:"url,omitempty"` // image uploaded url
OCRResult OCRResults `json:"ocrResult,omitempty"` // OCR texts

View File

@@ -19,9 +19,8 @@ func TestGetImageFromBuffer(t *testing.T) {
buf := new(bytes.Buffer)
buf.Read(file)
service := NewAIService(
WithCVService(CVServiceTypeVEDEM),
)
service, err := NewVEDEMImageService()
require.Nil(t, err)
cvResult, err := service.ReadFromBuffer(buf)
assert.Nil(t, err)
fmt.Println(fmt.Sprintf("cvResult: %v", cvResult))
@@ -29,9 +28,8 @@ func TestGetImageFromBuffer(t *testing.T) {
func TestGetImageFromPath(t *testing.T) {
imagePath := "/Users/debugtalk/Downloads/s1.png"
service := NewAIService(
WithCVService(CVServiceTypeVEDEM),
)
service, err := NewVEDEMImageService()
require.Nil(t, err)
cvResult, err := service.ReadFromPath(imagePath)
assert.Nil(t, err)
fmt.Println(fmt.Sprintf("cvResult: %v", cvResult))

View File

@@ -11,7 +11,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/httprunner/httprunner/v5/uixt/ai"
"github.com/httprunner/httprunner/v5/uixt/option"
"github.com/httprunner/httprunner/v5/uixt/types"
)
@@ -23,10 +22,12 @@ func setupADBDriverExt(t *testing.T) *XTDriver {
device.Options.LogOn = false
driver, err := device.NewDriver()
require.Nil(t, err)
return NewXTDriver(driver,
ai.WithCVService(ai.CVServiceTypeVEDEM),
ai.WithLLMService(ai.LLMServiceTypeUITARS),
driverExt, err := NewXTDriver(driver,
option.WithCVService(option.CVServiceTypeVEDEM),
option.WithLLMService(option.LLMServiceTypeUITARS),
)
require.Nil(t, err)
return driverExt
}
func setupUIA2DriverExt(t *testing.T) *XTDriver {
@@ -36,8 +37,10 @@ func setupUIA2DriverExt(t *testing.T) *XTDriver {
device.Options.LogOn = false
driver, err := device.NewDriver()
require.Nil(t, err)
return NewXTDriver(driver,
ai.WithCVService(ai.CVServiceTypeVEDEM))
driverExt, err := NewXTDriver(driver,
option.WithCVService(option.CVServiceTypeVEDEM))
require.Nil(t, err)
return driverExt
}
func TestDevice_Android_GetPackageInfo(t *testing.T) {

View File

@@ -9,7 +9,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/httprunner/httprunner/v5/uixt"
"github.com/httprunner/httprunner/v5/uixt/ai"
"github.com/httprunner/httprunner/v5/uixt/option"
)
@@ -23,9 +22,10 @@ func TestIOSDemo(t *testing.T) {
driver, err := device.NewDriver()
assert.Nil(t, err)
driverExt := uixt.NewXTDriver(driver,
ai.WithCVService(ai.CVServiceTypeVEDEM),
driverExt, err := uixt.NewXTDriver(driver,
option.WithCVService(option.CVServiceTypeVEDEM),
)
assert.Nil(t, err)
// release session
defer func() {

View File

@@ -8,6 +8,7 @@ import (
"github.com/httprunner/httprunner/v5/uixt/ai"
"github.com/httprunner/httprunner/v5/uixt/option"
"github.com/httprunner/httprunner/v5/uixt/types"
"github.com/rs/zerolog/log"
)
var (
@@ -81,16 +82,31 @@ type IDriver interface {
StopCaptureLog() (result interface{}, err error)
}
func NewXTDriver(driver IDriver, opts ...ai.AIServiceOption) *XTDriver {
services := ai.NewAIService(opts...)
func NewXTDriver(driver IDriver, opts ...option.AIServiceOption) (*XTDriver, error) {
driverExt := &XTDriver{
IDriver: driver,
CVService: services.ICVService,
LLMService: services.ILLMService,
IDriver: driver,
screenResults: make([]*ScreenResult, 0),
}
return driverExt
services := option.NewAIServiceOptions(opts...)
var err error
if services.CVService != "" {
driverExt.CVService, err = ai.NewCVService(services.CVService)
if err != nil {
log.Error().Err(err).Msg("init vedem image service failed")
return nil, err
}
}
if services.LLMService != "" {
driverExt.LLMService, err = ai.NewLLMService(services.LLMService)
if err != nil {
log.Error().Err(err).Msg("init llm service failed")
return nil, err
}
}
return driverExt, nil
}
// XTDriver = IDriver + AI

View File

@@ -6,7 +6,6 @@ import (
"testing"
"time"
"github.com/httprunner/httprunner/v5/uixt/ai"
"github.com/httprunner/httprunner/v5/uixt/option"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -17,8 +16,9 @@ func TestDriverExt_NewMethod1(t *testing.T) {
require.Nil(t, err)
driver, err := device.NewDriver()
require.Nil(t, err)
driverExt := NewXTDriver(driver,
ai.WithCVService(ai.CVServiceTypeVEDEM))
driverExt, err := NewXTDriver(driver,
option.WithCVService(option.CVServiceTypeVEDEM))
require.Nil(t, err)
driverExt.TapByOCR("推荐")
}
@@ -27,16 +27,18 @@ func TestDriverExt_NewMethod2(t *testing.T) {
require.Nil(t, err)
driver, err := NewUIA2Driver(device)
require.Nil(t, err)
driverExt := NewXTDriver(driver,
ai.WithCVService(ai.CVServiceTypeVEDEM))
driverExt, err := NewXTDriver(driver,
option.WithCVService(option.CVServiceTypeVEDEM))
require.Nil(t, err)
driverExt.TapByOCR("推荐")
}
func TestDriverExt(t *testing.T) {
device, _ := NewAndroidDevice()
driver, _ := NewADBDriver(device)
driverExt := NewXTDriver(driver,
ai.WithCVService(ai.CVServiceTypeVEDEM))
driverExt, err := NewXTDriver(driver,
option.WithCVService(option.CVServiceTypeVEDEM))
require.Nil(t, err)
// call IDriver methods
driverExt.TapXY(0.2, 0.5)
@@ -50,7 +52,7 @@ func TestDriverExt(t *testing.T) {
textRect, _ := driverExt.FindScreenText("hello")
t.Log(textRect)
err := driverExt.TapByCV(
err = driverExt.TapByCV(
option.WithScreenShotUITypes("deepseek_send"),
option.WithScope(0.8, 0.5, 1, 1))
assert.Nil(t, err)

View File

@@ -6,7 +6,7 @@ import (
"fmt"
"testing"
"github.com/httprunner/httprunner/v5/uixt/ai"
"github.com/httprunner/httprunner/v5/uixt/option"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -16,7 +16,9 @@ func setupHDCDriverExt(t *testing.T) *XTDriver {
require.Nil(t, err)
hdcDriver, err := NewHDCDriver(device)
require.Nil(t, err)
return NewXTDriver(hdcDriver, ai.WithCVService(ai.CVServiceTypeVEDEM))
driverExt, err := NewXTDriver(hdcDriver, option.WithCVService(option.CVServiceTypeVEDEM))
require.Nil(t, err)
return driverExt
}
func TestWindowSize(t *testing.T) {

View File

@@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/httprunner/httprunner/v5/uixt/ai"
"github.com/httprunner/httprunner/v5/uixt/option"
"github.com/httprunner/httprunner/v5/uixt/types"
)
@@ -23,7 +22,9 @@ func setupWDADriverExt(t *testing.T) *XTDriver {
require.Nil(t, err)
driver, err := device.NewDriver()
require.Nil(t, err)
return NewXTDriver(driver, ai.WithCVService(ai.CVServiceTypeVEDEM))
driverExt, err := NewXTDriver(driver, option.WithCVService(option.CVServiceTypeVEDEM))
require.Nil(t, err)
return driverExt
}
func TestDevice_IOS_Install(t *testing.T) {

43
uixt/option/ai.go Normal file
View File

@@ -0,0 +1,43 @@
package option
func NewAIServiceOptions(opts ...AIServiceOption) *AIServiceOptions {
services := &AIServiceOptions{}
for _, option := range opts {
option(services)
}
return services
}
type AIServiceOptions struct {
CVService CVServiceType
LLMService LLMServiceType
}
type AIServiceOption func(*AIServiceOptions)
type CVServiceType string
const (
CVServiceTypeVEDEM CVServiceType = "vedem"
CVServiceTypeOpenCV CVServiceType = "opencv"
)
func WithCVService(service CVServiceType) AIServiceOption {
return func(opts *AIServiceOptions) {
opts.CVService = service
}
}
type LLMServiceType string
const (
LLMServiceTypeUITARS LLMServiceType = "ui-tars"
LLMServiceTypeGPT LLMServiceType = "gpt"
LLMServiceTypeQwenVL LLMServiceType = "qwen-vl"
)
func WithLLMService(modelType LLMServiceType) AIServiceOption {
return func(opts *AIServiceOptions) {
opts.LLMService = modelType
}
}