mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-03 06:49:38 +08:00
fix: kuake input unicode error
This commit is contained in:
@@ -2,6 +2,7 @@ package uixt
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -353,7 +354,7 @@ func (ad *adbDriver) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffe
|
|||||||
|
|
||||||
func (ad *adbDriver) SendKeys(text string, options ...ActionOption) (err error) {
|
func (ad *adbDriver) SendKeys(text string, options ...ActionOption) (err error) {
|
||||||
// adb shell input text <text>
|
// adb shell input text <text>
|
||||||
_, err = ad.adbClient.RunShellCommand("input", "text", text)
|
_, err = ad.adbClient.RunShellCommand("input", "text", encodeUnicodeText(text))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "send keys failed")
|
return errors.Wrap(err, "send keys failed")
|
||||||
}
|
}
|
||||||
@@ -620,6 +621,34 @@ func (ad *adbDriver) AssertForegroundApp(packageName string, activityType ...str
|
|||||||
"assert foreground activity failed")
|
"assert foreground activity failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func encodeUnicode(c int32) string {
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
// Convert each rune (character) into two bytes
|
||||||
|
buffer.WriteByte(byte(c >> 8))
|
||||||
|
buffer.WriteByte(byte(c & 0xFF))
|
||||||
|
// Convert buffer bytes to base64 encoding
|
||||||
|
encoded := base64.StdEncoding.EncodeToString(buffer.Bytes())
|
||||||
|
// Replace "/" with "," and remove trailing "="
|
||||||
|
encoded = strings.ReplaceAll(encoded, "/", ",")
|
||||||
|
return strings.TrimRight(encoded, "=")
|
||||||
|
}
|
||||||
|
|
||||||
|
func encodeUnicodeText(text string) string {
|
||||||
|
text = strings.ReplaceAll(text, "&", "&-")
|
||||||
|
var sb strings.Builder
|
||||||
|
sb.WriteRune('"')
|
||||||
|
for _, c := range text {
|
||||||
|
if c <= 127 {
|
||||||
|
sb.WriteRune(c)
|
||||||
|
} else {
|
||||||
|
// Encode non-ASCII character and append it
|
||||||
|
sb.WriteString("&" + encodeUnicode(c) + "-")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.WriteRune('"')
|
||||||
|
return sb.String()
|
||||||
|
}
|
||||||
|
|
||||||
var androidActivities = map[string]map[string][]string{
|
var androidActivities = map[string]map[string][]string{
|
||||||
// DY
|
// DY
|
||||||
"com.ss.android.ugc.aweme": {
|
"com.ss.android.ugc.aweme": {
|
||||||
|
|||||||
@@ -255,17 +255,17 @@ func TestDriver_SendKeys(t *testing.T) {
|
|||||||
}
|
}
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
|
|
||||||
err = driver.SendKeys("def")
|
//err = driver.SendKeys("def")
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
//}
|
||||||
time.Sleep(time.Second * 2)
|
//time.Sleep(time.Second * 2)
|
||||||
|
|
||||||
err = driver.SendKeys("\\n")
|
//err = driver.SendKeys("\\n")
|
||||||
// err = driver.SendKeys(`\n`, false)
|
// err = driver.SendKeys(`\n`, false)
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDriver_PressBack(t *testing.T) {
|
func TestDriver_PressBack(t *testing.T) {
|
||||||
@@ -437,3 +437,23 @@ func TestConvertPoints(t *testing.T) {
|
|||||||
jsons, _ := json.Marshal(eps)
|
jsons, _ := json.Marshal(eps)
|
||||||
println(fmt.Sprintf("%v", string(jsons)))
|
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))
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user