From 939c153d370e3dcfefde3f18d672084a6bc71a7c Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 28 Aug 2022 12:04:21 +0800 Subject: [PATCH] feat: validate with label --- hrp/step.go | 1 + hrp/step_ios_ui.go | 40 ++++++++++++++++++++++++++++++++++++++++ hrp/step_ios_ui_test.go | 20 ++++++++++---------- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/hrp/step.go b/hrp/step.go index 87c48953..713b3417 100644 --- a/hrp/step.go +++ b/hrp/step.go @@ -42,6 +42,7 @@ const ( // UI validation uiSelectorName string = "ui_name" + uiSelectorLabel string = "ui_label" uiSelectorOCR string = "ui_ocr" assertionExists string = "exists" assertionNotExists string = "not_exists" diff --git a/hrp/step_ios_ui.go b/hrp/step_ios_ui.go index 6c458c6a..fb1f0a88 100644 --- a/hrp/step_ios_ui.go +++ b/hrp/step_ios_ui.go @@ -288,6 +288,36 @@ func (s *StepIOSValidation) AssertNameNotExists(expectedName string, msg ...stri return s } +func (s *StepIOSValidation) AssertLabelExists(expectedLabel string, msg ...string) *StepIOSValidation { + v := Validator{ + Check: uiSelectorLabel, + Assert: assertionExists, + Expect: expectedLabel, + } + if len(msg) > 0 { + v.Message = msg[0] + } else { + v.Message = fmt.Sprintf("[%s] not found", expectedLabel) + } + s.step.Validators = append(s.step.Validators, v) + return s +} + +func (s *StepIOSValidation) AssertLabelNotExists(expectedLabel string, msg ...string) *StepIOSValidation { + v := Validator{ + Check: uiSelectorLabel, + Assert: assertionNotExists, + Expect: expectedLabel, + } + if len(msg) > 0 { + v.Message = msg[0] + } else { + v.Message = fmt.Sprintf("[%s] should not exist", expectedLabel) + } + s.step.Validators = append(s.step.Validators, v) + return s +} + func (s *StepIOSValidation) Name() string { return s.step.Name } @@ -598,6 +628,8 @@ func (w *wdaClient) doValidation(iValidators []interface{}) (validateResults []* switch validator.Check { case uiSelectorName: result = w.assertName(expected, exists) + case uiSelectorLabel: + result = w.assertLabel(expected, exists) } if result { @@ -627,3 +659,11 @@ func (w *wdaClient) assertName(name string, exists bool) bool { _, err := w.DriverExt.FindElement(selector) return exists == (err == nil) } + +func (w *wdaClient) assertLabel(label string, exists bool) bool { + selector := gwda.BySelector{ + LinkText: gwda.NewElementAttribute().WithLabel(label), + } + _, err := w.DriverExt.FindElement(selector) + return exists == (err == nil) +} diff --git a/hrp/step_ios_ui_test.go b/hrp/step_ios_ui_test.go index 246ede90..2b6abd61 100644 --- a/hrp/step_ios_ui_test.go +++ b/hrp/step_ios_ui_test.go @@ -12,8 +12,8 @@ func TestIOSSettingsAction(t *testing.T) { NewStep("launch Settings"). IOS().Home().Tap("//*[@label='设置']"). Validate(). - AssertNameExists("飞行模式"). - AssertNameNotExists("飞行模式2"), + AssertLabelExists("飞行模式"). + AssertLabelNotExists("飞行模式2"), NewStep("swipe up and down"). IOS().SwipeUp().SwipeUp().SwipeDown(), }, @@ -33,7 +33,7 @@ func TestIOSSearchApp(t *testing.T) { NewStep("进入 App 资源库 搜索框"). IOS().Home().SwipeLeft().Times(2).Tap("dewey-search-field"). Validate(). - AssertNameExists("取消"), + AssertLabelExists("取消"), NewStep("搜索抖音"). IOS().Input("抖音\n"), }, @@ -80,13 +80,13 @@ func TestIOSWeixinLive(t *testing.T) { AppTerminate("com.tencent.xin"). // 关闭已运行的微信,确保启动微信后在「微信」首页 Tap("微信"). Validate(). - AssertNameExists("通讯录", "微信启动失败,「通讯录」不存在"), + AssertLabelExists("通讯录", "微信启动失败,「通讯录」不存在"), NewStep("进入直播页"). IOS(). Tap("发现").Sleep(5). // 进入「发现页」;等待 5 秒确保加载完成 TapXY(0.5, 0.3). // 基于坐标位置点击「直播」;TODO:通过 OCR 识别「直播」 Validate(). - AssertNameExists("直播"), + AssertLabelExists("直播"), NewStep("向上滑动 5 次"). IOS(). SwipeUp().Times(3).ScreenShot(). // 上划 3 次,截图保存 @@ -110,7 +110,7 @@ func TestIOSCameraPhotoCapture(t *testing.T) { StopCamera(). StartCamera(). Validate(). - AssertNameExists("PhotoCapture", "拍照按钮不存在"), + AssertLabelExists("PhotoCapture", "拍照按钮不存在"), NewStep("start recording"). IOS().Tap("PhotoCapture"), }, @@ -132,12 +132,12 @@ func TestIOSCameraVideoCapture(t *testing.T) { StopCamera(). StartCamera(). Validate(). - AssertNameExists("PhotoCapture", "录像按钮不存在"), + AssertLabelExists("PhotoCapture", "录像按钮不存在"), NewStep("switch to video capture"). IOS(). SwipeRight(). Validate(). - AssertNameExists("VideoCapture", "拍摄按钮不存在"), + AssertLabelExists("VideoCapture", "拍摄按钮不存在"), NewStep("start recording"). IOS(). Tap("VideoCapture"). // 开始录像 @@ -160,8 +160,8 @@ func TestIOSDouyinAction(t *testing.T) { NewStep("launch douyin"). IOS().Home().Tap("//*[@label='抖音']"). Validate(). - AssertNameExists("首页", "首页 tab 不存在"). - AssertNameExists("消息", "消息 tab 不存在"), + AssertLabelExists("首页", "首页 tab 不存在"). + AssertLabelExists("消息", "消息 tab 不存在"), NewStep("swipe up and down"). IOS().SwipeUp().Times(3).SwipeDown(), },