How to launch emulator through java program

46 Views Asked by At

I am new to mobile testing, I have written code to launch Appium server and to create android driver, and which getting failed to launch the specific android emulator when I run the code but if I launch the emulator manually then run, it works fine. may I know what I am missing here ? is there any way to launch emulators/simulators programatically?

Tech stack: java-client:9.0.0, testng, Appium 2.4.1, Java 17, macOS

I have tried the below way

`private AppiumDriverLocalService getAppiumServiceForMac() {
        HashMap<String, String> environment = getEnvironmentMapForAppiumServer();
        String nodePath="/usr/local/bin/node";
        String mainJSPath="/usr/local/lib/node_modules/appium/";
        return AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
                .usingDriverExecutable(new File(nodePath))
                .withAppiumJS(new File(mainJSPath))
                .usingAnyFreePort()
                .withArgument(GeneralServerFlag.SESSION_OVERRIDE)
                .withArgument(GeneralServerFlag.USE_DRIVERS, "uiautomator2")
                .withArgument (GeneralServerFlag.LOG_LEVEL, "debug")
                .withArgument(USE_DRIVERS, "chromium")
               .withArgument (ALLOW_INSECURE, "chromedriver_autodownload")
               
        );
    }`

AppiumDriverLocalService service = getAppiumServiceForMac(); service.start();

For android:

`String appUrlAndroid = System.getProperty("user.dir") + "//src//test//resources//app//Apitest.apk";
UiAutomator2Options androidOptions = new UiAutomator2Options()
                        .clearDeviceLogsOnStart()
                        .setAutomationName(AutomationName.ANDROID_UIAUTOMATOR2)
                        .setPlatformName("Android")
                        .setUdid("emulator-5554")                     
                        .setAutoGrantPermissions(true)
                        //.withBrowserName("Chrome")
                        .setApp(appUrlAndroid)
                        .setAppPackage(getAppiumProps().appPackage())
                        .setAppActivity(getAppiumProps().appActivity())
                        .setAppWaitActivity(getAppiumProps().appWaitActivity())
                        .setUiautomator2ServerLaunchTimeout(Duration.ofSeconds(60))
                 .setNewCommandTimeout(Duration.ofSeconds(getAppiumProps().command_timeout()))
                        ;
                driver = new AndroidDriver(url, androidOptions);`

What I am expecting:

  1. I wan to launch and run test on emulator through programatically in java
  2. I dont want to launch any emulator in manual way
  3. I want to open browser and do test on emulator
0

There are 0 best solutions below