Search code examples
javaappium

Start appium automatically within test execution?


Is there a way I can get appium to startup within the code I am writing for a junit test? Since appium only needs to run when my test is running it doesnt make sense to me to keep the appium server always going.

Right now I am using junit and maven to run test builds. Due to stability issues with appium it will sometimes die in the middle of the build, thus failing all remaining tests. I want to know if it is possible to add something to the @Before method to start the appium server before connecting the WebDriver to it, and then terminating it in the @After method. This should address any issues with appium failures since it can reset before starting the next test.

Still looking into starting and ending processes in general in java to see if this will work. If I figure this out I will update this post to help anyone else interested in testing this way.


Solution

  • I've solved this in a very similar way using a DefaultExecutor to keep track of the Appium process so it can be destroyed at the end of the tests. This also allows the Appium output to be logged out during the tests using a DefaultExecuteResultHandler.

    To avoid using a sleep to wait for Appium to start, you could create your WebDriver instance inside a try-catch

    for (int i = 10; i > 0; i--) {
            try {
                driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
                // If we successfully attach to appium, exit the loop.
                i = 0;
            } catch (UnreachableBrowserException e) {
                LOGGER.info("Waiting for Appium to start");
            }
        }
    

    You could add a sleep in the catch block if you wanted to poll less frequently.