From 2cd10989f0490805dd067fc52d1f5b39bb673fac Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 28 Aug 2022 12:18:45 +0800 Subject: [PATCH] feat: validate with ocr text --- hrp/step_ios_ui.go | 37 +++++++++++++++++++++++++++++++++++++ hrp/step_ios_ui_test.go | 4 +++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/hrp/step_ios_ui.go b/hrp/step_ios_ui.go index fb1f0a88..beebd782 100644 --- a/hrp/step_ios_ui.go +++ b/hrp/step_ios_ui.go @@ -318,6 +318,36 @@ func (s *StepIOSValidation) AssertLabelNotExists(expectedLabel string, msg ...st return s } +func (s *StepIOSValidation) AssertOCRExists(expectedText string, msg ...string) *StepIOSValidation { + v := Validator{ + Check: uiSelectorOCR, + Assert: assertionExists, + Expect: expectedText, + } + if len(msg) > 0 { + v.Message = msg[0] + } else { + v.Message = fmt.Sprintf("[%s] not found", expectedText) + } + s.step.Validators = append(s.step.Validators, v) + return s +} + +func (s *StepIOSValidation) AssertOCRNotExists(expectedText string, msg ...string) *StepIOSValidation { + v := Validator{ + Check: uiSelectorOCR, + Assert: assertionNotExists, + Expect: expectedText, + } + if len(msg) > 0 { + v.Message = msg[0] + } else { + v.Message = fmt.Sprintf("[%s] should not exist", expectedText) + } + s.step.Validators = append(s.step.Validators, v) + return s +} + func (s *StepIOSValidation) Name() string { return s.step.Name } @@ -630,6 +660,8 @@ func (w *wdaClient) doValidation(iValidators []interface{}) (validateResults []* result = w.assertName(expected, exists) case uiSelectorLabel: result = w.assertLabel(expected, exists) + case uiSelectorOCR: + result = w.assertOCR(expected, exists) } if result { @@ -667,3 +699,8 @@ func (w *wdaClient) assertLabel(label string, exists bool) bool { _, err := w.DriverExt.FindElement(selector) return exists == (err == nil) } + +func (w *wdaClient) assertOCR(text string, exists bool) bool { + _, _, _, _, err := w.DriverExt.FindTextByOCR(text) + return exists == (err == nil) +} diff --git a/hrp/step_ios_ui_test.go b/hrp/step_ios_ui_test.go index 2b6abd61..6711fed6 100644 --- a/hrp/step_ios_ui_test.go +++ b/hrp/step_ios_ui_test.go @@ -10,9 +10,11 @@ func TestIOSSettingsAction(t *testing.T) { Config: NewConfig("ios ui action on Settings"), TestSteps: []IStep{ NewStep("launch Settings"). - IOS().Home().Tap("//*[@label='设置']"). + IOS().Home().Tap("设置"). Validate(). + AssertNameExists("飞行模式"). AssertLabelExists("飞行模式"). + AssertOCRExists("飞行模式"). AssertLabelNotExists("飞行模式2"), NewStep("swipe up and down"). IOS().SwipeUp().SwipeUp().SwipeDown(),