diff --git a/espresso/src/main/java/com/progressive/kherkin/espresso/steps/actions/ScreenOrientationSteps.kt b/espresso/src/main/java/com/progressive/kherkin/espresso/steps/actions/ScreenOrientationSteps.kt index b8d8ccc..24875d8 100644 --- a/espresso/src/main/java/com/progressive/kherkin/espresso/steps/actions/ScreenOrientationSteps.kt +++ b/espresso/src/main/java/com/progressive/kherkin/espresso/steps/actions/ScreenOrientationSteps.kt @@ -3,12 +3,15 @@ package com.progressive.kherkin.espresso.steps.actions import androidx.test.espresso.device.DeviceInteraction.Companion.setScreenOrientation import androidx.test.espresso.device.EspressoDevice.Companion.onDevice import androidx.test.espresso.device.action.ScreenOrientation +import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation +import androidx.test.uiautomator.UiDevice import com.progressive.kherkin.common.testcore.Gherkin /** * Rotates the emulator or device to landscape orientation. * Remember to rotate back to portrait, or use a rule to start tests in the desired orientation. */ +@Deprecated("Use ISetOrientationToLandscape instead, which uses UiAutomator and is more reliable across different devices and API levels.") fun Gherkin.IRotateToLandscape() { onDevice().setScreenOrientation(ScreenOrientation.LANDSCAPE) } @@ -16,6 +19,23 @@ fun Gherkin.IRotateToLandscape() { /** * Rotates the emulator or device to portrait orientation. */ +@Deprecated("Use ISetOrientationToLandscape instead, which uses UiAutomator and is more reliable across different devices and API levels.") fun Gherkin.IRotateToPortrait() { onDevice().setScreenOrientation(ScreenOrientation.PORTRAIT) +} + + +/** + * Rotates the emulator or device to landscape orientation. + * Remember to rotate back to portrait, or use a rule to start tests in the desired orientation. + */ +fun Gherkin.ISetOrientationToLandscape() { + UiDevice.getInstance(getInstrumentation()).setOrientationLandscape() +} + +/** + * Rotates the emulator or device to portrait orientation. + */ +fun Gherkin.ISetOrientationToPortrait() { + UiDevice.getInstance(getInstrumentation()).setOrientationPortrait() } \ No newline at end of file diff --git a/sampleapp/src/androidTest/java/com/progressive/sampleapp/tests/espresso/TestScreenOrientationSteps.kt b/sampleapp/src/androidTest/java/com/progressive/sampleapp/tests/espresso/TestScreenOrientationSteps.kt index 784489e..26550a4 100644 --- a/sampleapp/src/androidTest/java/com/progressive/sampleapp/tests/espresso/TestScreenOrientationSteps.kt +++ b/sampleapp/src/androidTest/java/com/progressive/sampleapp/tests/espresso/TestScreenOrientationSteps.kt @@ -6,6 +6,8 @@ import com.progressive.kherkin.common.testcore.Then import com.progressive.kherkin.common.testcore.When import com.progressive.kherkin.espresso.steps.actions.IRotateToLandscape import com.progressive.kherkin.espresso.steps.actions.IRotateToPortrait +import com.progressive.kherkin.espresso.steps.actions.ISetOrientationToLandscape +import com.progressive.kherkin.espresso.steps.actions.ISetOrientationToPortrait import com.progressive.kherkin.espresso.steps.assertion.IShouldNotSee import com.progressive.kherkin.espresso.steps.assertion.IShouldSee import com.progressive.kherkin.espresso.steps.setup.IRenderScreen @@ -19,18 +21,18 @@ class TestScreenOrientationSteps : SampleBaseIntegrationTestCase() { @Test fun testRotateToLandscapeStep() { Given.IRenderScreen(MainScreen()) - When.IRotateToLandscape() + When.ISetOrientationToLandscape() Then.IShouldSee(R.id.buttonNav) And.IShouldNotSee(R.id.kherkin_logo) - And.IRotateToPortrait() + And.ISetOrientationToPortrait() } @Test fun testRotateToPortraitStep() { Given.IRenderScreen(MainScreen()) - And.IRotateToLandscape() + And.ISetOrientationToLandscape() And.IShouldNotSee(R.id.kherkin_logo) - When.IRotateToPortrait() + When.ISetOrientationToPortrait() Then.IShouldSee(R.id.kherkin_logo) } } \ No newline at end of file