I have decided that one of the testing criteria for my application tests with Google's Espresso is:
Test should maintain Activity state after screen orientation rotation
How do I rotate the screen when using Espresso?
I have tried the following Robotium code (Yes I placed Robotium code in my Espresso test so sue me)
solo.setActivityOrientation(solo.LANDSCAPE);
solo.setActivityOrientation(solo.PORTRAIT);
but It crashes the application when I run it within my Espresso test.
Is there any way to do this?
Thanks in advance for any help
You can't mix Robotium and Espresso tests. The best way sometimes to solve any issue is to check source code of desired but not comaptible method.
I'm pretty sure that you have already
setUp()
method, which has code like:myActivity = this.getActivity();
Use this to change your screen orientation change:
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
or
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
You may also need to use
myActivity.getInstrumentation().waitForIdleSync();
orThread.sleep(milliseconds);
in order to wait for the rotation end because it is performed in Async manner. The second methods depends on emulator/device so choose it wisely.Hope it help.
How to rotate the screen:
The
Activity
can be obtained from the ActivityRule.You can do it with uiautomator library
ui automator require min sdk version 18 so if your app has a lower min sdk you need to create a new
AndroidManifest.xml
inandroidTest
folderand then in your test
This more complete solution creates a custom Espresso
ViewAction
and works well. It shows how to get theActivity
(even when it is anAppCompatActivity
) before calling itssetRequestedOrientation()
method. It also has a clean caller interface:I follow each orientation change with a 100 ms delay, though you may not need it.
If you have the only Activity in your test case, you can do:
1. Declare you test
Rule
.2. Get you
Activity
and apply a screen rotation.That's a piece of pie!