change: make assert msg optional

This commit is contained in:
debugtalk 2022-08-15 21:53:33 +08:00
parent e76d396519
commit 0407c02391
5 changed files with 139 additions and 83 deletions

View File

@ -38,6 +38,12 @@ const (
uiLongClick MobileMethod = "long_click"
uiSwipe MobileMethod = "swipe"
uiInput MobileMethod = "input"
// UI validation
assertionNameExists string = "name_exists"
assertionNameNotExists string = "name_not_exists"
assertionXpathExists string = "xpath_exists"
assertionXpathNotExists string = "xpath_not_exists"
)
type MobileAction struct {

View File

@ -1,5 +1,7 @@
package hrp
import "fmt"
type AndroidAction struct {
MobileAction
Serial string `json:"serial,omitempty" yaml:"serial,omitempty"`
@ -164,12 +166,31 @@ type StepAndroidValidation struct {
step *TStep
}
func (s *StepAndroidValidation) AssertXpathExists(expectedXpath string, msg string) *StepAndroidValidation {
func (s *StepAndroidValidation) AssertXpathExists(expectedXpath string, msg ...string) *StepAndroidValidation {
v := Validator{
Check: "UI",
Assert: "xpath_exists",
Expect: expectedXpath,
Message: msg,
Check: "UI",
Assert: assertionXpathExists,
Expect: expectedXpath,
}
if len(msg) > 0 {
v.Message = msg[0]
} else {
v.Message = fmt.Sprintf("xpath [%s] not found", expectedXpath)
}
s.step.Validators = append(s.step.Validators, v)
return s
}
func (s *StepAndroidValidation) AssertXpathNotExists(expectedXpath string, msg ...string) *StepAndroidValidation {
v := Validator{
Check: "UI",
Assert: assertionXpathNotExists,
Expect: expectedXpath,
}
if len(msg) > 0 {
v.Message = msg[0]
} else {
v.Message = fmt.Sprintf("xpath [%s] should not exist", expectedXpath)
}
s.step.Validators = append(s.step.Validators, v)
return s

View File

@ -0,0 +1,28 @@
package hrp
import (
"fmt"
"testing"
)
func TestAndroidAction(t *testing.T) {
testCase := &TestCase{
Config: NewConfig("android ui action"),
TestSteps: []IStep{
NewStep("launch douyin").
Android().Serial("xxx").Click("抖音").
Validate().
AssertXpathExists("首页", "首页 tab 不存在").
AssertXpathExists("消息", "消息 tab 不存在"),
NewStep("swipe up and down").
Android().Serial("xxx").SwipeUp().SwipeUp().SwipeDown(),
},
}
tCase := testCase.ToTCase()
fmt.Println(tCase)
// err := NewRunner(t).Run(testCase)
// if err != nil {
// t.Fatal(err)
// }
}

View File

@ -242,45 +242,61 @@ type StepIOSValidation struct {
step *TStep
}
func (s *StepIOSValidation) AssertNameExists(expectedName string, msg string) *StepIOSValidation {
func (s *StepIOSValidation) AssertNameExists(expectedName string, msg ...string) *StepIOSValidation {
v := Validator{
Check: "UI",
Assert: "name_exists",
Expect: expectedName,
Message: msg,
Check: "UI",
Assert: assertionNameExists,
Expect: expectedName,
}
if len(msg) > 0 {
v.Message = msg[0]
} else {
v.Message = fmt.Sprintf("[%s] not found", expectedName)
}
s.step.Validators = append(s.step.Validators, v)
return s
}
func (s *StepIOSValidation) AssertNameNotExists(expectedName string, msg string) *StepIOSValidation {
func (s *StepIOSValidation) AssertNameNotExists(expectedName string, msg ...string) *StepIOSValidation {
v := Validator{
Check: "UI",
Assert: "name_not_exists",
Expect: expectedName,
Message: msg,
Check: "UI",
Assert: assertionNameNotExists,
Expect: expectedName,
}
if len(msg) > 0 {
v.Message = msg[0]
} else {
v.Message = fmt.Sprintf("[%s] should not exist", expectedName)
}
s.step.Validators = append(s.step.Validators, v)
return s
}
func (s *StepIOSValidation) AssertXpathExists(expectedXpath string, msg string) *StepIOSValidation {
func (s *StepIOSValidation) AssertXpathExists(expectedXpath string, msg ...string) *StepIOSValidation {
v := Validator{
Check: "UI",
Assert: "xpath_exists",
Expect: expectedXpath,
Message: msg,
Check: "UI",
Assert: assertionXpathExists,
Expect: expectedXpath,
}
if len(msg) > 0 {
v.Message = msg[0]
} else {
v.Message = fmt.Sprintf("xpath [%s] not found", expectedXpath)
}
s.step.Validators = append(s.step.Validators, v)
return s
}
func (s *StepIOSValidation) AssertXpathNotExists(expectedXpath string, msg string) *StepIOSValidation {
func (s *StepIOSValidation) AssertXpathNotExists(expectedXpath string, msg ...string) *StepIOSValidation {
v := Validator{
Check: "UI",
Assert: "xpath_not_exists",
Expect: expectedXpath,
Message: msg,
Check: "UI",
Assert: assertionXpathNotExists,
Expect: expectedXpath,
}
if len(msg) > 0 {
v.Message = msg[0]
} else {
v.Message = fmt.Sprintf("xpath [%s] should not exist", expectedXpath)
}
s.step.Validators = append(s.step.Validators, v)
return s
@ -673,13 +689,13 @@ func (w *wdaClient) doValidation(iValidators []interface{}) (validateResults []*
var result bool
switch validator.Assert {
case "xpath_exists":
case assertionXpathExists:
result = w.assertXpath(expected, true)
case "xpath_not_exists":
case assertionXpathNotExists:
result = w.assertXpath(expected, false)
case "name_exists":
case assertionNameExists:
result = w.assertName(expected, true)
case "name_not_exists":
case assertionNameNotExists:
result = w.assertName(expected, false)
}
if result {

View File

@ -5,28 +5,6 @@ import (
"testing"
)
func TestAndroidAction(t *testing.T) {
testCase := &TestCase{
Config: NewConfig("android ui action"),
TestSteps: []IStep{
NewStep("launch douyin").
Android().Serial("xxx").Click("抖音").
Validate().
AssertXpathExists("首页", "首页 tab 不存在").
AssertXpathExists("消息", "消息 tab 不存在"),
NewStep("swipe up and down").
Android().Serial("xxx").SwipeUp().SwipeUp().SwipeDown(),
},
}
tCase := testCase.ToTCase()
fmt.Println(tCase)
err := NewRunner(t).Run(testCase)
if err != nil {
t.Fatal(err)
}
}
func TestIOSSettingsAction(t *testing.T) {
testCase := &TestCase{
Config: NewConfig("ios ui action on Settings"),
@ -34,17 +12,18 @@ func TestIOSSettingsAction(t *testing.T) {
NewStep("launch Settings").
IOS().Home().Click("//*[@label='设置']").
Validate().
AssertNameExists("飞行模式", "「飞行模式」不存在").
AssertNameNotExists("飞行模式2", "「飞行模式2」不存在"),
AssertNameExists("飞行模式").
AssertNameNotExists("飞行模式2"),
NewStep("swipe up and down").
IOS().SwipeUp().SwipeUp().SwipeDown(),
},
}
fmt.Println(testCase)
err := NewRunner(t).Run(testCase)
if err != nil {
t.Fatal(err)
}
// err := NewRunner(t).Run(testCase)
// if err != nil {
// t.Fatal(err)
// }
}
func TestIOSSearchApp(t *testing.T) {
@ -54,16 +33,17 @@ func TestIOSSearchApp(t *testing.T) {
NewStep("进入 App 资源库 搜索框").
IOS().Home().SwipeLeft().Times(2).Click("dewey-search-field").
Validate().
AssertNameExists("取消", "「取消」不存在"),
AssertNameExists("取消"),
NewStep("搜索抖音").
IOS().Input("抖音\n"),
},
}
fmt.Println(testCase)
err := NewRunner(t).Run(testCase)
if err != nil {
t.Fatal(err)
}
// err := NewRunner(t).Run(testCase)
// if err != nil {
// t.Fatal(err)
// }
}
func TestIOSAppLaunch(t *testing.T) {
@ -80,11 +60,12 @@ func TestIOSAppLaunch(t *testing.T) {
IOS().AppLaunchUnattached("com.ss.iphone.article.News"),
},
}
fmt.Println(testCase)
err := NewRunner(t).Run(testCase)
if err != nil {
t.Fatal(err)
}
// err := NewRunner(t).Run(testCase)
// if err != nil {
// t.Fatal(err)
// }
}
func TestIOSWeixinLive(t *testing.T) {
@ -103,18 +84,19 @@ func TestIOSWeixinLive(t *testing.T) {
Click("发现").Sleep(5). // 进入「发现页」;等待 5 秒确保加载完成
Click([]float64{0.5, 0.3}). // 基于坐标位置点击「直播」TODO通过 OCR 识别「直播」
Validate().
AssertNameExists("直播", "「直播」不存在"),
AssertNameExists("直播"),
NewStep("向上滑动 5 次").
IOS().
SwipeUp().Times(3).ScreenShot(). // 上划 3 次,截图保存
SwipeUp().Times(2).ScreenShot(), // 再上划 2 次,截图保存
},
}
fmt.Println(testCase)
err := NewRunner(t).Run(testCase)
if err != nil {
t.Fatal(err)
}
// err := NewRunner(t).Run(testCase)
// if err != nil {
// t.Fatal(err)
// }
}
func TestIOSCameraPhotoCapture(t *testing.T) {
@ -131,11 +113,12 @@ func TestIOSCameraPhotoCapture(t *testing.T) {
IOS().Click("PhotoCapture"),
},
}
fmt.Println(testCase)
err := NewRunner(t).Run(testCase)
if err != nil {
t.Fatal(err)
}
// err := NewRunner(t).Run(testCase)
// if err != nil {
// t.Fatal(err)
// }
}
func TestIOSCameraVideoCapture(t *testing.T) {
@ -160,11 +143,12 @@ func TestIOSCameraVideoCapture(t *testing.T) {
Click("VideoCapture"), // 停止录像
},
}
fmt.Println(testCase)
err := NewRunner(t).Run(testCase)
if err != nil {
t.Fatal(err)
}
// err := NewRunner(t).Run(testCase)
// if err != nil {
// t.Fatal(err)
// }
}
func TestIOSDouyinAction(t *testing.T) {
@ -180,9 +164,10 @@ func TestIOSDouyinAction(t *testing.T) {
IOS().SwipeUp().Times(3).SwipeDown(),
},
}
fmt.Println(testCase)
err := NewRunner(t).Run(testCase)
if err != nil {
t.Fatal(err)
}
// err := NewRunner(t).Run(testCase)
// if err != nil {
// t.Fatal(err)
// }
}