From 0407c0239129a9a84ca935fc436aae866b6d5e1e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Mon, 15 Aug 2022 21:53:33 +0800 Subject: [PATCH] change: make assert msg optional --- hrp/step.go | 6 ++ hrp/step_android_ui.go | 31 +++++-- hrp/step_android_ui_test.go | 28 ++++++ hrp/step_ios_ui.go | 64 +++++++++----- hrp/{step_ui_test.go => step_ios_ui_test.go} | 93 ++++++++------------ 5 files changed, 139 insertions(+), 83 deletions(-) create mode 100644 hrp/step_android_ui_test.go rename hrp/{step_ui_test.go => step_ios_ui_test.go} (72%) diff --git a/hrp/step.go b/hrp/step.go index c05a3166..dcc2c7ff 100644 --- a/hrp/step.go +++ b/hrp/step.go @@ -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 { diff --git a/hrp/step_android_ui.go b/hrp/step_android_ui.go index 322dfb7b..60b9cc3a 100644 --- a/hrp/step_android_ui.go +++ b/hrp/step_android_ui.go @@ -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 diff --git a/hrp/step_android_ui_test.go b/hrp/step_android_ui_test.go new file mode 100644 index 00000000..13d3cba9 --- /dev/null +++ b/hrp/step_android_ui_test.go @@ -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) + // } +} diff --git a/hrp/step_ios_ui.go b/hrp/step_ios_ui.go index 1db74798..3e7b3adc 100644 --- a/hrp/step_ios_ui.go +++ b/hrp/step_ios_ui.go @@ -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 { diff --git a/hrp/step_ui_test.go b/hrp/step_ios_ui_test.go similarity index 72% rename from hrp/step_ui_test.go rename to hrp/step_ios_ui_test.go index 1ec259ae..7a836cdd 100644 --- a/hrp/step_ui_test.go +++ b/hrp/step_ios_ui_test.go @@ -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) + // } }