Android Emulator is only getting invoked when the test is getting failed

16 Views Asked by At

Problem I have this AndroidDriverManager class to initialize the android driver and to run appium server and to start emulator using code as well. So I have written the code in a way that First Emulator will start then appium server then driver will initialize and test will run.

Observation But What I am observing is Emulator is not getting started until the test is getting failed and whole execution is getting stopped. So I can see the appium server is running and it is saying that no device has been detected then the test failed and execution stops then the emulator starts.

Things I have tried I have provided the wait as well after executing the cmd command to start emulator while execution wait works but emulator is not getting invoked. It invokes only after test failed and execution stops.

AndroidDriverManager.class

package ProjectIntegration.Managers;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.appium.java_client.service.local.flags.GeneralServerFlag;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.HashMap;

public class AndroidDriverManager {
    private static AndroidDriver driver;
    private static AppiumDriverLocalService server;

    private static Process process;

    public static void startAppiumService()
    {
        HashMap<String, String> environment = new HashMap<>();
        environment.put("PATH", "C:\\Program Files\\nodejs;" + System.getenv("PATH"));
        AppiumServiceBuilder builder = new AppiumServiceBuilder();
        builder
                .withAppiumJS(new File("C:\\Users\\singh\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"))
                .usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))
                .usingPort(4723)
                .withEnvironment(environment)
                .withArgument(GeneralServerFlag.LOCAL_TIMEZONE)
                .withLogFile(new File("AppiumLog.txt"));
        server = AppiumDriverLocalService.buildService(builder);
        System.out.println("Server started at :" + server.getUrl());
        server.start();
        System.out.println("Server is Started.");
    }

    public static void startEmulator(String avdName) throws IOException {
        try {
            String emulatorPath = "C:\\Users\\singh\\AppData\\Local\\Android\\Sdk\\emulator\\emulator.exe";
            String command = emulatorPath + " -avd " + avdName;
            process = Runtime.getRuntime().exec(command);
            Thread.sleep(60000);
        }catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    public static AndroidDriver getDriver() throws IOException {
        startEmulator("emulator");
        startAppiumService();
        if(driver == null)
        {
            UiAutomator2Options options = new UiAutomator2Options();
            options.setDeviceName("emulator");
            options.setPlatformName("Android");
            options.setApp("C:\\Users\\singh\\OneDrive\\Documents\\Development\\Projects\\ProjectIntegration\\src\\main\\resources\\General-Store.apk");
            options.setCapability("adbExecTimeout", 30000);

            try
            {
                driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), options);
                driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        return driver;
    }

    public static void quitDriver()
    {
        if(driver != null)
        {
            driver.quit();
            driver = null;
        }
        server.stop();
        process.destroy();
    }
}

Error in Console

io.cucumber.core.exception.CucumberException: Failed to instantiate class ProjectIntegration.StepDefinitions.Mobile.loginSteps

    at io.cucumber.core.backend.DefaultObjectFactory.cacheNewInstance(DefaultObjectFactory.java:67)
    at io.cucumber.core.backend.DefaultObjectFactory.getInstance(DefaultObjectFactory.java:45)
    at io.cucumber.java.AbstractGlueDefinition.invokeMethod(AbstractGlueDefinition.java:47)
    at io.cucumber.java.JavaStepDefinition.execute(JavaStepDefinition.java:29)
    at io.cucumber.core.runner.CoreStepDefinition.execute(CoreStepDefinition.java:66)
    at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:63)
    at io.cucumber.core.runner.ExecutionMode$1.execute(ExecutionMode.java:10)
    at io.cucumber.core.runner.TestStep.executeStep(TestStep.java:84)
    at io.cucumber.core.runner.TestStep.run(TestStep.java:56)
    at io.cucumber.core.runner.PickleStepTestStep.run(PickleStepTestStep.java:51)
    at io.cucumber.core.runner.TestCase.run(TestCase.java:84)
    at io.cucumber.core.runner.Runner.runPickle(Runner.java:75)
    at io.cucumber.junit.PickleRunners$NoStepDescriptions.lambda$run$0(PickleRunners.java:151)
    at io.cucumber.core.runtime.CucumberExecutionContext.lambda$runTestCase$5(CucumberExecutionContext.java:137)
    at io.cucumber.core.runtime.RethrowingThrowableCollector.executeAndThrow(RethrowingThrowableCollector.java:23)
    at io.cucumber.core.runtime.CucumberExecutionContext.runTestCase(CucumberExecutionContext.java:137)
    at io.cucumber.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:148)
    at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:144)
    at io.cucumber.junit.FeatureRunner.runChild(FeatureRunner.java:28)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at io.cucumber.junit.FeatureRunner.run(FeatureRunner.java:137)
    at io.cucumber.junit.Cucumber.runChild(Cucumber.java:196)
    at io.cucumber.junit.Cucumber.runChild(Cucumber.java:89)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at io.cucumber.core.runtime.CucumberExecutionContext.lambda$runFeatures$6(CucumberExecutionContext.java:148)
    at io.cucumber.core.runtime.CucumberExecutionContext.execute(CucumberExecutionContext.java:163)
    at io.cucumber.core.runtime.CucumberExecutionContext.runFeatures(CucumberExecutionContext.java:146)
    at io.cucumber.junit.Cucumber$StartAndFinishTestRun.evaluate(Cucumber.java:226)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:74)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
    at io.cucumber.core.backend.DefaultObjectFactory.cacheNewInstance(DefaultObjectFactory.java:53)
    ... 46 more
Caused by: java.lang.RuntimeException
    at ProjectIntegration.StepDefinitions.Mobile.loginSteps.<init>(loginSteps.java:22)
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
    ... 49 more


Process finished with exit code -1
0

There are 0 best solutions below