Appium Concepts: Screen Orientation in Appium: Switching Seamlessly Between Landscape & Portrait
Command — “.rotate(ScreenOrientation.LANDSCAPE/PORTRAIT)”

Check Out the Video Below for Complete Details!
Command — .rotate(ScreenOrientation.LANDSCAPE/PORTRAIT)
Introduction:
InAppium, you can use the .rotate()
method to change the screen orientation of a mobile device during automation testing
Appium Java client provides a method — “.rotate(ScreenOrientation.LANDSCAPE/PORTRAIT)”.
To Fetch/Get current mobile orientation:
driver.getOrientation();
Approach 1:
By using “.rotate” method during the test execution
to change Screen Orientation to “LANDSCAPE”
driver.rotate(ScreenOrientation.LANDSCAPE);
to change Screen Orientation to “PORTRAIT”
driver.rotate(ScreenOrientation.PORTRAIT);
Approach 2 :
By adding the capability to change ‘screen orientation’ before staring the test execution.
to change Screen Orientation to “PORTRAIT”
cap.setOrientation(ScreenOrientation.PORTRAIT);
Or
cap.setCapability("orientation", "PORTRAIT");
to change Screen Orientation to “LANDSCAPE”
cap.setOrientation(ScreenOrientation.LANDSCAPE);
Or
cap.setCapability("orientation", "LANDSCAPE");
“Complete Code: Optimized and Ready to Use”:
package com.appiumguide.appiumconcepts;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import org.openqa.selenium.ScreenOrientation;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
/**
* Script Details - Appium Concepts : "ScreenOrientation-Landscape/Portrait"
*
* appium-java-client version: 9.3.0
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/
public class OrientationTest {
private AndroidDriver driver;
ScreenOrientation currentorientation;
@BeforeTest
public void steup() throws MalformedURLException {
UiAutomator2Options cap =new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setDeviceName("Pixal7a");
cap.setAppPackage("com.google.android.deskclock");
cap.setAppActivity("com.android.deskclock.DeskClock");
//cap.setOrientation(currentorientation.LANDSCAPE);
driver=new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
@AfterTest
public void teardown() {
if(driver!=null) {
driver.quit();
System.out.println("Test Execution Completed");
}
}
@Test
public void orientationexampl() throws InterruptedException {
currentorientation=driver.getOrientation();
System.out.println("Current Orientation - "+currentorientation);
Thread.sleep(5000);
driver.rotate(ScreenOrientation.LANDSCAPE);
Thread.sleep(5000);
driver.rotate(ScreenOrientation.PORTRAIT);
}
}
Explore on GitHub:
No comments:
Post a Comment