Versions used while doing this blog post,
Appium server version : 1.20.2
Appium JAVA client version : 7.5.1
As we know, Appium is capable of automating Web, native and hybrid apps in android. In this post, lets see how we can invoke an app in android and read its logs. The APK file of the app is in executors machine.
Before getting into actual coding, lets get the steps to be followed to make any appium program functional:
1. We need the latest appium java client and latest stable selenium to be referred into the project library
2. The first step of the program should be setting up of desired capabilities. Here we need to provide the details of the android device with which we are gonna interact and app that is to be installed & invoked.
The mandatory parameters here are:
a. DEVICE_NAME
b. APP [As, we have the apk/app file in PC from which we execute the program]
3. Next inline should be the creation of android/iOS driver by passing appium server URL and device capabilities
4. The current screen orientation can be read using driver.getOrientation() method
System.out.println("Current orientation -> " + driver.getOrientation().toString());
5. Screen can be rotated by passing the type of orientation as parameter in driver.rotate() method
driver.rotate(ScreenOrientation.LANDSCAPE);
6. Once done with execution of expected functionality, the session should be closed/quit
Complete code:
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
public class GetAndSetOrientationFunctionality {
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "*****");
if (System.getProperty("os.name").toLowerCase().startsWith("mac"))
capabilities.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir")+"/src/main/resources/ApiDemos-debug.apk");
else
capabilities.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir")+"\\src\\main\\resources\\ApiDemos-debug.apk");
URL url = new URL("http://0.0.0.0:4723/wd/hub");
AndroidDriver driver = new AndroidDriver(url, capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(driver.findElementByAccessibilityId("Views")));
System.out.println("Current orientation -> " + driver.getOrientation().toString());
driver.findElementByAccessibilityId("Views").click();
wait.until(ExpectedConditions.visibilityOf(driver.findElementByAccessibilityId("Chronometer")));
driver.rotate(ScreenOrientation.LANDSCAPE);
Thread.sleep(5000);
System.out.println("Current orientation after change -> " + driver.getOrientation().toString());
driver.closeApp();
driver.quit();
}
}