Screen Orientation In Appium Android App Automation Testing

You occasionally needs to rotate screen orientation from portrait to landscape and landscape to portrait during android software application testing to check everything display and working fine In both orientation. Same thing you have to verify during appium android software test automation. For screen orientation of android software app screen during appium test automation, We can use rotate() method of appium. Let's look at simple example of how to perform screen orientation In appium android app software test automation.

App To Use And Aim To Achieve
We are going to use API Demos app(download from HERE) In android screen orientation test. Our aim Is to get current screen orientation and switch android mobile screen orientation from landscape to portrait and portrait to landscape.
  • We will use getOrientation() method of appium to get current screen orientation.
  • We will use rotate() method of appium to change orientation from landscape to portrait and portrait to landscape.
  • driver.rotate(org.openqa.selenium.ScreenOrientation.LANDSCAPE) will rotate screen orientation from portrait to landscape.
  • driver.rotate(org.openqa.selenium.ScreenOrientation.PORTRAIT) will rotate screen orientation from landscape to portrait.
Let's Implement It practically.

Create And Run Test
Create new ScreenOrientation.java file Under Android package of your package In place bellow given software test code In It.

Note : In this test, We have Implemented logic to start and stop appium server programmatically In appiumStart() and appiumStop() methods. So It will start automatically and you do not need to start and stop appium server manually. If appiumStart() and appiumStop() methods not works for you then remove It and start appium server manually.

ScreenOrientation.java
package Android;

import io.appium.java_client.android.AndroidDriver;

import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ScreenOrientation {
 static AndroidDriver driver;
 // Set path of your node.exe file. Set your path.
 // Progra~1 represents Program Files folder.
 String nodePath = "C:/Progra~1/Appium/node.exe";
 // Set path of your appium.js file. Set your path.
 String appiumJSPath = "C:/Progra~1/Appium/node_modules/appium/bin/appium.js";

 // This method Is responsible for starting appium server.
 public void appiumStart() throws IOException, InterruptedException {
  // Created object of apache CommandLine class.
  // It will start command prompt In background.
  CommandLine command = new CommandLine("cmd");
  // Add different arguments In command line which requires to start appium server.
  command.addArgument("/c");
  command.addArgument(nodePath);
  command.addArgument(appiumJSPath);
  // Set Server address
  command.addArgument("--address");
  command.addArgument("127.0.0.1");
  // Set Port
  command.addArgument("--port");
  command.addArgument("4723");
  command.addArgument("--no-reset");
  command.addArgument("--log");
  // Set path to store appium server log file.
  command.addArgument("D://appiumLogs.txt");
  // Execute command line arguments to start appium server.
  DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
  DefaultExecutor executor = new DefaultExecutor();
  executor.setExitValue(1);
  executor.execute(command, resultHandler);
  // Wait for 15 minutes so that appium server can start properly before going for test execution.
  // Increase this time If face any error.
  Thread.sleep(15000);
 }

 // This method Is responsible for stopping appium server.
 public static void appiumStop() throws IOException {
  // Add different arguments In command line which requires to stop appium server.
  CommandLine command = new CommandLine("cmd");
  command.addArgument("/c");
  command.addArgument("taskkill");
  command.addArgument("/F");
  command.addArgument("/IM");
  command.addArgument("node.exe");
  // Execute command line arguments to stop appium server.
  DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
  DefaultExecutor executor = new DefaultExecutor();
  executor.setExitValue(1);
  executor.execute(command, resultHandler);
 }

 @BeforeTest
 public void setUp() throws Exception {
  // Start appium server.
  appiumStart();
  DesiredCapabilities capabilities = new DesiredCapabilities();
  capabilities.setCapability("deviceName", "ZX1B32FFXF");
  capabilities.setCapability("browserName", "Android");
  capabilities.setCapability("platformVersion", "4.4.2");
  capabilities.setCapability("platformName", "Android");
  capabilities.setCapability("appPackage", "io.appium.android.apis");
  capabilities.setCapability("appActivity", "io.appium.android.apis.ApiDemos");
  driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
  driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
 }

 @Test
 public void performOrientation() throws InterruptedException {
  //Get and print current screen orientation.
  System.out.println("*--*--*-- Current screen orientation Is : " + driver.getOrientation());
  System.out.println("*--*--*-- Changing screen Orientation to LANDSCAPE.");
  //Changing screen Orientation to LANDSCAPE.
  driver.rotate(org.openqa.selenium.ScreenOrientation.LANDSCAPE);
  //Get and print screen orientation after changing It.
  System.out.println("*--*--*-- Now screen orientation Is : "+ driver.getOrientation());
  Thread.sleep(5000);
  // Scroll till element which contains "Views" text If It Is not visible on screen.
  driver.scrollTo("Views");
  // Click on Views.
  driver.findElement(By.name("Views")).click();  
  System.out.println("*--*--*-- Changing screen Orientation to PORTRAIT.");
  //Changing screen Orientation to PORTRAIT.
  driver.rotate(org.openqa.selenium.ScreenOrientation.PORTRAIT);
  //Get and print screen orientation after changing It.
  System.out.println("*--*--*-- Now screen orientation Is : "+ driver.getOrientation());
  Thread.sleep(5000);
 }

 @AfterTest
 public void End() throws IOException {
  driver.quit();
  // Stop appium server when test Is ended.
  appiumStop();
 }
}

Test Description
In above test, Main thing you need to understand Is how to get current screen orientation and how to change It from landscape to portrait and portrait to landscape. Everything Is described within script so no need more declaration here.

This Is the way to change screen orientation of android mobile screen when you run android software automation test using appium.

2 comments:

  1. I have trying following code but it displays error message:

    driver.rotate(org.openqa.selenium.ScreenOrientation.LANDSCAPE);
    Thread.sleep(5000);
    driver.rotate(org.openqa.selenium.ScreenOrientation.PORTRAIT);
    Thread.sleep(5000);

    Error message:-
    An unknown server-side error occurred while processing the command.
    (WARNING: The server did not provide any stacktrace information)

    ReplyDelete
  2. I am also getting same error on mac
    Please do suggest

    ReplyDelete