(Appium-Adv-Concepts)-How to open ‘emulator’ programmatically through ‘Appium-DesiredCapabilities’

Get a step-by-step walkthrough in the video below!
For Android, tests often require launching an emulator before executing scripts. While emulators can be started manually via Android Studio, automating this step improves efficiency and makes the testing pipeline smoother.
In this article, we’ll explore how to open an Android Emulator programmatically using Appium DesiredCapabilities, integrating emulator boot and test launch into one automated flow.
Step-by-Step: Launch Emulator and Appium Test Programmatically
Start the Emulator Programmatically
You can start the Android Emulator using a Capability — “appium:avd” command from within your test script before initializing the DesiredCapabilities
. Here's how:
Command:
UiAutomator2Options cap =new UiAutomator2Options();
cap.setCapability("appium:avd",<AVD_Name>);
Note:
Use this option to display a list of AVD names from your Android home directory, run below command:
emulator -list-avds
Java Example:
public void steup() throws MalformedURLException {
UiAutomator2Options cap =new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setAppPackage(AUTappPackage);
cap.setAppActivity(AUTappActivity);
cap.setNoReset(true);
cap.setCapability("appium:avd","device11");
cap.setDeviceName("device11");
driver=new AndroidDriver(new URL("http://127.0.0.1:4723"), cap);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
}
Alternatively, use the full path to emulator
if it's not in your system path:
Runtime.getRuntime().exec("C:\\Users\\YourName\\AppData\\Local\\Android\\Sdk\\emulator\\emulator -avd Pixel_4_API_30");
“Complete Code: Optimized and Ready to Use”:
package com.appiumguide.advanceappiumconcepts;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import org.openqa.selenium.remote.SessionId;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
/**
* Script Details - 1.How to open emulator programmatically through ‘Appium-DesiredCapabilities'-Mobile Automation
*
* appium-java-client version: 9.3.0
*
* @author 'Ramesh Kodumuru' for AppiumGuide [appiumguide@gmail.com]
*/
public class OpenEmulatorTest {
private AndroidDriver driver;
String AUTappPackage="com.fastaguser";
String AUTappActivity="com.fastaguser.newui.InteractiveActivity";
@BeforeTest
public void steup() throws MalformedURLException {
UiAutomator2Options cap =new UiAutomator2Options();
cap.setPlatformName("android");
cap.setAutomationName("uiautomator2");
cap.setAppPackage(AUTappPackage);
cap.setAppActivity(AUTappActivity);
cap.setNoReset(true);
cap.setCapability("appium:avd","device11");
cap.setDeviceName("device11");
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 sessionidsample() throws InterruptedException {
Thread.sleep(5000);
SessionId sessionid_value=driver.getSessionId();
System.out.println(sessionid_value);
Thread.sleep(5000);
driver.findElement(AppiumBy.xpath("//android.widget.LinearLayout[@resource-id=\"com.fastaguser:id/nhaionlinenew\"]/android.widget.LinearLayout/android.widget.ImageView")).click();
Thread.sleep(5000);
driver.findElement(AppiumBy.xpath("//android.widget.TextView[@resource-id=\"com.fastaguser:id/branch_company_name\" and @text=\"Amazon\"]")).click();
}
}
Conclusion
Automating emulator startup via Appium and DesiredCapabilities improves test workflow efficiency, especially in CI/CD pipelines. By incorporating emulator boot into your test script, you minimize manual intervention and create more scalable automation.
No comments:
Post a Comment