Get your setup working faster. Join our Discord for optimisation tips from elite testers.Join our Discord
Debug app orientation issues
Learn about possible solutions to resolve Android app orientation issues on BrowserStack App Automate.
On some Android devices, an app can stay in landscape mode or switch orientation unexpectedly during a test. This page explains why that happens and how to fix it.
Issue
When you run tests on BrowserStack App Automate, you may encounter the following Android app orientation issues :
App remains in landscape mode irrespective of the orientation set
App randomly switches to landscape mode in-between a test
Causes
Android apps that use device sensors to determine orientation may move to landscape mode irrespective of the orientation set. This happens because BrowserStack mobile devices are kept in landscape position as a default and are programmatically set to portrait mode. Therefore, if an Android app uses device sensor data to determine orientation it may automatically switch to landscape mode.
In Android tests that use Appium commands activate_app or background_app, Appium may set the device orientation to auto-rotate and as a result, the app might switch to landscape mode in-between the tests.
Resolutions
Ensure your Android app does not use device sensor to determine app orientation. If your app determines screenOrientation using device sensor values, ensure that it is set to non-sensor value such as unspecified, user or fullUser. For more details, check Android screenOrientation documentation.
On Android version 10 and above, you can also make use of the following BrowserStack executors to make the app adhere to user-set orientation rather than orientation set by device sensor values.
JavascriptExecutorjse=(JavascriptExecutor)driver;// enable user-set orientation on Android version >=12 devicesjse.executeScript("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm fixed-to-user-rotation enabled\" }}");// disable user-set orientation on Android version >=12 devicesjse.executeScript("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm fixed-to-user-rotation disabled\" }}");// enable user-set orientation on Android version 10,11 devicesjse.executeScript("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm set-fix-to-user-rotation enabled\" }}");// disable user-set orientation on Android version 10,11 devicesjse.executeScript("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm set-fix-to-user-rotation disabled\" }}");
// enable user-set orientation on Android version >=12 devicesawaitdriver.execute("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm fixed-to-user-rotation enabled\" }}");// disable user-set orientation on Android version >=12 devicesawaitdriver.execute("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm fixed-to-user-rotation disabled\" }}");// enable user-set orientation on Android version 10,11 devicesawaitdriver.execute("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm set-fix-to-user-rotation enabled\" }}");// disable user-set orientation on Android version 10,11 devicesawaitdriver.execute("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm set-fix-to-user-rotation disabled\" }}");
// enable user-set orientation on Android version >=12 devices((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm fixed-to-user-rotation enabled\" }}");// disable user-set orientation on Android version >=12 devices((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm fixed-to-user-rotation disabled\" }}");// enable user-set orientation on Android version 10,11 devices((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm set-fix-to-user-rotation enabled\" }}");// disable user-set orientation on Android version 10,11 devices((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm set-fix-to-user-rotation disabled\"}}");
# enable user-set orientation on Android version >=12 devices
driver.execute_script("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm fixed-to-user-rotation enabled\" }}");# disable user-set orientation on Android version >=12 devices
driver.execute_script("browserstack_executor: {\"action\": \"adbShell\", \"arguments\":{\"command\" : \"wm fixed-to-user-rotation disabled\" }}");# enable user-set orientation on Android version 10,11 devices
driver.execute_script("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm set-fix-to-user-rotation enabled\" }}");# disable user-set orientation on Android version 10,11 devices
driver.execute_script("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm set-fix-to-user-rotation disabled\" }}");
caps["javascriptEnabled"]="true"#include this capability for JavaScript Executors to work# enable user-set orientation on Android version >=12 devicesdriver.execute_script("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm fixed-to-user-rotation enabled\" }}");# disable user-set orientation on Android version >=12 devicesdriver.execute_script("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm fixed-to-user-rotation disabled\" }}");# enable user-set orientation on Android version 10,11 devicesdriver.execute_script("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm set-fix-to-user-rotation enabled\" }}");# disable user-set orientation on Android version 10,11 devicesdriver.execute_script("browserstack_executor: {\"action\": \"adbShell\", \"arguments\": {\"command\" : \"wm set-fix-to-user-rotation disabled\" }}");
If your Android test uses commands such as activate_app or background_app and your app is moving to landscape mode in-between the tests, explicitly set the orientation of the device again using Appium setOrientation command after these Appium commands are invoked.
// Put the app in backgrounddriver.runAppInBackground();//...// Put the app in foreground.driver.activateApp(<app_package>);// Set the orientation of the device.driver.rotate(ScreenOrientation.PORTRAIT);
// Put the app in backgrounddriver.background();//...// Set the orientation of the device/* (Note: Earlier versions of Appium used:
*
* driver.setOrientation('PORTRAIT')
*
* which has been deprecated) */driver.rotate(ScreenOrientation.PORTRAIT)
// Put the app in backgrounddriver.BackgroundApp()//...// Put the app in foreground.driver.activateApp(<app_package>);// Set the orientation of the device.driver.Orientation=ScreenOrientation.Portrait;
# Put the app in background
driver.background_app()#...
# Put the app in foreground
driver.activate_app(<package_name>)# Set the orientation of the device.
driver.orientation="PORTRAIT"
#Put the app in background.@driver.background_app()#...#Put the app in foreground.@driver.activate_app(<package_name>)# Set the orientation of the device.@driver.rotation=:portrait
Need help?
If you need additional help, contact our Support team.
Did this page help you?
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked
Thank you for your valuable feedback
Is this page helping you?
Yes
No
We're sorry to hear that. Please share your feedback so we can do better
Contact our Support team for immediate help while we work on improving our docs.
We're continuously improving our docs. We'd love to know what you liked