Switch to landscape / portrait mode

Hi @Chris_Trevarthen,

Is it possible to switch to landscape mode for 10 seconds and after the 10 seconds ends I come back to the portrait mode

If it’s possible to do this with the manual mode offered by Katalon Studio because I’m not a developer and I do not know how to use the script mode

I use Katalon Studio 6.1.2
appium 1.8.1
local device Samsung S7

Thank you very much in advance :slight_smile:

Hi @walpokbymon,

You should definitely be able to switch between portrait and landscape mode. I think you’ll have to do this in the script mode, but the code isn’t too complicated. You can copy and paste this code into your test case script where you want the switching to happen:

'Switch the current device to landscape mode'
Mobile.switchToLandscape()

'Pause for 10 seconds'
Mobile.delay(10)

'Switch the current device to portrait mode'
Mobile.switchToPortrait()

Please let me know if you need any more info on how to do this.

– Chris

Hi @Chris_Trevarthen ,

I did as you told me but unfortunately it did not work.

I explain my scenario a bit:

I need to film a video, here is my script:

‘press the button that allows you to start capturing’
Mobile.tap (findTestObject (‘android.widget.ImageView2 (10)’), 0)

Mobile.delay (10, FailureHandling.STOP_ON_FAILURE)

‘press the same button to stop capturing’
Mobile.tap (findTestObject (‘android.widget.ImageView2 (9)’), 0)

the problem is, i need to film a video landscape and the current script does not allow me to do that. it is necessary that a human is precent for return the telephone an landscape mode during the capture

Note that on the app there is a feature that allows when capturing the video, to check if the phone in landscape or not, if it is not it displays a toast

Thank you very much in advance

Hi @walpokbymon,

If you try inserting the switching code into your test, does it do anything to switch the orientation?

'press the button that allows you to start capturing'
Mobile.tap (findTestObject (‘android.widget.ImageView2 (10)’), 0)

'Switch the current device to landscape mode'
Mobile.switchToLandscape()

'Pause for 10 seconds'
Mobile.delay(10)

'Switch the current device to portrait mode'
Mobile.switchToPortrait()

'press the same button to stop capturing'
Mobile.tap (findTestObject (‘android.widget.ImageView2 (9)’), 0)

– Chris

Hi @Chris_Trevarthen,

I tried the same script that you sent me but unfortunately it does not work, the application remains in portrait mode throughout the execution of the script

Here is my script:

import static com.kms.katalon.core.checkpoint.CheckpointFactory. findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory. findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory. findTestData
import static com.kms.katalon.core.testobject.ObjectRepository. findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory as MobileDriverFactory
import io.appium.java_client.AppiumDriver as AppiumDriver
import com.kms.katalon.core.util.KeywordUtil as KeywordUtil

Mobile. startApplication (’#######.apk’, true )

Mobile. tap ( findTestObject (‘android.widget.TextView0 - Project Name’), 0)

Mobile. setText ( findTestObject (‘android.widget.EditText0 - Project Name’), ‘R 20’, 0)

Mobile. tap ( findTestObject (‘android.widget.LinearLayout7 (2)’), 0)

Mobile. tap ( findTestObject (‘android.widget.ImageView0 (4)’), 0)

Mobile. switchToLandscape ()

Mobile. tap ( findTestObject (‘android.widget.ImageView2 (10)’), 0)

Mobile. delay (10, FailureHandling. STOP_ON_FAILURE )

Mobile. tap ( findTestObject (‘android.widget.ImageView2 (9)’), 0)

Mobile. switchToPortrait ()

‘Valider’

Mobile. tap ( findTestObject (‘android.widget.ImageView9 (7)’), 0)

Mobile. delay (3, FailureHandling. STOP_ON_FAILURE )

Mobile. closeApplication ()

Note that when I launch the script I hold the Android device with my hand in portrait mode to view the progress of the test.

I’m waiting for script that the application gets into landscape mode just before starting to film and that it turns in portrait mode when it stops filming.
thanks in advance

Hi @walpokbymon,

Does switching to landscape mode work if you lay the device down flat? I’ve tried with an emulator and it does switch, so I’m wondering if it’s because you’re holding it in your hand that it won’t actually switch to landscape.

– Chris

Hi @Chris_Trevarthen,

If I put the Android device on a flat surface, the application appears by default in landscape, so with the script above I have a problem to return the application to portrait mode

On the net I found this link http://easybix.com/rotate-mobile-screen-using-appium/ , they used the rotate () method, I do not know how to use this script because as you know I’m not a developer.

Do you think that can be a solution? if it’s so can you please tell me how to use this script in my Katalon project?
If you have any other solution it makes me hugely happy :slight_smile:

Thank you in advance

Hi @walpokbymon

To use the code you sent, you can do the following:

At the top of your test file, add the following import statements:

import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory
import io.appium.java_client.AppiumDriver as AppiumDriver

At the place you want to switch to landscape, use the following code:

AppiumDriver<?> driver = MobileDriverFactory.getDriver()
driver.rotate(org.openqa.selenium.ScreenOrientation.LANDSCAPE);

Then when you want to switch back to portrait, use the following code:

driver.rotate(org.openqa.selenium.ScreenOrientation.PORTRAIT);

Hope this helps,

Chris

Hi @Chris_Trevarthen,

Unfortunately it’s still the same problem, :frowning:

Do not you think that launching the camera has an effect on the change between landscape mode and portrait mode?
If so, is there a solution where we force the change of the orientation of the phone even if the camera is launched ?
So is there a solution where the script does not take into consideration how I hold the phone by my hand ?

Thank you in advance :slight_smile:

Hi @walpokbymon,

I think you might be running into limitations of running a test on a real device where the device detects that it is really in portrait mode and is trying to show it that way. You might be able to change the phone settings to lock the orientation of the phone so it doesn’t auto-rotate. I’m not sure if that will allow rotation through your test script, but it’s worth a try.

– Chris

Please use below tutorial