In this blog, we would discuss how we can start and stop Appium driver and Web driver service programmatically so that you don’t have to depend on a script to start your server during CI/CD execution.
Creating Appium driver service through AppiumDriverLocalService object:
You can use either AppiumDriverLocalService.buildDefaultService() or new AppiumServiceBuilder().build() to create AppiumDriverLocalService objects. In the later case, wrap your AppiumServiceBuilder method invocation inside AppiumDriverLocalService.buildService(...) method. Then just use the start() method to start the Appium server. Example:
AppiumDriverLocalService service = AppiumDriverLocalService.buildDefaultService();
service.start();
This would create the service with default arguments. A better way to create the service is to have given parameters that you can control. Example:
AppiumDriverLocalService service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
.withIPAddress("customProperties.get("ipAddress")
.usingAnyFreePort()
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withArgument(GeneralServerFlag.LOG_LEVEL, customProperties.get("logLevel"))
.withArgument(CustomServerFlag.BASE_PATH, "/wd/hub")
.withEnvironment(env)
.usingDriverExecutable(new File("/opt/homebrew/bin/node"))
.withAppiumJS(new File("/opt/homebrew/bin/appium"))
.withArgument(GeneralServerFlag.RELAXED_SECURITY));
All the string inputs above are configurable. To demonstrate this, I have used a map customProperties to store the values for ipAddress and logLevel. These could be 127.0.0.0 and error respectively.
Creating Web driver service through DriverService object:
Unlike AppiumDriverService which works for both Android and iOS, you need to create instances of browser specific driver service for Web. Example, for chrome:
DriverService webService = new ChromeDriverService.Builder()
.usingDriverExecutable(new File(customProperties.get("chromedriverPath")))
.usingAnyFreePort()
.build();
Similarly for Firefox, use GeckoDriverService as below:
DriverService webService = new GeckoDriverService.Builder()
.usingAnyFreePort()
.usingDriverExecutable(new File(customProperties.get("geckodriverPath")))
.usingAnyFreePort()
.build();