mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-24 01:40:02 +08:00
merge branch merge-from-video
This commit is contained in:
@@ -108,7 +108,7 @@ func (c Client) DeviceList() (devices []*Device, err error) {
|
||||
}
|
||||
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) < 5 || len(fields[0]) == 0 {
|
||||
if len(fields) < 4 || len(fields[0]) == 0 {
|
||||
log.Error().Str("line", line).Msg("get unexpected line")
|
||||
continue
|
||||
}
|
||||
@@ -117,6 +117,9 @@ func (c Client) DeviceList() (devices []*Device, err error) {
|
||||
mapAttrs := map[string]string{}
|
||||
for _, field := range sliceAttrs {
|
||||
split := strings.Split(field, ":")
|
||||
if len(split) == 1 {
|
||||
continue
|
||||
}
|
||||
key, val := split[0], split[1]
|
||||
mapAttrs[key] = val
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -107,20 +108,50 @@ func (d *Device) features() (features Features, err error) {
|
||||
return features, nil
|
||||
}
|
||||
|
||||
func (d *Device) Product() string {
|
||||
return d.attrs["product"]
|
||||
func (d *Device) HasAttribute(key string) bool {
|
||||
_, ok := d.attrs[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (d *Device) Model() string {
|
||||
return d.attrs["model"]
|
||||
func (d *Device) Product() (string, error) {
|
||||
if d.HasAttribute("product") {
|
||||
return d.attrs["product"], nil
|
||||
}
|
||||
return "", errors.New("does not have attribute: product")
|
||||
}
|
||||
|
||||
func (d *Device) Usb() string {
|
||||
return d.attrs["usb"]
|
||||
func (d *Device) Model() (string, error) {
|
||||
if d.HasAttribute("model") {
|
||||
return d.attrs["model"], nil
|
||||
}
|
||||
return "", errors.New("does not have attribute: model")
|
||||
}
|
||||
|
||||
func (d *Device) transportId() string {
|
||||
return d.attrs["transport_id"]
|
||||
func (d *Device) Brand() (string, error) {
|
||||
if d.HasAttribute("brand") {
|
||||
return d.attrs["brand"], nil
|
||||
}
|
||||
brand, err := d.RunShellCommand("getprop", "ro.product.brand")
|
||||
brand = strings.TrimSpace(brand)
|
||||
if err != nil {
|
||||
return "", errors.New("does not have attribute: brand")
|
||||
}
|
||||
d.attrs["brand"] = brand
|
||||
return brand, nil
|
||||
}
|
||||
|
||||
func (d *Device) Usb() (string, error) {
|
||||
if d.HasAttribute("usb") {
|
||||
return d.attrs["usb"], nil
|
||||
}
|
||||
return "", errors.New("does not have attribute: usb")
|
||||
}
|
||||
|
||||
func (d *Device) transportId() (string, error) {
|
||||
if d.HasAttribute("transport_id") {
|
||||
return d.attrs["transport_id"], nil
|
||||
}
|
||||
return "", errors.New("does not have attribute: transport_id")
|
||||
}
|
||||
|
||||
func (d *Device) DeviceInfo() map[string]string {
|
||||
@@ -132,8 +163,13 @@ func (d *Device) Serial() string {
|
||||
return d.serial
|
||||
}
|
||||
|
||||
func (d *Device) IsUsb() bool {
|
||||
return d.Usb() != ""
|
||||
func (d *Device) IsUsb() (bool, error) {
|
||||
usb, err := d.Usb()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return usb != "", nil
|
||||
}
|
||||
|
||||
func (d *Device) State() (DeviceState, error) {
|
||||
@@ -146,10 +182,8 @@ func (d *Device) DevicePath() (string, error) {
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (d *Device) Forward(localPort int, remoteInterface interface{}, noRebind ...bool) (err error) {
|
||||
command := ""
|
||||
func (d *Device) Forward(remoteInterface interface{}, noRebind ...bool) (port int, err error) {
|
||||
var remote string
|
||||
local := fmt.Sprintf("tcp:%d", localPort)
|
||||
switch r := remoteInterface.(type) {
|
||||
// for unix sockets
|
||||
case string:
|
||||
@@ -158,14 +192,28 @@ func (d *Device) Forward(localPort int, remoteInterface interface{}, noRebind ..
|
||||
remote = fmt.Sprintf("tcp:%d", r)
|
||||
}
|
||||
|
||||
forwardList, err := d.ForwardList()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, forwardItem := range forwardList {
|
||||
if forwardItem.Remote == remote {
|
||||
return strconv.Atoi(forwardItem.Local[4:])
|
||||
}
|
||||
}
|
||||
localPort, err := builtin.GetFreePort()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
local := fmt.Sprintf("tcp:%d", localPort)
|
||||
|
||||
command := fmt.Sprintf("host-serial:%s:forward:%s;%s", d.serial, local, remote)
|
||||
if len(noRebind) != 0 && noRebind[0] {
|
||||
command = fmt.Sprintf("host-serial:%s:forward:norebind:%s;%s", d.serial, local, remote)
|
||||
} else {
|
||||
command = fmt.Sprintf("host-serial:%s:forward:%s;%s", d.serial, local, remote)
|
||||
}
|
||||
|
||||
_, err = d.adbClient.executeCommand(command, true)
|
||||
return
|
||||
return localPort, nil
|
||||
}
|
||||
|
||||
func (d *Device) ForwardList() (deviceForwardList []DeviceForward, err error) {
|
||||
@@ -502,7 +550,7 @@ func (d *Device) Pull(remotePath string, dest io.Writer) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Device) installViaABBExec(apk io.ReadSeeker) (raw []byte, err error) {
|
||||
func (d *Device) installViaABBExec(apk io.ReadSeeker, args ...string) (raw []byte, err error) {
|
||||
var (
|
||||
tp transport
|
||||
filesize int64
|
||||
@@ -515,8 +563,11 @@ func (d *Device) installViaABBExec(apk io.ReadSeeker) (raw []byte, err error) {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = tp.Close() }()
|
||||
|
||||
cmd := fmt.Sprintf("abb_exec:package\x00install\x00-t\x00-S\x00%d", filesize)
|
||||
cmd := "abb_exec:package\x00install\x00-t"
|
||||
for _, arg := range args {
|
||||
cmd += "\x00" + arg
|
||||
}
|
||||
cmd += fmt.Sprintf("\x00-S\x00%d", filesize)
|
||||
if err = tp.SendWithCheck(cmd); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -533,7 +584,7 @@ func (d *Device) installViaABBExec(apk io.ReadSeeker) (raw []byte, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Device) InstallAPK(apk io.ReadSeeker) (string, error) {
|
||||
func (d *Device) InstallAPK(apk io.ReadSeeker, args ...string) (string, error) {
|
||||
haserr := func(ret string) bool {
|
||||
return strings.Contains(ret, "Failure")
|
||||
}
|
||||
@@ -553,8 +604,9 @@ func (d *Device) InstallAPK(apk io.ReadSeeker) (string, error) {
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("error pushing: %v", err)
|
||||
}
|
||||
|
||||
res, err := d.RunShellCommand("pm", "install", "-f", remote)
|
||||
args = append([]string{"install"}, args...)
|
||||
args = append(args, "-f", remote)
|
||||
res, err := d.RunShellCommand("pm", args...)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "install apk failed")
|
||||
}
|
||||
@@ -569,7 +621,7 @@ func (d *Device) Uninstall(packageName string, keepData ...bool) (string, error)
|
||||
if len(keepData) == 0 {
|
||||
keepData = []bool{false}
|
||||
}
|
||||
packageName = strings.ReplaceAll(packageName, " ", "")
|
||||
packageName = strings.TrimSpace(packageName)
|
||||
if len(packageName) == 0 {
|
||||
return "", fmt.Errorf("invalid package name")
|
||||
}
|
||||
@@ -581,6 +633,33 @@ func (d *Device) Uninstall(packageName string, keepData ...bool) (string, error)
|
||||
return d.RunShellCommand("pm", args...)
|
||||
}
|
||||
|
||||
func (d *Device) ListPackages() ([]string, error) {
|
||||
args := []string{"list", "packages"}
|
||||
resRaw, err := d.RunShellCommand("pm", args...)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
lines := strings.Split(resRaw, "\n")
|
||||
var packages []string
|
||||
for _, line := range lines {
|
||||
packageName := strings.TrimPrefix(line, "package:")
|
||||
packages = append(packages, packageName)
|
||||
}
|
||||
return packages, nil
|
||||
}
|
||||
|
||||
func (d *Device) IsPackagesInstalled(packageName string) bool {
|
||||
packages, err := d.ListPackages()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
packageName = strings.TrimSpace(packageName)
|
||||
if len(packageName) == 0 {
|
||||
return false
|
||||
}
|
||||
return builtin.Contains(packages, packageName)
|
||||
}
|
||||
|
||||
func (d *Device) ScreenCap() ([]byte, error) {
|
||||
if d.HasFeature(FeatShellV2) {
|
||||
return d.RunShellCommandV2WithBytes("screencap", "-p")
|
||||
|
||||
@@ -59,7 +59,10 @@ func TestDevice_Product(t *testing.T) {
|
||||
|
||||
for i := range devices {
|
||||
dev := devices[i]
|
||||
product := dev.Product()
|
||||
product, err := dev.Product()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(dev.Serial(), product)
|
||||
}
|
||||
}
|
||||
@@ -69,7 +72,24 @@ func TestDevice_Model(t *testing.T) {
|
||||
|
||||
for i := range devices {
|
||||
dev := devices[i]
|
||||
t.Log(dev.Serial(), dev.Model())
|
||||
model, err := dev.Model()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(dev.Serial(), model)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevice_Brand(t *testing.T) {
|
||||
setupDevices(t)
|
||||
|
||||
for i := range devices {
|
||||
dev := devices[i]
|
||||
brand, err := dev.Brand()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(dev.Serial(), brand)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +98,15 @@ func TestDevice_Usb(t *testing.T) {
|
||||
|
||||
for i := range devices {
|
||||
dev := devices[i]
|
||||
t.Log(dev.Serial(), dev.Usb(), dev.IsUsb())
|
||||
usb, err := dev.Usb()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
isUsb, err := dev.IsUsb()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(dev.Serial(), usb, isUsb)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,8 +123,7 @@ func TestDevice_Forward(t *testing.T) {
|
||||
setupDevices(t)
|
||||
|
||||
for _, device := range devices {
|
||||
localPort := 61000
|
||||
err := device.Forward(localPort, 6790)
|
||||
localPort, err := device.Forward(6790)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -314,6 +341,22 @@ func TestDevice_InstallAPK(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevice_ListPackages(t *testing.T) {
|
||||
setupDevices(t)
|
||||
for _, dev := range devices {
|
||||
res, err := dev.ListPackages()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(res)
|
||||
installed := dev.IsPackagesInstalled("io.appium.uiautomator2.server")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(installed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDevice_HasFeature(t *testing.T) {
|
||||
setupDevices(t)
|
||||
|
||||
|
||||
@@ -59,6 +59,7 @@ const (
|
||||
ACTION_SwipeToTapTexts ActionMethod = "swipe_to_tap_texts" // swipe up & down to find text and tap
|
||||
ACTION_VideoCrawler ActionMethod = "video_crawler"
|
||||
ACTION_ClosePopups ActionMethod = "close_popups"
|
||||
ACTION_EndToEndDelay ActionMethod = "live_e2e"
|
||||
)
|
||||
|
||||
type MobileAction struct {
|
||||
@@ -116,11 +117,13 @@ type ActionOptions struct {
|
||||
Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"`
|
||||
|
||||
// screenshot related
|
||||
ScreenShotWithOCR bool `json:"screenshot_with_ocr,omitempty" yaml:"screenshot_with_ocr,omitempty"`
|
||||
ScreenShotWithUpload bool `json:"screenshot_with_upload,omitempty" yaml:"screenshot_with_upload,omitempty"`
|
||||
ScreenShotWithLiveType bool `json:"screenshot_with_live_type,omitempty" yaml:"screenshot_with_live_type,omitempty"`
|
||||
ScreenShotWithUITypes []string `json:"screenshot_with_ui_types,omitempty" yaml:"screenshot_with_ui_types,omitempty"`
|
||||
ScreenShotWithClosePopups bool `json:"screenshot_with_close_popups,omitempty" yaml:"screenshot_with_close_popups,omitempty"`
|
||||
ScreenShotWithOCR bool `json:"screenshot_with_ocr,omitempty" yaml:"screenshot_with_ocr,omitempty"`
|
||||
ScreenShotWithUpload bool `json:"screenshot_with_upload,omitempty" yaml:"screenshot_with_upload,omitempty"`
|
||||
ScreenShotWithLiveType bool `json:"screenshot_with_live_type,omitempty" yaml:"screenshot_with_live_type,omitempty"`
|
||||
ScreenShotWithLivePopularity bool `json:"screenshot_with_live_popularity,omitempty" yaml:"screenshot_with_live_popularity,omitempty"`
|
||||
ScreenShotWithUITypes []string `json:"screenshot_with_ui_types,omitempty" yaml:"screenshot_with_ui_types,omitempty"`
|
||||
ScreenShotWithClosePopups bool `json:"screenshot_with_close_popups,omitempty" yaml:"screenshot_with_close_popups,omitempty"`
|
||||
ScreenShotWithOCRCluster string `json:"screenshot_with_ocr_cluster,omitempty" yaml:"screenshot_with_ocr_cluster,omitempty"`
|
||||
}
|
||||
|
||||
func (o *ActionOptions) Options() []ActionOption {
|
||||
@@ -180,6 +183,9 @@ func (o *ActionOptions) Options() []ActionOption {
|
||||
if len(o.AbsScope) == 4 {
|
||||
options = append(options, WithAbsScope(
|
||||
o.AbsScope[0], o.AbsScope[1], o.AbsScope[2], o.AbsScope[3]))
|
||||
} else if len(o.Scope) == 4 {
|
||||
options = append(options, WithScope(
|
||||
o.Scope[0], o.Scope[1], o.Scope[2], o.Scope[3]))
|
||||
}
|
||||
if len(o.Offset) == 2 {
|
||||
// for tap [x,y] offset
|
||||
@@ -221,9 +227,18 @@ func (o *ActionOptions) Options() []ActionOption {
|
||||
if o.ScreenShotWithLiveType {
|
||||
options = append(options, WithScreenShotLiveType(true))
|
||||
}
|
||||
if o.ScreenShotWithLivePopularity {
|
||||
options = append(options, WithScreenShotLivePopularity(true))
|
||||
}
|
||||
if len(o.ScreenShotWithUITypes) > 0 {
|
||||
options = append(options, WithScreenShotUITypes(o.ScreenShotWithUITypes...))
|
||||
}
|
||||
if o.ScreenShotWithClosePopups {
|
||||
options = append(options, WithScreenShotClosePopups(true))
|
||||
}
|
||||
if o.ScreenShotWithOCRCluster != "" {
|
||||
options = append(options, WithScreenOCRCluster(o.ScreenShotWithOCRCluster))
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
@@ -239,6 +254,9 @@ func (o *ActionOptions) screenshotActions() []string {
|
||||
if o.ScreenShotWithLiveType {
|
||||
actions = append(actions, "liveType")
|
||||
}
|
||||
if o.ScreenShotWithLivePopularity {
|
||||
actions = append(actions, "livePopularity")
|
||||
}
|
||||
// UI detection
|
||||
if len(o.ScreenShotWithUITypes) > 0 {
|
||||
actions = append(actions, "ui")
|
||||
@@ -286,11 +304,11 @@ func (o *ActionOptions) updateData(data map[string]interface{}) {
|
||||
data["frequency"] = o.Frequency
|
||||
}
|
||||
if _, ok := data["frequency"]; !ok {
|
||||
data["frequency"] = 60 // default frequency
|
||||
data["frequency"] = 10 // default frequency
|
||||
}
|
||||
|
||||
if _, ok := data["isReplace"]; !ok {
|
||||
data["isReplace"] = true // default true
|
||||
if _, ok := data["replace"]; !ok {
|
||||
data["replace"] = true // default true
|
||||
}
|
||||
|
||||
// custom options
|
||||
@@ -309,6 +327,11 @@ func NewActionOptions(options ...ActionOption) *ActionOptions {
|
||||
return actionOptions
|
||||
}
|
||||
|
||||
type TapTextAction struct {
|
||||
Text string
|
||||
Options []ActionOption
|
||||
}
|
||||
|
||||
type ActionOption func(o *ActionOptions)
|
||||
|
||||
func WithCustomOption(key string, value interface{}) ActionOption {
|
||||
@@ -460,6 +483,12 @@ func WithScreenShotLiveType(liveTypeOn bool) ActionOption {
|
||||
}
|
||||
}
|
||||
|
||||
func WithScreenShotLivePopularity(livePopularityOn bool) ActionOption {
|
||||
return func(o *ActionOptions) {
|
||||
o.ScreenShotWithLivePopularity = livePopularityOn
|
||||
}
|
||||
}
|
||||
|
||||
func WithScreenShotUITypes(uiTypes ...string) ActionOption {
|
||||
return func(o *ActionOptions) {
|
||||
o.ScreenShotWithUITypes = uiTypes
|
||||
@@ -472,6 +501,12 @@ func WithScreenShotClosePopups(closeOn bool) ActionOption {
|
||||
}
|
||||
}
|
||||
|
||||
func WithScreenOCRCluster(ocrCluster string) ActionOption {
|
||||
return func(o *ActionOptions) {
|
||||
o.ScreenShotWithOCRCluster = ocrCluster
|
||||
}
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) ParseActionOptions(options ...ActionOption) []ActionOption {
|
||||
actionOptions := NewActionOptions(options...)
|
||||
|
||||
@@ -669,6 +704,9 @@ func (dExt *DriverExt) DoAction(action MobileAction) (err error) {
|
||||
return dExt.VideoCrawler(configs)
|
||||
case ACTION_ClosePopups:
|
||||
return dExt.ClosePopupsHandler()
|
||||
case ACTION_EndToEndDelay:
|
||||
dExt.CollectEndToEndDelay(action.GetOptions()...)
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,17 +1,31 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/funplugin/myexec"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/gadb"
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/utf7"
|
||||
)
|
||||
|
||||
const (
|
||||
AdbKeyBoardPackageName = "com.android.adbkeyboard/.AdbIME"
|
||||
UnicodeImePackageName = "io.appium.settings/.UnicodeIME"
|
||||
)
|
||||
|
||||
type adbDriver struct {
|
||||
@@ -84,7 +98,14 @@ func (ad *adbDriver) WindowSize() (size Size, err error) {
|
||||
return Size{Width: width, Height: height}, nil
|
||||
}
|
||||
}
|
||||
|
||||
orientation, err := ad.Orientation()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("window size get orientation failed, use default orientation")
|
||||
orientation = OrientationPortrait
|
||||
}
|
||||
if orientation != OrientationPortrait {
|
||||
size.Width, size.Height = size.Height, size.Width
|
||||
}
|
||||
err = errors.New("physical window size not found by adb")
|
||||
return
|
||||
}
|
||||
@@ -178,6 +199,24 @@ func (ad *adbDriver) StopCamera() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *adbDriver) Orientation() (orientation Orientation, err error) {
|
||||
output, err := ad.adbClient.RunShellCommand("dumpsys", "input", "|", "grep", "'SurfaceOrientation'")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
re := regexp.MustCompile(`SurfaceOrientation: (\d)`)
|
||||
matches := re.FindStringSubmatch(output)
|
||||
if len(matches) > 1 { // 确保找到了匹配项
|
||||
if matches[1] == "0" || matches[1] == "2" {
|
||||
return OrientationPortrait, nil
|
||||
} else if matches[1] == "1" || matches[1] == "3" {
|
||||
return OrientationLandscapeLeft, nil
|
||||
}
|
||||
}
|
||||
err = fmt.Errorf("not found SurfaceOrientation value")
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *adbDriver) Homescreen() (err error) {
|
||||
return ad.PressKeyCode(KCHome, KMEmpty)
|
||||
}
|
||||
@@ -319,6 +358,15 @@ func (ad *adbDriver) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffe
|
||||
}
|
||||
|
||||
func (ad *adbDriver) SendKeys(text string, options ...ActionOption) (err error) {
|
||||
err = ad.SendUnicodeKeys(text, options...)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
err = ad.InputText(text, options...)
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *adbDriver) InputText(text string, options ...ActionOption) (err error) {
|
||||
// adb shell input text <text>
|
||||
_, err = ad.adbClient.RunShellCommand("input", "text", text)
|
||||
if err != nil {
|
||||
@@ -327,6 +375,85 @@ func (ad *adbDriver) SendKeys(text string, options ...ActionOption) (err error)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ad *adbDriver) SendUnicodeKeys(text string, options ...ActionOption) (err error) {
|
||||
// If the Unicode IME is not installed, fall back to the old interface.
|
||||
// There might be differences in the tracking schemes across different phones, and it is pending further verification.
|
||||
// In release version: without the Unicode IME installed, the test cannot execute.
|
||||
if !ad.IsUnicodeIMEInstalled() {
|
||||
return fmt.Errorf("appium unicode ime not installed")
|
||||
}
|
||||
currentIme, err := ad.GetIme()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if currentIme != UnicodeImePackageName {
|
||||
defer func() {
|
||||
_ = ad.SetIme(currentIme)
|
||||
}()
|
||||
err = ad.SetIme(UnicodeImePackageName)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("set Unicode Ime failed")
|
||||
return
|
||||
}
|
||||
}
|
||||
encodedStr, err := utf7.Encoding.NewEncoder().String(text)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("encode text with modified utf7 failed")
|
||||
return
|
||||
}
|
||||
err = ad.InputText("\""+strings.ReplaceAll(encodedStr, "\"", "\\\"")+"\"", options...)
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *adbDriver) IsAdbKeyBoardInstalled() bool {
|
||||
output, err := ad.adbClient.RunShellCommand("ime", "list", "-a")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(output, AdbKeyBoardPackageName)
|
||||
}
|
||||
|
||||
func (ad *adbDriver) IsUnicodeIMEInstalled() bool {
|
||||
output, err := ad.adbClient.RunShellCommand("ime", "list", "-s")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(output, UnicodeImePackageName)
|
||||
}
|
||||
|
||||
func (ad *adbDriver) SendKeysByAdbKeyBoard(text string) (err error) {
|
||||
defer func() {
|
||||
// Reset to default, don't care which keyboard was chosen before switch:
|
||||
if _, resetErr := ad.adbClient.RunShellCommand("ime", "reset"); resetErr != nil {
|
||||
log.Error().Err(err).Msg("failed to reset ime")
|
||||
}
|
||||
}()
|
||||
|
||||
// Enable ADBKeyBoard from adb
|
||||
if _, err = ad.adbClient.RunShellCommand("ime", "enable", AdbKeyBoardPackageName); err != nil {
|
||||
log.Error().Err(err).Msg("failed to enable adbKeyBoard")
|
||||
return
|
||||
}
|
||||
// Switch to ADBKeyBoard from adb
|
||||
if _, err = ad.adbClient.RunShellCommand("ime", "set", AdbKeyBoardPackageName); err != nil {
|
||||
log.Error().Err(err).Msg("failed to set adbKeyBoard")
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
// input Quoted text
|
||||
text = strings.ReplaceAll(text, " ", "\\ ")
|
||||
if _, err = ad.adbClient.RunShellCommand("am", "broadcast", "-a", "ADB_INPUT_TEXT", "--es", "msg", text); err != nil {
|
||||
log.Error().Err(err).Msg("failed to input by adbKeyBoard")
|
||||
return
|
||||
}
|
||||
if _, err = ad.adbClient.RunShellCommand("input", "keyevent", fmt.Sprintf("%d", KCEnter)); err != nil {
|
||||
log.Error().Err(err).Msg("failed to input keyevent enter")
|
||||
return
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *adbDriver) Input(text string, options ...ActionOption) (err error) {
|
||||
return ad.SendKeys(text, options...)
|
||||
}
|
||||
@@ -356,10 +483,103 @@ func (ad *adbDriver) Screenshot() (raw *bytes.Buffer, err error) {
|
||||
}
|
||||
|
||||
func (ad *adbDriver) Source(srcOpt ...SourceOption) (source string, err error) {
|
||||
err = errDriverNotImplemented
|
||||
_, err = ad.adbClient.RunShellCommand("rm", "-rf", "/sdcard/window_dump.xml")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// 高版本报错 ERROR: null root node returned by UiTestAutomationBridge.
|
||||
_, err = ad.adbClient.RunShellCommand("uiautomator", "dump")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
source, err = ad.adbClient.RunShellCommand("cat", "/sdcard/window_dump.xml")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *adbDriver) sourceTree(srcOpt ...SourceOption) (sourceTree *Hierarchy, err error) {
|
||||
source, err := ad.Source()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
sourceTree = new(Hierarchy)
|
||||
err = xml.Unmarshal([]byte(source), sourceTree)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ad *adbDriver) TapByText(text string, options ...ActionOption) error {
|
||||
sourceTree, err := ad.sourceTree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ad.tapByTextUsingHierarchy(sourceTree, text, options...)
|
||||
}
|
||||
|
||||
func (ad *adbDriver) tapByTextUsingHierarchy(hierarchy *Hierarchy, text string, options ...ActionOption) error {
|
||||
bounds := ad.searchNodes(hierarchy.Layout, text, options...)
|
||||
actionOptions := NewActionOptions(options...)
|
||||
if len(bounds) == 0 {
|
||||
if actionOptions.IgnoreNotFoundError {
|
||||
log.Info().Msg("not found element by text " + text)
|
||||
return nil
|
||||
}
|
||||
return errors.New("not found element by text " + text)
|
||||
}
|
||||
for _, bound := range bounds {
|
||||
width, height := bound.Center()
|
||||
err := ad.TapFloat(width, height, options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ad *adbDriver) TapByTexts(actions ...TapTextAction) error {
|
||||
sourceTree, err := ad.sourceTree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, action := range actions {
|
||||
err := ad.tapByTextUsingHierarchy(sourceTree, action.Text, action.Options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ad *adbDriver) searchNodes(nodes []Layout, text string, options ...ActionOption) []Bounds {
|
||||
actionOptions := NewActionOptions(options...)
|
||||
var results []Bounds
|
||||
for _, node := range nodes {
|
||||
result := ad.searchNodes(node.Layout, text, options...)
|
||||
results = append(results, result...)
|
||||
if actionOptions.Regex {
|
||||
// regex on, check if match regex
|
||||
if !regexp.MustCompile(text).MatchString(node.Text) {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
// regex off, check if match exactly
|
||||
if node.Text != text {
|
||||
ad.searchNodes(node.Layout, text, options...)
|
||||
continue
|
||||
}
|
||||
}
|
||||
if node.Bounds != nil {
|
||||
results = append(results, *node.Bounds)
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func (ad *adbDriver) AccessibleSource() (source string, err error) {
|
||||
err = errDriverNotImplemented
|
||||
return
|
||||
@@ -387,14 +607,8 @@ func (ad *adbDriver) IsHealthy() (healthy bool, err error) {
|
||||
|
||||
func (ad *adbDriver) StartCaptureLog(identifier ...string) (err error) {
|
||||
log.Info().Msg("start adb log recording")
|
||||
|
||||
// clear logcat
|
||||
if _, err = ad.adbClient.RunShellCommand("logcat", "-c"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// start logcat
|
||||
err = ad.logcat.CatchLogcat()
|
||||
err = ad.logcat.CatchLogcat("iesqaMonitor:V")
|
||||
if err != nil {
|
||||
err = errors.Wrap(code.AndroidCaptureLogError,
|
||||
fmt.Sprintf("start adb log recording failed: %v", err))
|
||||
@@ -404,17 +618,66 @@ func (ad *adbDriver) StartCaptureLog(identifier ...string) (err error) {
|
||||
}
|
||||
|
||||
func (ad *adbDriver) StopCaptureLog() (result interface{}, err error) {
|
||||
log.Info().Msg("stop adb log recording")
|
||||
err = ad.logcat.Stop()
|
||||
defer func() {
|
||||
log.Info().Msg("stop adb log recording")
|
||||
err = ad.logcat.Stop()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to get adb log recording")
|
||||
}
|
||||
}()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to get adb log recording")
|
||||
err = errors.Wrap(code.AndroidCaptureLogError,
|
||||
fmt.Sprintf("get adb log recording failed: %v", err))
|
||||
return "", err
|
||||
log.Error().Err(err).Msg("failed to close adb log writer")
|
||||
}
|
||||
content := ad.logcat.logBuffer.String()
|
||||
log.Info().Str("logcat content", content).Msg("display logcat content")
|
||||
return ConvertPoints(content), nil
|
||||
pointRes := ConvertPoints(ad.logcat.logs)
|
||||
|
||||
// 没有解析到打点日志,走兜底逻辑
|
||||
if len(pointRes) == 0 {
|
||||
log.Info().Msg("action log is null, use action file >>>")
|
||||
logFilePathPrefix := fmt.Sprintf("%v/data", env.ActionLogFilePath)
|
||||
files := []string{}
|
||||
myexec.RunCommand("adb", "-s", ad.adbClient.Serial(), "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
|
||||
}
|
||||
|
||||
reader, err := os.Open(files[0])
|
||||
if err != nil {
|
||||
log.Info().Msg("open File error")
|
||||
return pointRes, nil
|
||||
}
|
||||
defer func() {
|
||||
_ = reader.Close()
|
||||
}()
|
||||
|
||||
var lines []string // 创建一个空的字符串数组来存储文件的每一行
|
||||
|
||||
// 使用 bufio.NewScanner 读取文件
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text()) // 将每行文本添加到字符串数组
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return pointRes, nil
|
||||
}
|
||||
|
||||
pointRes = ConvertPoints(lines)
|
||||
}
|
||||
return pointRes, nil
|
||||
}
|
||||
|
||||
func (ad *adbDriver) GetForegroundApp() (app AppInfo, err error) {
|
||||
@@ -452,6 +715,30 @@ func (ad *adbDriver) GetForegroundApp() (app AppInfo, err error) {
|
||||
return AppInfo{}, errors.Wrap(code.MobileUIAssertForegroundAppError, "get foreground app failed")
|
||||
}
|
||||
|
||||
func (ad *adbDriver) SetIme(ime string) error {
|
||||
_, err := ad.adbClient.RunShellCommand("ime", "set", ime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// even if the shell command has returned,
|
||||
// as there might be a situation where the input method has not been completely switched yet
|
||||
// Listen to the following message.
|
||||
// InputMethodManagerService: onServiceConnected, name:ComponentInfo{io.appium.settings/io.appium.settings.UnicodeIME}, token:android.os.Binder@44f825
|
||||
// But there is no such log on Vivo.
|
||||
time.Sleep(3 * time.Second)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ad *adbDriver) GetIme() (ime string, err error) {
|
||||
currentIme, err := ad.adbClient.RunShellCommand("settings", "get", "secure", "default_input_method")
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("get default ime failed")
|
||||
return
|
||||
}
|
||||
currentIme = strings.TrimSpace(currentIme)
|
||||
return currentIme, nil
|
||||
}
|
||||
|
||||
func (ad *adbDriver) AssertForegroundApp(packageName string, activityType ...string) error {
|
||||
log.Debug().Str("package_name", packageName).
|
||||
Strs("activity_type", activityType).
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
@@ -22,7 +22,6 @@ var (
|
||||
AdbServerPort = gadb.AdbServerPort // 5037
|
||||
UIA2ServerHost = "localhost"
|
||||
UIA2ServerPort = 6790
|
||||
DeviceTempPath = "/data/local/tmp"
|
||||
)
|
||||
|
||||
const forwardToPrefix = "forward-to-"
|
||||
@@ -88,10 +87,13 @@ func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, er
|
||||
for _, option := range options {
|
||||
option(device)
|
||||
}
|
||||
|
||||
deviceList, err := GetAndroidDevices(device.SerialNumber)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(code.AndroidDeviceConnectionError, err.Error())
|
||||
}
|
||||
|
||||
if device.SerialNumber == "" && len(deviceList) > 1 {
|
||||
return nil, errors.Wrap(code.AndroidDeviceConnectionError, "more than one device connected, please specify the serial")
|
||||
}
|
||||
|
||||
dev := deviceList[0]
|
||||
@@ -199,12 +201,8 @@ func (dev *AndroidDevice) NewDriver(options ...DriverOption) (driverExt *DriverE
|
||||
|
||||
// NewUSBDriver creates new client via USB connected device, this will also start a new session.
|
||||
func (dev *AndroidDevice) NewUSBDriver(capabilities Capabilities) (driver WebDriver, err error) {
|
||||
var localPort int
|
||||
if localPort, err = getFreePort(); err != nil {
|
||||
return nil, errors.Wrap(code.AndroidDeviceConnectionError,
|
||||
fmt.Sprintf("get free port failed: %v", err))
|
||||
}
|
||||
if err = dev.d.Forward(localPort, UIA2ServerPort); err != nil {
|
||||
localPort, err := dev.d.Forward(UIA2ServerPort)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(code.AndroidDeviceConnectionError,
|
||||
fmt.Sprintf("forward port %d->%d failed: %v",
|
||||
localPort, UIA2ServerPort, err))
|
||||
@@ -263,41 +261,43 @@ func (dev *AndroidDevice) StopPcap() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func getFreePort() (int, error) {
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "resolve tcp addr failed")
|
||||
}
|
||||
|
||||
l, err := net.ListenTCP("tcp", addr)
|
||||
if err != nil {
|
||||
return 0, errors.Wrap(err, "listen tcp addr failed")
|
||||
}
|
||||
defer func() { _ = l.Close() }()
|
||||
return l.Addr().(*net.TCPAddr).Port, nil
|
||||
}
|
||||
type LineCallback func(string)
|
||||
|
||||
type AdbLogcat struct {
|
||||
serial string
|
||||
logBuffer *bytes.Buffer
|
||||
errs []error
|
||||
stopping chan struct{}
|
||||
done chan struct{}
|
||||
cmd *exec.Cmd
|
||||
serial string
|
||||
// logBuffer *bytes.Buffer
|
||||
errs []error
|
||||
stopping chan struct{}
|
||||
done chan struct{}
|
||||
cmd *exec.Cmd
|
||||
callback LineCallback
|
||||
logs []string
|
||||
}
|
||||
|
||||
func NewAdbLogcatWithCallback(serial string, callback LineCallback) *AdbLogcat {
|
||||
return &AdbLogcat{
|
||||
serial: serial,
|
||||
// logBuffer: new(bytes.Buffer),
|
||||
stopping: make(chan struct{}),
|
||||
done: make(chan struct{}),
|
||||
callback: callback,
|
||||
logs: make([]string, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func NewAdbLogcat(serial string) *AdbLogcat {
|
||||
return &AdbLogcat{
|
||||
serial: serial,
|
||||
logBuffer: new(bytes.Buffer),
|
||||
stopping: make(chan struct{}),
|
||||
done: make(chan struct{}),
|
||||
serial: serial,
|
||||
// logBuffer: new(bytes.Buffer),
|
||||
stopping: make(chan struct{}),
|
||||
done: make(chan struct{}),
|
||||
logs: make([]string, 0),
|
||||
}
|
||||
}
|
||||
|
||||
// CatchLogcatContext starts logcat with timeout context
|
||||
func (l *AdbLogcat) CatchLogcatContext(timeoutCtx context.Context) (err error) {
|
||||
if err = l.CatchLogcat(); err != nil {
|
||||
if err = l.CatchLogcat(""); err != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
@@ -332,7 +332,7 @@ func (l *AdbLogcat) Errors() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (l *AdbLogcat) CatchLogcat() (err error) {
|
||||
func (l *AdbLogcat) CatchLogcat(filter string) (err error) {
|
||||
if l.cmd != nil {
|
||||
log.Warn().Msg("logcat already start")
|
||||
return nil
|
||||
@@ -342,33 +342,43 @@ func (l *AdbLogcat) CatchLogcat() (err error) {
|
||||
if err = myexec.RunCommand("adb", "-s", l.serial, "shell", "logcat", "-c"); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
args := []string{"-s", l.serial, "logcat", "--format", "time"}
|
||||
if filter != "" {
|
||||
args = append(args, "-s", filter)
|
||||
}
|
||||
// start logcat
|
||||
l.cmd = myexec.Command("adb", "-s", l.serial,
|
||||
"logcat", "--format", "time", "-s", "iesqaMonitor:V")
|
||||
l.cmd.Stderr = l.logBuffer
|
||||
l.cmd.Stdout = l.logBuffer
|
||||
l.cmd = myexec.Command("adb", args...)
|
||||
// l.cmd.Stderr = l.logBuffer
|
||||
// l.cmd.Stdout = l.logBuffer
|
||||
reader, err := l.cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = l.cmd.Start(); err != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if l.callback != nil {
|
||||
l.callback(line) // Process each line with callback
|
||||
} else {
|
||||
l.logs = append(l.logs, line) // Store line if no callback
|
||||
}
|
||||
}
|
||||
}()
|
||||
go func() {
|
||||
<-l.stopping
|
||||
if e := reader.Close(); e != nil {
|
||||
log.Error().Err(e).Msg("close logcat reader failed")
|
||||
}
|
||||
if e := myexec.KillProcessesByGpid(l.cmd); e != nil {
|
||||
log.Error().Err(e).Msg("kill logcat process failed")
|
||||
}
|
||||
l.done <- struct{}{}
|
||||
}()
|
||||
return
|
||||
}
|
||||
|
||||
func (l *AdbLogcat) BufferedLogcat() (err error) {
|
||||
// -d: dump the current buffered logcat result and exits
|
||||
cmd := myexec.Command("adb", "-s", l.serial, "logcat", "-d")
|
||||
cmd.Stdout = l.logBuffer
|
||||
cmd.Stderr = l.logBuffer
|
||||
if err = cmd.Run(); err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -382,8 +392,9 @@ type ExportPoint struct {
|
||||
RunTime int `json:"run_time,omitempty" yaml:"run_time,omitempty"`
|
||||
}
|
||||
|
||||
func ConvertPoints(data string) (eps []ExportPoint) {
|
||||
lines := strings.Split(data, "\n")
|
||||
func ConvertPoints(lines []string) (eps []ExportPoint) {
|
||||
log.Info().Msg("ConvertPoints")
|
||||
log.Info().Msg(strings.Join(lines, "\n"))
|
||||
for _, line := range lines {
|
||||
if strings.Contains(line, "ext") {
|
||||
idx := strings.Index(line, "{")
|
||||
@@ -397,6 +408,7 @@ func ConvertPoints(data string) (eps []ExportPoint) {
|
||||
log.Error().Msg("failed to parse point data")
|
||||
continue
|
||||
}
|
||||
log.Info().Msg(line)
|
||||
eps = append(eps, p)
|
||||
}
|
||||
}
|
||||
@@ -563,7 +575,7 @@ func (s UiSelectorHelper) Index(index int) UiSelectorHelper {
|
||||
//
|
||||
// For example, to simulate a user click on
|
||||
// the third image that is enabled in a UI screen, you
|
||||
// could specify a a search criteria where the instance is
|
||||
// could specify a search criteria where the instance is
|
||||
// 2, the `className(String)` matches the image
|
||||
// widget class, and `enabled(boolean)` is true.
|
||||
// The code would look like this:
|
||||
|
||||
62
hrp/pkg/uixt/android_layout.go
Normal file
62
hrp/pkg/uixt/android_layout.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Attributes struct {
|
||||
Index int `xml:"index,attr"`
|
||||
Package string `xml:"package,attr"`
|
||||
Class string `xml:"class,attr"`
|
||||
Text string `xml:"text,attr"`
|
||||
ResourceId string `xml:"resource-id,attr"`
|
||||
Checkable bool `xml:"checkable,attr"`
|
||||
Checked bool `xml:"checked,attr"`
|
||||
Clickable bool `xml:"clickable,attr"`
|
||||
Enabled bool `xml:"enabled,attr"`
|
||||
Focusable bool `xml:"focusable,attr"`
|
||||
Focused bool `xml:"focused,attr"`
|
||||
LongClickable bool `xml:"long-clickable,attr"`
|
||||
Password bool `xml:"password,attr"`
|
||||
Scrollable bool `xml:"scrollable,attr"`
|
||||
Selected bool `xml:"selected,attr"`
|
||||
Bounds *Bounds `xml:"bounds,attr"`
|
||||
Displayed bool `xml:"displayed,attr"`
|
||||
}
|
||||
|
||||
type Hierarchy struct {
|
||||
XMLName xml.Name `xml:"hierarchy"`
|
||||
Attributes
|
||||
Layout []Layout `xml:",any"`
|
||||
}
|
||||
|
||||
type Layout struct {
|
||||
Attributes
|
||||
Layout []Layout `xml:",any"`
|
||||
}
|
||||
|
||||
type Bounds struct {
|
||||
X1, Y1, X2, Y2 int
|
||||
}
|
||||
|
||||
func (b *Bounds) Center() (float64, float64) {
|
||||
return float64(b.X1+b.X2) / 2, float64(b.Y1+b.Y2) / 2
|
||||
}
|
||||
|
||||
func (b *Bounds) UnmarshalXMLAttr(attr xml.Attr) error {
|
||||
// 正则表达式用于解析格式为"[x1,y1][x2,y2]"
|
||||
re := regexp.MustCompile(`\[(\d+),(\d+)]\[(\d+),(\d+)]`)
|
||||
matches := re.FindStringSubmatch(attr.Value)
|
||||
if matches == nil {
|
||||
return fmt.Errorf("bounds format is incorrect")
|
||||
}
|
||||
// 转换字符串为整数
|
||||
b.X1, _ = strconv.Atoi(matches[1])
|
||||
b.Y1, _ = strconv.Atoi(matches[2])
|
||||
b.X2, _ = strconv.Atoi(matches[3])
|
||||
b.Y2, _ = strconv.Atoi(matches[4])
|
||||
return nil
|
||||
}
|
||||
@@ -6,18 +6,23 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||
)
|
||||
|
||||
var (
|
||||
uiaServerURL = "http://localhost:6790/wd/hub"
|
||||
uiaServerURL = "http://forward-to-6790:6790/wd/hub"
|
||||
driverExt *DriverExt
|
||||
)
|
||||
|
||||
func setupAndroid(t *testing.T) {
|
||||
device, err := NewAndroidDevice()
|
||||
checkErr(t, err)
|
||||
device.UIA2 = false
|
||||
device.LogOn = true
|
||||
driverExt, err = device.NewDriver()
|
||||
checkErr(t, err)
|
||||
}
|
||||
@@ -132,6 +137,18 @@ func TestDriver_Source(t *testing.T) {
|
||||
t.Log(source)
|
||||
}
|
||||
|
||||
func TestDriver_TapByText(t *testing.T) {
|
||||
driver, err := NewUIADriver(nil, uiaServerURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = driver.TapByText("安装")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_BatteryInfo(t *testing.T) {
|
||||
driver, err := NewUIADriver(nil, uiaServerURL)
|
||||
if err != nil {
|
||||
@@ -180,22 +197,21 @@ func TestDriver_DeviceInfo(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDriver_Tap(t *testing.T) {
|
||||
driver, err := NewUIADriver(nil, uiaServerURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = driver.Tap(150, 340)
|
||||
setupAndroid(t)
|
||||
driverExt.Driver.StartCaptureLog("")
|
||||
err := driverExt.Driver.Tap(150, 340, WithIdentifier("test"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
|
||||
err = driver.TapFloat(60.5, 125.5)
|
||||
err = driverExt.Driver.TapFloat(60.5, 125.5, WithIdentifier("test"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
result, _ := driverExt.Driver.StopCaptureLog()
|
||||
t.Log(result)
|
||||
}
|
||||
|
||||
func TestDriver_Swipe(t *testing.T) {
|
||||
@@ -204,7 +220,7 @@ func TestDriver_Swipe(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = driver.Swipe(400, 1000, 400, 500)
|
||||
err = driver.Swipe(400, 1000, 400, 500, WithPressDuration(2000))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -215,6 +231,14 @@ func TestDriver_Swipe(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_Swipe_Relative(t *testing.T) {
|
||||
setupAndroid(t)
|
||||
err := driverExt.SwipeRelative(0.5, 0.7, 0.5, 0.5)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_Drag(t *testing.T) {
|
||||
driver, err := NewUIADriver(nil, uiaServerURL)
|
||||
if err != nil {
|
||||
@@ -235,28 +259,26 @@ func TestDriver_Drag(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestDriver_SendKeys(t *testing.T) {
|
||||
driver, err := NewUIADriver(nil, uiaServerURL)
|
||||
setupAndroid(t)
|
||||
|
||||
err := driverExt.Driver.SendKeys("Android\"输入速度测试", WithIdentifier("test"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = driver.SendKeys("abc")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(time.Second * 2)
|
||||
|
||||
err = driver.SendKeys("def")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
time.Sleep(time.Second * 2)
|
||||
//err = driver.SendKeys("def")
|
||||
//if err != nil {
|
||||
// t.Fatal(err)
|
||||
//}
|
||||
//time.Sleep(time.Second * 2)
|
||||
|
||||
err = driver.SendKeys("\\n")
|
||||
//err = driver.SendKeys("\\n")
|
||||
// err = driver.SendKeys(`\n`, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
//if err != nil {
|
||||
// t.Fatal(err)
|
||||
//}
|
||||
}
|
||||
|
||||
func TestDriver_PressBack(t *testing.T) {
|
||||
@@ -312,7 +334,7 @@ func TestUiSelectorHelper_NewUiSelectorHelper(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_getFreePort(t *testing.T) {
|
||||
freePort, err := getFreePort()
|
||||
freePort, err := builtin.GetFreePort()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -421,10 +443,44 @@ func TestDriver_AppTerminate(t *testing.T) {
|
||||
|
||||
func TestConvertPoints(t *testing.T) {
|
||||
data := "10-09 20:16:48.216 I/iesqaMonitor(17845): {\"duration\":0,\"end\":1665317808206,\"ext\":\"输入\",\"from\":{\"x\":0.0,\"y\":0.0},\"operation\":\"Gtf-SendKeys\",\"run_time\":627,\"start\":1665317807579,\"start_first\":0,\"start_last\":0,\"to\":{\"x\":0.0,\"y\":0.0}}\n10-09 20:18:22.899 I/iesqaMonitor(17845): {\"duration\":0,\"end\":1665317902898,\"ext\":\"进入直播间\",\"from\":{\"x\":717.0,\"y\":2117.5},\"operation\":\"Gtf-Tap\",\"run_time\":121,\"start\":1665317902777,\"start_first\":0,\"start_last\":0,\"to\":{\"x\":717.0,\"y\":2117.5}}\n10-09 20:18:32.063 I/iesqaMonitor(17845): {\"duration\":0,\"end\":1665317912062,\"ext\":\"第一次上划\",\"from\":{\"x\":1437.0,\"y\":2409.9},\"operation\":\"Gtf-Swipe\",\"run_time\":32,\"start\":1665317912030,\"start_first\":0,\"start_last\":0,\"to\":{\"x\":1437.0,\"y\":2409.9}}"
|
||||
eps := ConvertPoints(data)
|
||||
|
||||
eps := ConvertPoints(strings.Split(data, "\n"))
|
||||
if len(eps) != 3 {
|
||||
t.Fatal()
|
||||
}
|
||||
jsons, _ := json.Marshal(eps)
|
||||
println(fmt.Sprintf("%v", string(jsons)))
|
||||
}
|
||||
|
||||
func TestDriver_ShellInputUnicode(t *testing.T) {
|
||||
device, _ := NewAndroidDevice()
|
||||
driver, err := device.NewAdbDriver()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = driver.SendKeys("test中文输入&")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
raw, err := driver.Screenshot()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Log(os.WriteFile("s1.png", raw.Bytes(), 0o600))
|
||||
}
|
||||
|
||||
func TestTapTexts(t *testing.T) {
|
||||
setupAndroid(t)
|
||||
actions := []TapTextAction{
|
||||
{Text: "^.*无视风险安装$", Options: []ActionOption{WithTapOffset(100, 0), WithRegex(true), WithIgnoreNotFoundError(true)}},
|
||||
{Text: "已了解此应用未经检测.*", Options: []ActionOption{WithTapOffset(-450, 0), WithRegex(true), WithIgnoreNotFoundError(true)}},
|
||||
{Text: "^(.*无视风险安装|确定|继续|完成|点击继续安装|继续安装旧版本|替换|安装|授权本次安装|继续安装|重新安装)$", Options: []ActionOption{WithRegex(true), WithIgnoreNotFoundError(true)}},
|
||||
}
|
||||
err := driverExt.Driver.TapByTexts(actions...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -15,7 +16,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
"github.com/httprunner/httprunner/v4/hrp/pkg/utf7"
|
||||
)
|
||||
|
||||
var errDriverNotImplemented = errors.New("driver method not implemented")
|
||||
@@ -103,8 +104,8 @@ func (ud *uiaDriver) httpRequest(method string, rawURL string, rawBody []byte, d
|
||||
// wait for UIA2 server to resume automatically
|
||||
time.Sleep(3 * time.Second)
|
||||
oldSessionID := ud.sessionId
|
||||
if err = ud.resetDriver(); err != nil {
|
||||
log.Err(err).Msgf("failed to reset uia2 driver, retry count: %v", retryCount)
|
||||
if err2 := ud.resetDriver(); err2 != nil {
|
||||
log.Err(err2).Msgf("failed to reset uia2 driver, retry count: %v", retryCount)
|
||||
continue
|
||||
}
|
||||
log.Debug().Str("new session", ud.sessionId).Str("old session", oldSessionID).Msgf("successful to reset uia2 driver, retry count: %v", retryCount)
|
||||
@@ -224,6 +225,14 @@ func (ud *uiaDriver) WindowSize() (size Size, err error) {
|
||||
return Size{}, err
|
||||
}
|
||||
size = reply.Value.Size
|
||||
orientation, err := ud.Orientation()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("window size get orientation failed, use default orientation")
|
||||
orientation = OrientationPortrait
|
||||
}
|
||||
if orientation != OrientationPortrait {
|
||||
size.Width, size.Height = size.Height, size.Width
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -253,6 +262,20 @@ func (ud *uiaDriver) PressKeyCode(keyCode KeyCode, metaState KeyMeta, flags ...K
|
||||
return
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) Orientation() (orientation Orientation, err error) {
|
||||
// [[FBRoute GET:@"/orientation"] respondWithTarget:self action:@selector(handleGetOrientation:)]
|
||||
var rawResp rawResponse
|
||||
if rawResp, err = ud.httpGET("/session", ud.sessionId, "/orientation"); err != nil {
|
||||
return "", err
|
||||
}
|
||||
reply := new(struct{ Value Orientation })
|
||||
if err = json.Unmarshal(rawResp, reply); err != nil {
|
||||
return "", err
|
||||
}
|
||||
orientation = reply.Value
|
||||
return
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) Tap(x, y int, options ...ActionOption) error {
|
||||
return ud.TapFloat(float64(x), float64(y), options...)
|
||||
}
|
||||
@@ -268,15 +291,31 @@ func (ud *uiaDriver) TapFloat(x, y float64, options ...ActionOption) (err error)
|
||||
x += actionOptions.getRandomOffset()
|
||||
y += actionOptions.getRandomOffset()
|
||||
|
||||
data := map[string]interface{}{
|
||||
"x": x,
|
||||
"y": y,
|
||||
duration := 100.0
|
||||
if actionOptions.PressDuration > 0 {
|
||||
duration = actionOptions.PressDuration
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"actions": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "pointer",
|
||||
"parameters": map[string]string{"pointerType": "touch"},
|
||||
"id": "touch",
|
||||
"actions": []interface{}{
|
||||
map[string]interface{}{"type": "pointerMove", "duration": 0, "x": x, "y": y, "origin": "viewport"},
|
||||
map[string]interface{}{"type": "pointerDown", "duration": 0, "button": 0},
|
||||
map[string]interface{}{"type": "pause", "duration": duration},
|
||||
map[string]interface{}{"type": "pointerUp", "duration": 0, "button": 0},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// update data options in post data for extra uiautomator configurations
|
||||
actionOptions.updateData(data)
|
||||
|
||||
_, err = ud.httpPOST(data, "/session", ud.sessionId, "appium/tap")
|
||||
return
|
||||
_, err = ud.httpPOST(data, "/session", ud.sessionId, "actions/tap")
|
||||
return err
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) TouchAndHold(x, y int, second ...float64) (err error) {
|
||||
@@ -358,17 +397,30 @@ func (ud *uiaDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...Actio
|
||||
toX += actionOptions.getRandomOffset()
|
||||
toY += actionOptions.getRandomOffset()
|
||||
|
||||
duration := 200.0
|
||||
if actionOptions.PressDuration > 0 {
|
||||
duration = actionOptions.PressDuration
|
||||
}
|
||||
data := map[string]interface{}{
|
||||
"startX": fromX,
|
||||
"startY": fromY,
|
||||
"endX": toX,
|
||||
"endY": toY,
|
||||
"actions": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "pointer",
|
||||
"parameters": map[string]string{"pointerType": "touch"},
|
||||
"id": "touch",
|
||||
"actions": []interface{}{
|
||||
map[string]interface{}{"type": "pointerMove", "duration": 0, "x": fromX, "y": fromY, "origin": "viewport"},
|
||||
map[string]interface{}{"type": "pointerDown", "duration": 0, "button": 0},
|
||||
map[string]interface{}{"type": "pointerMove", "duration": duration, "x": toX, "y": toY, "origin": "viewport"},
|
||||
map[string]interface{}{"type": "pointerUp", "duration": 0, "button": 0},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// update data options in post data for extra uiautomator configurations
|
||||
actionOptions.updateData(data)
|
||||
|
||||
_, err := ud.httpPOST(data, "/session", ud.sessionId, "touch/perform")
|
||||
_, err := ud.httpPOST(data, "/session", ud.sessionId, "actions/swipe")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -415,17 +467,79 @@ func (ud *uiaDriver) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffe
|
||||
return
|
||||
}
|
||||
|
||||
// SendKeys Android input does not support setting frequency.
|
||||
func (ud *uiaDriver) SendKeys(text string, options ...ActionOption) (err error) {
|
||||
// register(postHandler, new SendKeysToElement("/wd/hub/session/:sessionId/keys"))
|
||||
// https://github.com/appium/appium-uiautomator2-server/blob/master/app/src/main/java/io/appium/uiautomator2/handler/SendKeysToElement.java#L76-L85
|
||||
actionOptions := NewActionOptions(options...)
|
||||
data := map[string]interface{}{
|
||||
"text": text,
|
||||
err = ud.SendUnicodeKeys(text, options...)
|
||||
if err != nil {
|
||||
data := map[string]interface{}{
|
||||
"text": text,
|
||||
}
|
||||
|
||||
// new data options in post data for extra uiautomator configurations
|
||||
actionOptions.updateData(data)
|
||||
|
||||
_, err = ud.httpPOST(data, "/session", ud.sessionId, "/keys")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) SendUnicodeKeys(text string, options ...ActionOption) (err error) {
|
||||
// If the Unicode IME is not installed, fall back to the old interface.
|
||||
// There might be differences in the tracking schemes across different phones, and it is pending further verification.
|
||||
// In release version: without the Unicode IME installed, the test cannot execute.
|
||||
if !ud.IsUnicodeIMEInstalled() {
|
||||
return fmt.Errorf("appium unicode ime not installed")
|
||||
}
|
||||
currentIme, err := ud.adbDriver.GetIme()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if currentIme != UnicodeImePackageName {
|
||||
defer func() {
|
||||
_ = ud.adbDriver.SetIme(currentIme)
|
||||
}()
|
||||
err = ud.adbDriver.SetIme(UnicodeImePackageName)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("set Unicode Ime failed")
|
||||
return
|
||||
}
|
||||
}
|
||||
encodedStr, err := utf7.Encoding.NewEncoder().String(text)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("encode text with modified utf7 failed")
|
||||
return
|
||||
}
|
||||
err = ud.SendActionKey(encodedStr, options...)
|
||||
return
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) SendActionKey(text string, options ...ActionOption) (err error) {
|
||||
actionOptions := NewActionOptions(options...)
|
||||
var actions []interface{}
|
||||
for i, c := range text {
|
||||
actions = append(actions, map[string]interface{}{"type": "keyDown", "value": string(c)},
|
||||
map[string]interface{}{"type": "keyUp", "value": string(c)})
|
||||
if i != len(text)-1 {
|
||||
actions = append(actions, map[string]interface{}{"type": "pause", "duration": 40})
|
||||
}
|
||||
}
|
||||
|
||||
data := map[string]interface{}{
|
||||
"actions": []interface{}{
|
||||
map[string]interface{}{
|
||||
"type": "key",
|
||||
"id": "key",
|
||||
"actions": actions,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// new data options in post data for extra uiautomator configurations
|
||||
actionOptions.updateData(data)
|
||||
|
||||
_, err = ud.httpPOST(data, "/session", ud.sessionId, "keys")
|
||||
_, err = ud.httpPOST(data, "/session", ud.sessionId, "/actions/keys")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -449,25 +563,9 @@ func (ud *uiaDriver) Rotation() (rotation Rotation, err error) {
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) Screenshot() (raw *bytes.Buffer, err error) {
|
||||
// register(getHandler, new CaptureScreenshot("/wd/hub/session/:sessionId/screenshot"))
|
||||
var rawResp rawResponse
|
||||
if rawResp, err = ud.httpGET("/session", ud.sessionId, "screenshot"); err != nil {
|
||||
return nil, errors.Wrap(code.AndroidScreenShotError,
|
||||
fmt.Sprintf("get UIA screenshot data failed: %v", err))
|
||||
}
|
||||
reply := new(struct{ Value string })
|
||||
if err = json.Unmarshal(rawResp, reply); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var decodeStr []byte
|
||||
if decodeStr, err = base64.StdEncoding.DecodeString(reply.Value); err != nil {
|
||||
return nil, errors.Wrap(code.AndroidScreenShotError,
|
||||
fmt.Sprintf("decode UIA screenshot data failed: %v", err))
|
||||
}
|
||||
|
||||
raw = bytes.NewBuffer(decodeStr)
|
||||
return
|
||||
// https://bytedance.larkoffice.com/docx/C8qEdmSHnoRvMaxZauocMiYpnLh
|
||||
// ui2截图受内存影响,改为adb截图
|
||||
return ud.adbDriver.Screenshot()
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) Source(srcOpt ...SourceOption) (source string, err error) {
|
||||
@@ -484,3 +582,39 @@ func (ud *uiaDriver) Source(srcOpt ...SourceOption) (source string, err error) {
|
||||
source = reply.Value
|
||||
return
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) sourceTree(srcOpt ...SourceOption) (sourceTree *Hierarchy, err error) {
|
||||
source, err := ud.Source()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
sourceTree = new(Hierarchy)
|
||||
err = xml.Unmarshal([]byte(source), sourceTree)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) TapByText(text string, options ...ActionOption) error {
|
||||
sourceTree, err := ud.sourceTree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ud.tapByTextUsingHierarchy(sourceTree, text, options...)
|
||||
}
|
||||
|
||||
func (ud *uiaDriver) TapByTexts(actions ...TapTextAction) error {
|
||||
sourceTree, err := ud.sourceTree()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, action := range actions {
|
||||
err := ud.tapByTextUsingHierarchy(sourceTree, action.Text, action.Options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,9 +4,9 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/gif"
|
||||
_ "image/gif"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
_ "image/png"
|
||||
"math/rand"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
@@ -62,9 +62,12 @@ type ScreenResult struct {
|
||||
Video *Video `json:"video,omitempty"`
|
||||
Popup *PopupInfo `json:"popup,omitempty"`
|
||||
|
||||
SwipeStartTime int64 `json:"swipe_start_time"` // 滑动开始时间戳
|
||||
SwipeFinishTime int64 `json:"swipe_finish_time"` // 滑动结束时间戳
|
||||
SwipeStartTime int64 `json:"swipe_start_time"` // 滑动开始时间戳
|
||||
SwipeFinishTime int64 `json:"swipe_finish_time"` // 滑动结束时间戳
|
||||
FetchVideoStartTime int64 `json:"fetch_video_start_time"` // 抓取视频开始时间戳
|
||||
FetchVideoFinishTime int64 `json:"fetch_video_finish_time"` // 抓取视频结束时间戳
|
||||
|
||||
FetchVideoElapsed int64 `json:"fetch_video_elapsed"` // 抓取视频耗时(ms)
|
||||
ScreenshotTakeElapsed int64 `json:"screenshot_take_elapsed"` // 设备截图耗时(ms)
|
||||
ScreenshotCVElapsed int64 `json:"screenshot_cv_elapsed"` // CV 识别耗时(ms)
|
||||
|
||||
@@ -93,12 +96,14 @@ type cacheStepData struct {
|
||||
screenResults ScreenResultMap
|
||||
// cache feed/live video stat
|
||||
videoCrawler *VideoCrawler
|
||||
e2eDelay []timeLog
|
||||
}
|
||||
|
||||
func (d *cacheStepData) reset() {
|
||||
d.screenShots = make([]string, 0)
|
||||
d.screenResults = make(map[string]*ScreenResult)
|
||||
d.videoCrawler = nil
|
||||
d.e2eDelay = nil
|
||||
}
|
||||
|
||||
type DriverExt struct {
|
||||
@@ -190,8 +195,29 @@ func (dExt *DriverExt) takeScreenShot(fileName string) (raw *bytes.Buffer, path
|
||||
}
|
||||
|
||||
func compressImageBuffer(raw *bytes.Buffer) (compressed *bytes.Buffer, err error) {
|
||||
// TODO: compress image data
|
||||
return raw, nil
|
||||
// 解码原始图像数据
|
||||
img, format, err := image.Decode(raw)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 创建一个用来保存压缩后数据的buffer
|
||||
var buf bytes.Buffer
|
||||
|
||||
switch format {
|
||||
// Convert to jpeg uniformly and compress with a compression rate of 95
|
||||
case "jpeg", "png":
|
||||
jpegOptions := &jpeg.Options{Quality: 95}
|
||||
err = jpeg.Encode(&buf, img, jpegOptions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported image format: %s", format)
|
||||
}
|
||||
|
||||
// 返回压缩后的图像数据
|
||||
return &buf, nil
|
||||
}
|
||||
|
||||
// saveScreenShot saves image file with file name
|
||||
@@ -207,7 +233,8 @@ func (dExt *DriverExt) saveScreenShot(raw *bytes.Buffer, fileName string) (strin
|
||||
return "", errors.Wrap(err, "decode screenshot image failed")
|
||||
}
|
||||
|
||||
screenshotPath := filepath.Join(fmt.Sprintf("%s.%s", fileName, format))
|
||||
// The default format uses jpeg for compression
|
||||
screenshotPath := filepath.Join(fmt.Sprintf("%s.%s", fileName, "jpeg"))
|
||||
file, err := os.Create(screenshotPath)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "create screenshot image file failed")
|
||||
@@ -217,12 +244,9 @@ func (dExt *DriverExt) saveScreenShot(raw *bytes.Buffer, fileName string) (strin
|
||||
}()
|
||||
|
||||
switch format {
|
||||
case "png":
|
||||
err = png.Encode(file, img)
|
||||
case "jpeg":
|
||||
err = jpeg.Encode(file, img, nil)
|
||||
case "gif":
|
||||
err = gif.Encode(file, img, nil)
|
||||
case "jpeg", "png":
|
||||
jpegOptions := &jpeg.Options{}
|
||||
err = jpeg.Encode(file, img, jpegOptions)
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported image format: %s", format)
|
||||
}
|
||||
@@ -242,6 +266,7 @@ func (dExt *DriverExt) GetStepCacheData() map[string]interface{} {
|
||||
|
||||
cacheData["screenshots_urls"] = dExt.cacheStepData.screenResults.getScreenShotUrls()
|
||||
cacheData["screen_results"] = dExt.cacheStepData.screenResults
|
||||
cacheData["e2e_results"] = dExt.cacheStepData.e2eDelay
|
||||
|
||||
// clear cache
|
||||
dExt.cacheStepData.reset()
|
||||
|
||||
@@ -510,6 +510,10 @@ type WebDriver interface {
|
||||
// since the location service needs some time to update the location data.
|
||||
Location() (Location, error)
|
||||
BatteryInfo() (BatteryInfo, error)
|
||||
|
||||
// WindowSize Return the width and height in portrait mode.
|
||||
// when getting the window size in wda/ui2/adb, if the device is in landscape mode,
|
||||
// the width and height will be reversed.
|
||||
WindowSize() (Size, error)
|
||||
Screen() (Screen, error)
|
||||
Scale() (float64, error)
|
||||
@@ -536,6 +540,8 @@ type WebDriver interface {
|
||||
// StopCamera Stops the camera for recording
|
||||
StopCamera() error
|
||||
|
||||
Orientation() (orientation Orientation, err error)
|
||||
|
||||
// Tap Sends a tap event at the coordinate.
|
||||
Tap(x, y int, options ...ActionOption) error
|
||||
TapFloat(x, y float64, options ...ActionOption) error
|
||||
@@ -582,6 +588,11 @@ type WebDriver interface {
|
||||
|
||||
// Source Return application elements tree
|
||||
Source(srcOpt ...SourceOption) (string, error)
|
||||
|
||||
TapByText(text string, options ...ActionOption) error
|
||||
|
||||
TapByTexts(actions ...TapTextAction) error
|
||||
|
||||
// AccessibleSource Return application elements accessibility tree
|
||||
AccessibleSource() (string, error)
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"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/pkg/gidevice"
|
||||
@@ -237,6 +238,10 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) {
|
||||
return nil, errors.Wrap(code.IOSDeviceConnectionError, err.Error())
|
||||
}
|
||||
|
||||
if device.UDID == "" && len(deviceList) > 1 {
|
||||
return nil, errors.Wrap(code.IOSDeviceConnectionError, "more than one device connected, please specify the udid")
|
||||
}
|
||||
|
||||
dev := deviceList[0]
|
||||
udid := dev.Properties().SerialNumber
|
||||
|
||||
@@ -592,7 +597,7 @@ func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver
|
||||
var localPort int
|
||||
localPort, err = strconv.Atoi(env.WDA_LOCAL_PORT)
|
||||
if err != nil {
|
||||
localPort, err = getFreePort()
|
||||
localPort, err = builtin.GetFreePort()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(code.IOSDeviceHTTPDriverError,
|
||||
fmt.Sprintf("get free port failed: %v", err))
|
||||
@@ -609,7 +614,7 @@ func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver
|
||||
var localMjpegPort int
|
||||
localMjpegPort, err = strconv.Atoi(env.WDA_LOCAL_MJPEG_PORT)
|
||||
if err != nil {
|
||||
localMjpegPort, err = getFreePort()
|
||||
localMjpegPort, err = builtin.GetFreePort()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(code.IOSDeviceHTTPDriverError,
|
||||
fmt.Sprintf("get free port failed: %v", err))
|
||||
|
||||
@@ -55,8 +55,8 @@ func (wd *wdaDriver) httpRequest(method string, rawURL string, rawBody []byte, d
|
||||
// TODO: polling WDA to check if resumed automatically
|
||||
time.Sleep(5 * time.Second)
|
||||
oldSessionID := wd.sessionId
|
||||
if err = wd.resetSession(); err != nil {
|
||||
log.Err(err).Msgf("failed to reset wda driver, retry count: %v", retryCount)
|
||||
if err2 := wd.resetSession(); err2 != nil {
|
||||
log.Err(err2).Msgf("failed to reset wda driver, retry count: %v", retryCount)
|
||||
continue
|
||||
}
|
||||
log.Debug().Str("new session", wd.sessionId).Str("old session", oldSessionID).Msgf("successful to reset wda driver, retry count: %v", retryCount)
|
||||
@@ -207,6 +207,14 @@ func (wd *wdaDriver) WindowSize() (size Size, err error) {
|
||||
}
|
||||
size.Height = size.Height * int(scale)
|
||||
size.Width = size.Width * int(scale)
|
||||
orientation, err := wd.Orientation()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("window size get orientation failed, use default orientation")
|
||||
orientation = OrientationPortrait
|
||||
}
|
||||
if orientation != OrientationPortrait {
|
||||
size.Width, size.Height = size.Height, size.Width
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -547,8 +555,8 @@ func (wd *wdaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...Action
|
||||
|
||||
// update data options in post data for extra WDA configurations
|
||||
actionOptions.updateData(data)
|
||||
|
||||
_, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/dragfromtoforduration")
|
||||
// wda 43 version
|
||||
_, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/drag")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -751,6 +759,14 @@ func (wd *wdaDriver) Source(srcOpt ...SourceOption) (source string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (wd *wdaDriver) TapByText(text string, options ...ActionOption) error {
|
||||
return errDriverNotImplemented
|
||||
}
|
||||
|
||||
func (wd *wdaDriver) TapByTexts(actions ...TapTextAction) error {
|
||||
return errDriverNotImplemented
|
||||
}
|
||||
|
||||
func (wd *wdaDriver) AccessibleSource() (source string, err error) {
|
||||
// [[FBRoute GET:@"/wda/accessibleSource"] respondWithTarget:self action:@selector(handleGetAccessibleSourceCommand:)]
|
||||
// [[FBRoute GET:@"/wda/accessibleSource"].withoutSession
|
||||
|
||||
@@ -10,17 +10,23 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
bundleId = "com.apple.Preferences"
|
||||
driver WebDriver
|
||||
bundleId = "com.apple.Preferences"
|
||||
driver WebDriver
|
||||
iOSDriverExt *DriverExt
|
||||
)
|
||||
|
||||
func setup(t *testing.T) {
|
||||
device, err := NewIOSDevice()
|
||||
device, err := NewIOSDevice(WithWDAPort(8700), WithWDAMjpegPort(8800), WithWDALogOn(true))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
driver, err = device.NewUSBDriver(nil)
|
||||
capabilities := NewCapabilities()
|
||||
capabilities.WithDefaultAlertAction(AlertActionAccept)
|
||||
driver, err = device.NewUSBDriver(capabilities)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
iOSDriverExt, err = newDriverExt(device, driver, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -267,6 +273,16 @@ func Test_remoteWD_Drag(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Relative_Drag(t *testing.T) {
|
||||
setup(t)
|
||||
|
||||
// err := driver.Drag(200, 300, 200, 500, WithDataPressDuration(0.5))
|
||||
err := iOSDriverExt.SwipeRelative(0.5, 0.7, 0.5, 0.5)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func Test_remoteWD_SetPasteboard(t *testing.T) {
|
||||
setup(t)
|
||||
|
||||
@@ -305,12 +321,14 @@ func Test_remoteWD_GetPasteboard(t *testing.T) {
|
||||
|
||||
func Test_remoteWD_SendKeys(t *testing.T) {
|
||||
setup(t)
|
||||
|
||||
err := driver.SendKeys("App Store")
|
||||
driver.StartCaptureLog("hrp_wda_log")
|
||||
err := driver.SendKeys("", WithIdentifier("test"))
|
||||
result, _ := driver.StopCaptureLog()
|
||||
// err := driver.SendKeys("App Store", WithFrequency(3))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(result)
|
||||
}
|
||||
|
||||
func Test_remoteWD_PressButton(t *testing.T) {
|
||||
@@ -374,7 +392,7 @@ func Test_remoteWD_Source(t *testing.T) {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
|
||||
source, err = driver.Source(NewSourceOption().WithScope("AppiumAUT"))
|
||||
source, err = driver.Source()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
140
hrp/pkg/uixt/live_e2e.go
Normal file
140
hrp/pkg/uixt/live_e2e.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type timeLog struct {
|
||||
UTCTimeStr string `json:"utc_time_str"`
|
||||
UTCTime int64 `json:"utc_time"`
|
||||
LiveTimeStr string `json:"live_time_str"`
|
||||
LiveTime int64 `json:"live_time"`
|
||||
Delay float64 `json:"delay"`
|
||||
}
|
||||
|
||||
type EndToEndDelay struct {
|
||||
driver *DriverExt
|
||||
StartTime string `json:"startTime"`
|
||||
EndTime string `json:"endTime"`
|
||||
Interval int `json:"interval"` // seconds
|
||||
Duration int `json:"duration"` // seconds
|
||||
Timelines []timeLog `json:"timelines"`
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) CollectEndToEndDelay(options ...ActionOption) {
|
||||
dataOptions := NewActionOptions(options...)
|
||||
startTime := time.Now()
|
||||
|
||||
if dataOptions.Interval == 0 {
|
||||
dataOptions.Interval = 5
|
||||
}
|
||||
if dataOptions.Timeout == 0 {
|
||||
dataOptions.Timeout = 60
|
||||
}
|
||||
|
||||
endToEndDelay := &EndToEndDelay{
|
||||
driver: dExt,
|
||||
Duration: int(dataOptions.Timeout),
|
||||
Interval: int(dataOptions.Interval),
|
||||
StartTime: startTime.Format("2006-01-02 15:04:05"),
|
||||
}
|
||||
|
||||
endToEndDelay.Start()
|
||||
|
||||
dExt.cacheStepData.e2eDelay = endToEndDelay.Timelines
|
||||
}
|
||||
|
||||
func (ete *EndToEndDelay) getCurrentLiveTime(utcTime time.Time) error {
|
||||
utcTimeStr := utcTime.Format("2006-01-02 15:04:05")
|
||||
ocrTexts, err := ete.driver.GetScreenTexts()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("get ocr texts failed")
|
||||
return err
|
||||
}
|
||||
|
||||
// filter ocr texts with time format
|
||||
var liveTimeTexts []string
|
||||
for _, ocrText := range ocrTexts {
|
||||
if len(ocrText.Text) < 10 || strings.Contains(ocrText.Text, ":") {
|
||||
continue
|
||||
}
|
||||
// exclude digit(s) recognized as letter(s)
|
||||
_, errParseInt := strconv.ParseInt(ocrText.Text[:10], 10, 64)
|
||||
if errParseInt != nil {
|
||||
continue
|
||||
}
|
||||
liveTimeTexts = append(liveTimeTexts, ocrText.Text)
|
||||
}
|
||||
|
||||
var liveTimeText string
|
||||
if len(liveTimeTexts) != 0 {
|
||||
liveTimeText = liveTimeTexts[0]
|
||||
} else {
|
||||
log.Warn().Msg("no time text found")
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(liveTimeText) < 13 {
|
||||
for (13 - len(liveTimeText)) > 0 {
|
||||
liveTimeText += "0"
|
||||
}
|
||||
}
|
||||
liveTimeInt, err := strconv.Atoi(liveTimeText)
|
||||
if err != nil {
|
||||
liveTimeInt = 0
|
||||
}
|
||||
liveTimeSInt, err := strconv.Atoi(liveTimeText[:10])
|
||||
if err != nil {
|
||||
liveTimeSInt = 0
|
||||
}
|
||||
liveTimeNSInt, err := strconv.Atoi(liveTimeText[10:13])
|
||||
if err != nil {
|
||||
liveTimeNSInt = 0
|
||||
}
|
||||
liveTimeStr := time.Unix(int64(liveTimeSInt), int64(liveTimeNSInt*1000*1000)).Format("2006-01-02 15:04:05")
|
||||
log.Info().
|
||||
Str("utcTime", utcTimeStr).
|
||||
Int64("utcTimeInt", utcTime.UnixMilli()).
|
||||
Str("liveTime", liveTimeStr).
|
||||
Int64("liveTimeInt", int64(liveTimeInt)).
|
||||
Float64("delay", float64(utcTime.UnixMilli()-int64(liveTimeInt))/1000).
|
||||
Msg("log live time")
|
||||
ete.Timelines = append(ete.Timelines, timeLog{
|
||||
UTCTimeStr: utcTimeStr,
|
||||
UTCTime: utcTime.UnixMilli(),
|
||||
LiveTimeStr: liveTimeStr,
|
||||
LiveTime: int64(liveTimeInt),
|
||||
Delay: float64(utcTime.UnixMilli()-int64(liveTimeInt)) / 1000,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ete *EndToEndDelay) Start() {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
|
||||
timer := time.NewTimer(time.Duration(ete.Duration) * time.Second)
|
||||
for {
|
||||
select {
|
||||
case <-timer.C:
|
||||
ete.EndTime = time.Now().Format("2006-01-02 15:04:05")
|
||||
return
|
||||
case <-c:
|
||||
ete.EndTime = time.Now().Format("2006-01-02 15:04:05")
|
||||
return
|
||||
default:
|
||||
utcTime := time.Now()
|
||||
if utcTime.Unix()%int64(ete.Interval) == 0 {
|
||||
_ = ete.getCurrentLiveTime(utcTime)
|
||||
} else {
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,6 +91,9 @@ type PopupInfo struct {
|
||||
*ClosePopupsResult
|
||||
CloseStatus string `json:"close_status"` // found/success/fail
|
||||
ClosePoints []PointF `json:"close_points,omitempty"` // CV 识别的所有关闭按钮(仅关闭按钮,可能存在多个)
|
||||
RetryCount int `json:"retry_count"`
|
||||
PicName string `json:"pic_name"`
|
||||
PicURL string `json:"pic_url"`
|
||||
}
|
||||
|
||||
func (p *PopupInfo) getClosePoint(lastPopup *PopupInfo) (*PointF, error) {
|
||||
|
||||
@@ -67,9 +67,10 @@ type ImageResult struct {
|
||||
// Media(媒体)
|
||||
// Chat(语音)
|
||||
// Event(赛事)
|
||||
LiveType string `json:"liveType,omitempty"` // 直播间类型
|
||||
UIResult UIResultMap `json:"uiResult,omitempty"` // 图标检测
|
||||
ClosePopupsResult *ClosePopupsResult `json:"closeResult,omitempty"` // 弹窗按钮检测
|
||||
LiveType string `json:"liveType,omitempty"` // 直播间类型
|
||||
LivePopularity int64 `json:"livePopularity,omitempty"` // 直播间热度
|
||||
UIResult UIResultMap `json:"uiResult,omitempty"` // 图标检测
|
||||
ClosePopupsResult *ClosePopupsResult `json:"closeResult,omitempty"` // 弹窗按钮检测
|
||||
}
|
||||
|
||||
type APIResponseImage struct {
|
||||
@@ -218,8 +219,19 @@ func (s *veDEMImageService) GetImage(imageBuf *bytes.Buffer, options ...ActionOp
|
||||
bodyWriter.WriteField("uiTypes", uiType)
|
||||
}
|
||||
|
||||
// 使用高精度集群
|
||||
bodyWriter.WriteField("ocrCluster", "highPrecision")
|
||||
|
||||
if actionOptions.ScreenShotWithOCRCluster != "" {
|
||||
bodyWriter.WriteField("ocrCluster", actionOptions.ScreenShotWithOCRCluster)
|
||||
}
|
||||
|
||||
if actionOptions.Timeout > 0 {
|
||||
bodyWriter.WriteField("timeout", fmt.Sprintf("%v", actionOptions.Timeout))
|
||||
} else {
|
||||
bodyWriter.WriteField("timeout", fmt.Sprintf("%v", 10))
|
||||
}
|
||||
|
||||
formWriter, err := bodyWriter.CreateFormFile("image", "screenshot.png")
|
||||
if err != nil {
|
||||
err = errors.Wrap(code.CVRequestError,
|
||||
@@ -406,18 +418,19 @@ func (dExt *DriverExt) GetScreenResult(options ...ActionOption) (screenResult *S
|
||||
screenResult.Texts = imageResult.OCRResult.ToOCRTexts()
|
||||
screenResult.UploadedURL = imageResult.URL
|
||||
screenResult.Icons = imageResult.UIResult
|
||||
screenResult.Video = &Video{LiveType: imageResult.LiveType, ViewCount: imageResult.LivePopularity}
|
||||
|
||||
if actionOptions.ScreenShotWithClosePopups {
|
||||
popup := &PopupInfo{
|
||||
if actionOptions.ScreenShotWithClosePopups && imageResult.ClosePopupsResult != nil {
|
||||
screenResult.Popup = &PopupInfo{
|
||||
ClosePopupsResult: imageResult.ClosePopupsResult,
|
||||
PicName: imagePath,
|
||||
PicURL: imageResult.URL,
|
||||
}
|
||||
|
||||
closeAreas, _ := imageResult.UIResult.FilterUIResults([]string{"close"})
|
||||
for _, closeArea := range closeAreas {
|
||||
popup.ClosePoints = append(popup.ClosePoints, closeArea.Center())
|
||||
screenResult.Popup.ClosePoints = append(screenResult.Popup.ClosePoints, closeArea.Center())
|
||||
}
|
||||
|
||||
screenResult.Popup = popup
|
||||
}
|
||||
}
|
||||
|
||||
@@ -482,8 +495,8 @@ func (box Box) IsEmpty() bool {
|
||||
func (box Box) IsIdentical(box2 Box) bool {
|
||||
// set the coordinate precision to 1 pixel
|
||||
return box.Point.IsIdentical(box2.Point) &&
|
||||
math.Abs(box.Width-box2.Width) < 1 &&
|
||||
math.Abs(box.Height-box2.Height) < 1
|
||||
builtin.IsZeroFloat64(math.Abs(box.Width-box2.Width)) &&
|
||||
builtin.IsZeroFloat64(math.Abs(box.Height-box2.Height))
|
||||
}
|
||||
|
||||
func (box Box) Center() PointF {
|
||||
@@ -538,7 +551,7 @@ func (u UIResultMap) FilterUIResults(uiTypes []string) (uiResults UIResults, err
|
||||
return
|
||||
}
|
||||
}
|
||||
err = errors.Errorf("UI types %v not detected", uiTypes)
|
||||
err = errors.Wrap(code.CVResultNotFoundError, fmt.Sprintf("UI types %v not detected", uiTypes))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -11,25 +11,56 @@ import (
|
||||
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||
)
|
||||
|
||||
var directionSlice = [][]float64{
|
||||
{0.85, 0.83, 0.85, 0.1},
|
||||
{0.9, 0.75, 0.9, 0.1},
|
||||
{0.6, 0.5, 0.6, 0.1},
|
||||
}
|
||||
|
||||
func assertRelative(p float64) bool {
|
||||
return p >= 0 && p <= 1
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) SwipeUpUtil(count int64, options ...ActionOption) error {
|
||||
width := dExt.windowSize.Width
|
||||
height := dExt.windowSize.Height
|
||||
|
||||
fromX := float64(width) * directionSlice[count%3][0]
|
||||
fromY := float64(height) * directionSlice[count%3][1]
|
||||
toX := float64(width) * directionSlice[count%3][2]
|
||||
toY := float64(height) * directionSlice[count%3][3]
|
||||
|
||||
return dExt.Driver.SwipeFloat(fromX, fromY, toX, toY, options...)
|
||||
}
|
||||
|
||||
// SwipeRelative swipe from relative position [fromX, fromY] to relative position [toX, toY]
|
||||
func (dExt *DriverExt) SwipeRelative(fromX, fromY, toX, toY float64, options ...ActionOption) error {
|
||||
width := dExt.windowSize.Width
|
||||
height := dExt.windowSize.Height
|
||||
orientation, err := dExt.Driver.Orientation()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("swipe from (%v, %v) to (%v, %v) get orientation failed, use default orientation",
|
||||
fromX, fromY, toX, toY)
|
||||
orientation = OrientationPortrait
|
||||
}
|
||||
|
||||
if !assertRelative(fromX) || !assertRelative(fromY) ||
|
||||
!assertRelative(toX) || !assertRelative(toY) {
|
||||
return fmt.Errorf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1",
|
||||
fromX, fromY, toX, toY)
|
||||
}
|
||||
|
||||
fromX = float64(width) * fromX
|
||||
fromY = float64(height) * fromY
|
||||
toX = float64(width) * toX
|
||||
toY = float64(height) * toY
|
||||
// 左转和右转都是"LANDSCAPE"
|
||||
if orientation == OrientationPortrait {
|
||||
fromX = float64(width) * fromX
|
||||
fromY = float64(height) * fromY
|
||||
toX = float64(width) * toX
|
||||
toY = float64(height) * toY
|
||||
} else {
|
||||
fromX = float64(height) * fromX
|
||||
fromY = float64(width) * fromY
|
||||
toX = float64(height) * toX
|
||||
toY = float64(width) * toY
|
||||
}
|
||||
|
||||
return dExt.Driver.SwipeFloat(fromX, fromY, toX, toY, options...)
|
||||
}
|
||||
@@ -114,7 +145,7 @@ func (dExt *DriverExt) prepareSwipeAction(options ...ActionOption) func(d *Drive
|
||||
log.Error().Err(err).Msgf("swipe %s failed", d)
|
||||
return err
|
||||
}
|
||||
} else if d, ok := swipeDirection.([]float64); ok {
|
||||
} else if d, ok := swipeDirection.([]float64); ok && len(d) == 4 {
|
||||
// custom direction: [fromX, fromY, toX, toY]
|
||||
if err := dExt.SwipeRelative(d[0], d[1], d[2], d[3], options...); err != nil {
|
||||
log.Error().Err(err).Msgf("swipe from (%v, %v) to (%v, %v) failed",
|
||||
@@ -178,6 +209,11 @@ func (dExt *DriverExt) swipeToTapApp(appName string, options ...ActionOption) er
|
||||
return errors.Wrap(err, "go to home screen failed")
|
||||
}
|
||||
|
||||
// automatic handling popups before swipe
|
||||
if err := dExt.ClosePopupsHandler(); err != nil {
|
||||
log.Error().Err(err).Msg("auto handle popup failed")
|
||||
}
|
||||
|
||||
// swipe to first screen
|
||||
for i := 0; i < 5; i++ {
|
||||
dExt.SwipeRight()
|
||||
|
||||
@@ -2,6 +2,8 @@ package uixt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (dExt *DriverExt) TapAbsXY(x, y float64, options ...ActionOption) error {
|
||||
@@ -15,9 +17,19 @@ func (dExt *DriverExt) TapXY(x, y float64, options ...ActionOption) error {
|
||||
return fmt.Errorf("x, y percentage should be <= 1, got x=%v, y=%v", x, y)
|
||||
}
|
||||
|
||||
x = x * float64(dExt.windowSize.Width)
|
||||
y = y * float64(dExt.windowSize.Height)
|
||||
|
||||
orientation, err := dExt.Driver.Orientation()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("tap (%v, %v) get orientation failed, use default orientation",
|
||||
x, y)
|
||||
orientation = OrientationPortrait
|
||||
}
|
||||
if orientation == OrientationPortrait {
|
||||
x = x * float64(dExt.windowSize.Width)
|
||||
y = y * float64(dExt.windowSize.Height)
|
||||
} else {
|
||||
x = x * float64(dExt.windowSize.Height)
|
||||
y = y * float64(dExt.windowSize.Width)
|
||||
}
|
||||
return dExt.TapAbsXY(x, y, options...)
|
||||
}
|
||||
|
||||
@@ -86,9 +98,19 @@ func (dExt *DriverExt) DoubleTapXY(x, y float64) error {
|
||||
if x > 1 || y > 1 {
|
||||
return fmt.Errorf("x, y percentage should be < 1, got x=%v, y=%v", x, y)
|
||||
}
|
||||
|
||||
x = x * float64(dExt.windowSize.Width)
|
||||
y = y * float64(dExt.windowSize.Height)
|
||||
orientation, err := dExt.Driver.Orientation()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msgf("tap (%v, %v) get orientation failed, use default orientation",
|
||||
x, y)
|
||||
orientation = OrientationPortrait
|
||||
}
|
||||
if orientation == OrientationPortrait {
|
||||
x = x * float64(dExt.windowSize.Width)
|
||||
y = y * float64(dExt.windowSize.Height)
|
||||
} else {
|
||||
x = x * float64(dExt.windowSize.Height)
|
||||
y = y * float64(dExt.windowSize.Width)
|
||||
}
|
||||
return dExt.Driver.DoubleTapFloat(x, y)
|
||||
}
|
||||
|
||||
|
||||
@@ -114,7 +114,12 @@ func (vc *VideoCrawler) isTargetAchieved() bool {
|
||||
|
||||
func (vc *VideoCrawler) exitLiveRoom() error {
|
||||
log.Info().Msg("press back to exit live room")
|
||||
return vc.driverExt.Driver.PressBack()
|
||||
err := vc.driverExt.Driver.PressBack()
|
||||
time.Sleep(time.Duration(3) * time.Second)
|
||||
if vc.driverExt.TapByOCR("退出直播间") == nil {
|
||||
log.Info().Msg("clicked the button to exit the live room successfully")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -149,6 +154,9 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
||||
dExt.cacheStepData.videoCrawler = crawler
|
||||
}()
|
||||
|
||||
// flag,仅当 flag 为 false 时,并处于内流时,才执行退出直播间逻辑
|
||||
isFeed := true
|
||||
|
||||
// loop until target count achieved or timeout
|
||||
// the main loop is feed crawler
|
||||
crawler.timer = time.NewTimer(time.Duration(configs.Timeout) * time.Second)
|
||||
@@ -168,21 +176,22 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
||||
// swipe to next feed video
|
||||
log.Info().Msg("swipe to next feed video")
|
||||
swipeStartTime := time.Now()
|
||||
if err = dExt.SwipeRelative(0.9, 0.8, 0.9, 0.1, WithOffsetRandomRange(-10, 10)); err != nil {
|
||||
if err = dExt.SwipeUpUtil(crawler.failedCount, WithOffsetRandomRange(-10, 10)); err != nil {
|
||||
log.Error().Err(err).Msg("feed swipe up failed")
|
||||
return err
|
||||
}
|
||||
swipeFinishTime := time.Now()
|
||||
|
||||
// get app event trackings
|
||||
// retry 10 times if get feed failed, abort if fail 10 consecutive times
|
||||
// retry 3 times if get feed failed, abort if fail 3 consecutive times
|
||||
fetchVideoStartTime := time.Now()
|
||||
currentVideo, err := crawler.getCurrentVideo()
|
||||
if err != nil || currentVideo.Type == "" {
|
||||
crawler.failedCount++
|
||||
if crawler.failedCount >= 10 {
|
||||
// failed 10 consecutive times
|
||||
if crawler.failedCount >= 3 {
|
||||
// failed 3 consecutive times
|
||||
return errors.Wrap(code.TrackingGetError,
|
||||
"get current feed video failed 10 consecutive times")
|
||||
"get current feed video failed 3 consecutive times")
|
||||
}
|
||||
log.Warn().
|
||||
Int64("failedCount", crawler.failedCount).
|
||||
@@ -196,12 +205,14 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
||||
// retry
|
||||
continue
|
||||
}
|
||||
fetchVideoFinishTime := time.Now()
|
||||
|
||||
// 直播预览流线上概率
|
||||
livePreviewProb := crawler.getLivePreviewProb()
|
||||
|
||||
switch currentVideo.Type {
|
||||
case VideoType_PreviewLive:
|
||||
isFeed = true
|
||||
// 直播预览流
|
||||
var skipEnterLive bool
|
||||
if crawler.isLiveTargetAchieved() {
|
||||
@@ -227,13 +238,13 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
||||
log.Error().Err(err).Msg("tap live video failed")
|
||||
continue
|
||||
}
|
||||
currentVideo.Type = VideoType_Live
|
||||
} else {
|
||||
// skip entering live room
|
||||
// only mock simulation play duration
|
||||
sleepTime := math.Min(float64(currentVideo.SimulationPlayDuration), float64(currentVideo.RandomPlayDuration))
|
||||
currentVideo.PlayDuration = int64(sleepTime)
|
||||
}
|
||||
|
||||
fallthrough
|
||||
|
||||
case VideoType_Live:
|
||||
@@ -260,14 +271,18 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
||||
currentVideo.LiveType = screenResult.imageResult.LiveType
|
||||
}
|
||||
|
||||
// simulation watch feed video
|
||||
sleepStrict(swipeFinishTime, currentVideo.PlayDuration)
|
||||
// simulation watch live video
|
||||
simulationPlayDuration := math.Min(float64(currentVideo.PlayDuration), 300000)
|
||||
sleepStrict(swipeFinishTime, int64(simulationPlayDuration))
|
||||
|
||||
screenResult.Video = currentVideo
|
||||
screenResult.Resolution = dExt.windowSize
|
||||
screenResult.SwipeStartTime = swipeStartTime.UnixMilli()
|
||||
screenResult.SwipeFinishTime = swipeFinishTime.UnixMilli()
|
||||
screenResult.TotalElapsed = time.Since(swipeFinishTime).Milliseconds()
|
||||
screenResult.FetchVideoStartTime = fetchVideoStartTime.UnixMilli()
|
||||
screenResult.FetchVideoFinishTime = fetchVideoFinishTime.UnixMilli()
|
||||
screenResult.FetchVideoElapsed = fetchVideoFinishTime.Sub(fetchVideoStartTime).Milliseconds()
|
||||
|
||||
var exitLive bool
|
||||
if crawler.isLiveTargetAchieved() {
|
||||
@@ -278,7 +293,9 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
||||
log.Info().Interface("livePreviewProb", livePreviewProb).Msg("exit live room by preview live chance")
|
||||
exitLive = true
|
||||
}
|
||||
if exitLive && currentVideo.Type == VideoType_Live {
|
||||
|
||||
// isFeed:通过预览流进入内流失败的情况下,防止使用退出直播间逻辑,影响:首次进入内流,至少会消费两个直播间才能退出
|
||||
if !isFeed && exitLive && currentVideo.Type == VideoType_Live {
|
||||
err = crawler.exitLiveRoom()
|
||||
if err != nil {
|
||||
if errors.Is(err, code.TimeoutError) || errors.Is(err, code.InterruptError) {
|
||||
@@ -286,9 +303,12 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
||||
}
|
||||
log.Error().Err(err).Msg("run live crawler failed, continue")
|
||||
}
|
||||
} else {
|
||||
isFeed = false
|
||||
}
|
||||
|
||||
default:
|
||||
isFeed = true
|
||||
// 点播 || 图文 || 广告 || etc.
|
||||
crawler.FeedCount++
|
||||
log.Info().Interface("video", currentVideo).Msg(FOUND_FEED_SUCCESS)
|
||||
@@ -298,13 +318,17 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
||||
Video: currentVideo,
|
||||
|
||||
// log swipe timelines
|
||||
SwipeStartTime: swipeStartTime.UnixMilli(),
|
||||
SwipeFinishTime: swipeFinishTime.UnixMilli(),
|
||||
SwipeStartTime: swipeStartTime.UnixMilli(),
|
||||
SwipeFinishTime: swipeFinishTime.UnixMilli(),
|
||||
FetchVideoStartTime: fetchVideoStartTime.UnixMilli(),
|
||||
FetchVideoFinishTime: fetchVideoFinishTime.UnixMilli(),
|
||||
FetchVideoElapsed: fetchVideoFinishTime.Sub(fetchVideoStartTime).Milliseconds(),
|
||||
}
|
||||
dExt.cacheStepData.screenResults[time.Now().String()] = screenResult
|
||||
|
||||
// simulation watch feed video
|
||||
sleepStrict(swipeFinishTime, currentVideo.PlayDuration)
|
||||
simulationPlayDuration := math.Min(float64(currentVideo.PlayDuration), 600000)
|
||||
sleepStrict(swipeFinishTime, int64(simulationPlayDuration))
|
||||
screenResult.TotalElapsed = time.Since(swipeFinishTime).Milliseconds()
|
||||
}
|
||||
|
||||
@@ -340,6 +364,9 @@ type Video struct {
|
||||
UserName string `json:"user_name"` // 视频作者
|
||||
Duration int64 `json:"duration,omitempty"` // 视频时长(ms)
|
||||
Caption string `json:"caption,omitempty"` // 视频文案
|
||||
// 作者信息
|
||||
UserID string `json:"user_id"` // 作者用户名
|
||||
FollowerCount int64 `json:"follower_count"` // 作者粉丝数
|
||||
// 视频热度数据
|
||||
ViewCount int64 `json:"view_count,omitempty"` // feed 观看数
|
||||
LikeCount int64 `json:"like_count,omitempty"` // feed 点赞数
|
||||
|
||||
149
hrp/pkg/utf7/decoder.go
Normal file
149
hrp/pkg/utf7/decoder.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package utf7
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"unicode/utf16"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
// ErrInvalidUTF7 means that a transformer encountered invalid UTF-7.
|
||||
var ErrInvalidUTF7 = errors.New("utf7: invalid UTF-7")
|
||||
|
||||
type decoder struct {
|
||||
ascii bool
|
||||
}
|
||||
|
||||
func (d *decoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
|
||||
for i := 0; i < len(src); i++ {
|
||||
ch := src[i]
|
||||
|
||||
if ch < min || ch > max { // Illegal code point in ASCII mode
|
||||
err = ErrInvalidUTF7
|
||||
return
|
||||
}
|
||||
|
||||
if ch != '&' {
|
||||
if nDst+1 > len(dst) {
|
||||
err = transform.ErrShortDst
|
||||
return
|
||||
}
|
||||
|
||||
nSrc++
|
||||
|
||||
dst[nDst] = ch
|
||||
nDst++
|
||||
|
||||
d.ascii = true
|
||||
continue
|
||||
}
|
||||
|
||||
// Find the end of the Base64 or "&-" segment
|
||||
start := i + 1
|
||||
for i++; i < len(src) && src[i] != '-'; i++ {
|
||||
if src[i] == '\r' || src[i] == '\n' { // base64 package ignores CR and LF
|
||||
err = ErrInvalidUTF7
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if i == len(src) { // Implicit shift ("&...")
|
||||
if atEOF {
|
||||
err = ErrInvalidUTF7
|
||||
} else {
|
||||
err = transform.ErrShortSrc
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var b []byte
|
||||
if i == start { // Escape sequence "&-"
|
||||
b = []byte{'&'}
|
||||
d.ascii = true
|
||||
} else { // Control or non-ASCII code points in base64
|
||||
if !d.ascii { // Null shift ("&...-&...-")
|
||||
err = ErrInvalidUTF7
|
||||
return
|
||||
}
|
||||
|
||||
b = decode(src[start:i])
|
||||
d.ascii = false
|
||||
}
|
||||
|
||||
if len(b) == 0 { // Bad encoding
|
||||
err = ErrInvalidUTF7
|
||||
return
|
||||
}
|
||||
|
||||
if nDst+len(b) > len(dst) {
|
||||
d.ascii = true
|
||||
err = transform.ErrShortDst
|
||||
return
|
||||
}
|
||||
|
||||
nSrc = i + 1
|
||||
|
||||
for _, ch := range b {
|
||||
dst[nDst] = ch
|
||||
nDst++
|
||||
}
|
||||
}
|
||||
|
||||
if atEOF {
|
||||
d.ascii = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *decoder) Reset() {
|
||||
d.ascii = true
|
||||
}
|
||||
|
||||
// Extracts UTF-16-BE bytes from base64 data and converts them to UTF-8.
|
||||
// A nil slice is returned if the encoding is invalid.
|
||||
func decode(b64 []byte) []byte {
|
||||
var b []byte
|
||||
|
||||
// Allocate a single block of memory large enough to store the Base64 data
|
||||
// (if padding is required), UTF-16-BE bytes, and decoded UTF-8 bytes.
|
||||
// Since a 2-byte UTF-16 sequence may expand into a 3-byte UTF-8 sequence,
|
||||
// double the space allocation for UTF-8.
|
||||
if n := len(b64); b64[n-1] == '=' {
|
||||
return nil
|
||||
} else if n&3 == 0 {
|
||||
b = make([]byte, b64Enc.DecodedLen(n)*3)
|
||||
} else {
|
||||
n += 4 - n&3
|
||||
b = make([]byte, n+b64Enc.DecodedLen(n)*3)
|
||||
copy(b[copy(b, b64):n], []byte("=="))
|
||||
b64, b = b[:n], b[n:]
|
||||
}
|
||||
|
||||
// Decode Base64 into the first 1/3rd of b
|
||||
n, err := b64Enc.Decode(b, b64)
|
||||
if err != nil || n&1 == 1 {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decode UTF-16-BE into the remaining 2/3rds of b
|
||||
b, s := b[:n], b[n:]
|
||||
j := 0
|
||||
for i := 0; i < n; i += 2 {
|
||||
r := rune(b[i])<<8 | rune(b[i+1])
|
||||
if utf16.IsSurrogate(r) {
|
||||
if i += 2; i == n {
|
||||
return nil
|
||||
}
|
||||
r2 := rune(b[i])<<8 | rune(b[i+1])
|
||||
if r = utf16.DecodeRune(r, r2); r == repl {
|
||||
return nil
|
||||
}
|
||||
} else if min <= r && r <= max {
|
||||
return nil
|
||||
}
|
||||
j += utf8.EncodeRune(s[j:], r)
|
||||
}
|
||||
return s[:j]
|
||||
}
|
||||
91
hrp/pkg/utf7/encoder.go
Normal file
91
hrp/pkg/utf7/encoder.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package utf7
|
||||
|
||||
import (
|
||||
"unicode/utf16"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/text/transform"
|
||||
)
|
||||
|
||||
type encoder struct{}
|
||||
|
||||
func (e *encoder) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err error) {
|
||||
for i := 0; i < len(src); {
|
||||
ch := src[i]
|
||||
|
||||
var b []byte
|
||||
if min <= ch && ch <= max {
|
||||
b = []byte{ch}
|
||||
if ch == '&' {
|
||||
b = append(b, '-')
|
||||
}
|
||||
|
||||
i++
|
||||
} else {
|
||||
start := i
|
||||
|
||||
// Find the next printable ASCII code point
|
||||
i++
|
||||
for i < len(src) && (src[i] < min || src[i] > max) {
|
||||
i++
|
||||
}
|
||||
|
||||
if !atEOF && i == len(src) {
|
||||
err = transform.ErrShortSrc
|
||||
return
|
||||
}
|
||||
|
||||
b = encode(src[start:i])
|
||||
}
|
||||
|
||||
if nDst+len(b) > len(dst) {
|
||||
err = transform.ErrShortDst
|
||||
return
|
||||
}
|
||||
|
||||
nSrc = i
|
||||
|
||||
for _, ch := range b {
|
||||
dst[nDst] = ch
|
||||
nDst++
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e *encoder) Reset() {}
|
||||
|
||||
// Converts string s from UTF-8 to UTF-16-BE, encodes the result as base64,
|
||||
// removes the padding, and adds UTF-7 shifts.
|
||||
func encode(s []byte) []byte {
|
||||
// len(s) is sufficient for UTF-8 to UTF-16 conversion if there are no
|
||||
// control code points (see table below).
|
||||
b := make([]byte, 0, len(s)+4)
|
||||
for len(s) > 0 {
|
||||
r, size := utf8.DecodeRune(s)
|
||||
if r > utf8.MaxRune {
|
||||
r, size = utf8.RuneError, 1 // Bug fix (issue 3785)
|
||||
}
|
||||
s = s[size:]
|
||||
if r1, r2 := utf16.EncodeRune(r); r1 != repl {
|
||||
b = append(b, byte(r1>>8), byte(r1))
|
||||
r = r2
|
||||
}
|
||||
b = append(b, byte(r>>8), byte(r))
|
||||
}
|
||||
|
||||
// Encode as base64
|
||||
n := b64Enc.EncodedLen(len(b)) + 2
|
||||
b64 := make([]byte, n)
|
||||
b64Enc.Encode(b64[1:], b)
|
||||
|
||||
// Strip padding
|
||||
n -= 2 - (len(b)+2)%3
|
||||
b64 = b64[:n]
|
||||
|
||||
// Add UTF-7 shifts
|
||||
b64[0] = '&'
|
||||
b64[n-1] = '-'
|
||||
return b64
|
||||
}
|
||||
34
hrp/pkg/utf7/utf7.go
Normal file
34
hrp/pkg/utf7/utf7.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// Package utf7 implements modified UTF-7 encoding defined in RFC 3501 section 5.1.3
|
||||
package utf7
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
|
||||
"golang.org/x/text/encoding"
|
||||
)
|
||||
|
||||
const (
|
||||
min = 0x20 // Minimum self-representing UTF-7 value
|
||||
max = 0x7E // Maximum self-representing UTF-7 value
|
||||
|
||||
repl = '\uFFFD' // Unicode replacement code point
|
||||
)
|
||||
|
||||
var b64Enc = base64.NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,")
|
||||
|
||||
type enc struct{}
|
||||
|
||||
func (e enc) NewDecoder() *encoding.Decoder {
|
||||
return &encoding.Decoder{
|
||||
Transformer: &decoder{true},
|
||||
}
|
||||
}
|
||||
|
||||
func (e enc) NewEncoder() *encoding.Encoder {
|
||||
return &encoding.Encoder{
|
||||
Transformer: &encoder{},
|
||||
}
|
||||
}
|
||||
|
||||
// Encoding is the modified UTF-7 encoding.
|
||||
var Encoding encoding.Encoding = enc{}
|
||||
Reference in New Issue
Block a user