From 4847fbd0f3fd5e7dfadb236053e9aa1244cc8ee8 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 31 Jul 2022 15:29:29 +0800 Subject: [PATCH] feat: start & stop camera --- hrp/step.go | 22 ++++++++++--------- hrp/step_android_ui.go | 4 ++-- hrp/step_ios_ui.go | 31 +++++++++++++++++++++++++- hrp/step_ui_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 13 deletions(-) diff --git a/hrp/step.go b/hrp/step.go index 721e70a8..c05a3166 100644 --- a/hrp/step.go +++ b/hrp/step.go @@ -24,18 +24,20 @@ const ( appLaunchUnattached MobileMethod = "app_launch_unattached" // 只负责通知打开 app,不堵塞等待,不可传入启动参数 appTerminate MobileMethod = "app_terminate" appStop MobileMethod = "app_stop" - cameraStart MobileMethod = "camera_start" - cameraStop MobileMethod = "camera_stop" - recordStart MobileMethod = "record_start" - recordStop MobileMethod = "record_stop" - uiHome MobileMethod = "home" - uiClick MobileMethod = "click" - uiDoubleClick MobileMethod = "double_click" - uiLongClick MobileMethod = "long_click" - uiSwipe MobileMethod = "swipe" - uiInput MobileMethod = "input" ctlScreenShot MobileMethod = "screenshot" ctlSleep MobileMethod = "sleep" + ctlStartCamera MobileMethod = "camera_start" // alias for app_launch camera + ctlStopCamera MobileMethod = "camera_stop" // alias for app_terminate camera + recordStart MobileMethod = "record_start" + recordStop MobileMethod = "record_stop" + + // UI handling + uiHome MobileMethod = "home" + uiClick MobileMethod = "click" + uiDoubleClick MobileMethod = "double_click" + uiLongClick MobileMethod = "long_click" + uiSwipe MobileMethod = "swipe" + uiInput MobileMethod = "input" ) type MobileAction struct { diff --git a/hrp/step_android_ui.go b/hrp/step_android_ui.go index 8ea3db2c..322dfb7b 100644 --- a/hrp/step_android_ui.go +++ b/hrp/step_android_ui.go @@ -34,7 +34,7 @@ func (s *StepAndroid) StartAppByIntent(activity string) *StepAndroid { func (s *StepAndroid) StartCamera() *StepAndroid { s.step.Android.Actions = append(s.step.Android.Actions, MobileAction{ - Method: cameraStart, + Method: ctlStartCamera, Params: nil, }) return &StepAndroid{step: s.step} @@ -42,7 +42,7 @@ func (s *StepAndroid) StartCamera() *StepAndroid { func (s *StepAndroid) StopCamera() *StepAndroid { s.step.Android.Actions = append(s.step.Android.Actions, MobileAction{ - Method: cameraStop, + Method: ctlStopCamera, Params: nil, }) return &StepAndroid{step: s.step} diff --git a/hrp/step_ios_ui.go b/hrp/step_ios_ui.go index 09fe1ac6..1db74798 100644 --- a/hrp/step_ios_ui.go +++ b/hrp/step_ios_ui.go @@ -198,6 +198,22 @@ func (s *StepIOS) ScreenShot() *StepIOS { return &StepIOS{step: s.step} } +func (s *StepIOS) StartCamera() *StepIOS { + s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{ + Method: ctlStartCamera, + Params: nil, + }) + return &StepIOS{step: s.step} +} + +func (s *StepIOS) StopCamera() *StepIOS { + s.step.IOS.Actions = append(s.step.IOS.Actions, MobileAction{ + Method: ctlStopCamera, + Params: nil, + }) + return &StepIOS{step: s.step} +} + // Validate switches to step validation. func (s *StepIOS) Validate() *StepIOSValidation { return &StepIOSValidation{ @@ -612,7 +628,20 @@ func (w *wdaClient) doAction(action MobileAction) error { case ctlScreenShot: // take snapshot log.Info().Msg("take snapshot for current screen") - w.screenShot() + return w.screenShot() + case ctlStartCamera: + // start camera, alias for app_launch com.apple.camera + return w.Driver.AppLaunch("com.apple.camera") + case ctlStopCamera: + // stop camera, alias for app_terminate com.apple.camera + success, err := w.Driver.AppTerminate("com.apple.camera") + if err != nil { + return errors.Wrap(err, "failed to terminate camera") + } + if !success { + log.Warn().Msg("camera was not running") + } + return nil } return nil } diff --git a/hrp/step_ui_test.go b/hrp/step_ui_test.go index f888b55a..1ec259ae 100644 --- a/hrp/step_ui_test.go +++ b/hrp/step_ui_test.go @@ -117,6 +117,56 @@ func TestIOSWeixinLive(t *testing.T) { } } +func TestIOSCameraPhotoCapture(t *testing.T) { + testCase := &TestCase{ + Config: NewConfig("ios camera photo capture"), + TestSteps: []IStep{ + NewStep("launch camera"). + IOS().Home(). + StopCamera(). + StartCamera(). + Validate(). + AssertNameExists("PhotoCapture", "拍照按钮不存在"), + NewStep("start recording"). + IOS().Click("PhotoCapture"), + }, + } + + err := NewRunner(t).Run(testCase) + if err != nil { + t.Fatal(err) + } +} + +func TestIOSCameraVideoCapture(t *testing.T) { + testCase := &TestCase{ + Config: NewConfig("ios camera video capture"), + TestSteps: []IStep{ + NewStep("launch camera"). + IOS().Home(). + StopCamera(). + StartCamera(). + Validate(). + AssertNameExists("PhotoCapture", "录像按钮不存在"), + NewStep("switch to video capture"). + IOS(). + SwipeRight(). + Validate(). + AssertNameExists("VideoCapture", "拍摄按钮不存在"), + NewStep("start recording"). + IOS(). + Click("VideoCapture"). // 开始录像 + Sleep(5). + Click("VideoCapture"), // 停止录像 + }, + } + + err := NewRunner(t).Run(testCase) + if err != nil { + t.Fatal(err) + } +} + func TestIOSDouyinAction(t *testing.T) { testCase := &TestCase{ Config: NewConfig("ios ui action on 抖音"),