How to rotate android application screen portrait to landscape and landscape to portrait in Appium using java?

Android application testing, sometimes we need to check everything display and working fine in portrait and landscape orientation. In this tutorial, I am going to show how to rotate screen orientation from portrait to landscape and landscape to portrait using rotate() method of appium.

We will use getOrientation() method to get current screen orientation.
We will use rotate() method to change orientation from landscape to portrait and portrait to landscape.
driver.rotate(ScreenOrientation.PORTRAIT) will rotate screen orientation from landscape to portrait.

driver.rotate(ScreenOrientation.LANDSCAPE) will rotate screen orientation from portrait to landscape.
Download Drag-Sort Demos app from Google Play Store to perform screen orientation operation in android device and install device or Genymotion emulator.
Please visit below URL to download apk file from Google play Store

 import io.appium.java_client.android.AndroidDriver;  
 import java.io.File;  
 import java.io.InterruptedIOException;  
 import java.net.MalformedURLException;  
 import java.net.URL;  
 import java.util.concurrent.TimeUnit;  
   
 import org.openqa.selenium.By;  
 import org.openqa.selenium.ScreenOrientation;  
 import org.openqa.selenium.remote.CapabilityType;  
 import org.openqa.selenium.remote.DesiredCapabilities;  
   
   
   
 public class AppiumScreenOrientation {  
   
   private static AndroidDriver driver;  
   
   public static void main(String[] arg) throws MalformedURLException, InterruptedException {  
   
     // Created object of DesiredCapabilities class.  
     DesiredCapabilities capabilities = new DesiredCapabilities();  
   
     // Set android deviceName desired capability. Set genymotion emulator name.  
     capabilities.setCapability("deviceName", "192.168.56.101:5555");  
   
     // Set android platformName desired capability. It's Android in our case here.  
     capabilities.setCapability("platformName", "Android");  
   
     // Set android VERSION desired capability. Set genymotion emulator OS version.  
     capabilities.setCapability(CapabilityType.VERSION, "6.0.0");  
   
     // Set BROWSER_NAME desired capability. It's Android in our case here.  
     capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");  
   
     // Java package of the tested Android app  
     capabilities.setCapability("appPackage", "com.mobeta.android.demodslv");  
   
     // An activity name for the Android activity you want to run from your package.  
     capabilities.setCapability("appActivity", "com.mobeta.android.demodslv.Launcher");  
   
     driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);  
   
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  
     driver.findElementById("com.mobeta.android.demodslv:id/activity_title").click();  
   
     // perform Screen Orientation  
   
     //Get and print current screen orientation.  
     System.out.println("Current screen orientation is : " + driver.getOrientation());  
   
     //Changing screen Orientation to LANDSCAPE.  
     driver.rotate(ScreenOrientation.PORTRAIT);  
   
     //Get and print screen orientation after changing It.  
     System.out.println("Now screen orientation is : "+ driver.getOrientation());  
     Thread.sleep(2000);  
   
     //Changing screen Orientation to PORTRAIT.  
     driver.rotate(ScreenOrientation.LANDSCAPE);  
     //Get and print screen orientation after changing It.  
     System.out.println("Now screen orientation is : "+ driver.getOrientation());  
     Thread.sleep(5000);  
   
     //Changing screen Orientation to LANDSCAPE.  
     driver.rotate(ScreenOrientation.PORTRAIT);  
     Thread.sleep(2000);  
   
     // Close application  
     driver.quit();  
   
   }  
 }  

2 comments:

  1. Benefits of reading your post. this may facilitate within the maintenance of our health.The daily routine can assist you lose weight quickly and safely.My life is completely reworked once I followed this diet.I feeling nice concerning myself.

    Herbalife in Chennai
    Wellnesscoaches in Chennai
    Weight Loss in Chennai
    Weight Gain in Chennai

    ReplyDelete