Appium Cheat sheet for handling Appium local service.

IntentTest.beforeClass()

/**
* initialization.
*/
@BeforeClass public static void beforeClass() {
service = AppiumDriverLocalService.buildDefaultService();
service.start();
if (service == null || !service.isRunning()) {
throw new RuntimeException("An appium server node is not started!");
}
File appDir = new File("src/test/java/io/appium/java_client");
File app = new File(appDir, "IntentExample.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
driver = new AndroidDriver<>(service.getUrl(), capabilities);
}

AppiumDriverLocalService.buildDefaultService()

public static AppiumDriverLocalService buildDefaultService() {
return buildService(new AppiumServiceBuilder());
}

ServerBuilderTest.checkAbilityToShutDownService()

@Test public void checkAbilityToShutDownService() {
service = buildDefaultService();
service.start();
service.stop();
assertFalse(service.isRunning());
}

AppiumDriverLocalService.addSlf4jLogMessageConsumer(…)

addLogMessageConsumer(logMessage -> {
slf4jLogMessageConsumer.accept(logMessage, parseSlf4jContextFromLogMessage(logMessage));
});

FingerPrintTest.beforeClass()

/**
* initialization.
*/
@BeforeClass public static void beforeClass() {
service = AppiumDriverLocalService.buildDefaultService();
service.start();
if (service == null || !service.isRunning()) {
throw new ExceptionInInitializerError("An appium server node is not started!");
}
}

AcquireTokenSilentTestWithAuthenticator.startAppiumServer()

@BeforeClass
public static void startAppiumServer() throws IOException {
appiumService = AppiumDriverLocalService.buildDefaultService();
appiumService.start();
}

MobileBrowserCompatibilityTest.setUp()

/**
* The setting up.
*/
@Before public void setUp() {
service = AppiumDriverLocalService.buildDefaultService();
service.start();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, MobileBrowserType.BROWSER);
driver = new AndroidDriver<RemoteWebElement>(service.getUrl(), capabilities);
//This time out is set because test can be run on slow Android SDK emulator
PageFactory.initElements(new AppiumFieldDecorator(driver, ofSeconds(5)), this);
}

ServerBuilderTest.checkAbilityToChangeOutputStreamAfterTheServiceIsStarted()

@Test public void checkAbilityToChangeOutputStreamAfterTheServiceIsStarted() throws Exception {
testLogFile = new File("test");
testLogFile.createNewFile();
stream = new FileOutputStream(testLogFile);
service = buildDefaultService();
service.start();
service.addOutPutStream(stream);
service.isRunning();
assertThat(testLogFile.length(), greaterThan(0L));
}

ServerBuilderTest.checkAbilityToStartServiceUsingNonLocalhostIP()

@Test public void checkAbilityToStartServiceUsingNonLocalhostIP() {
service = new AppiumServiceBuilder().withIPAddress(testIP).build();
service.start();
assertTrue(service.isRunning());
}

ServerBuilderTest.checkAbilityToStartServiceWithPortUsingFlag()

@Test public void checkAbilityToStartServiceWithPortUsingFlag() {
String port = "8996";
String expectedUrl = String.format("http://0.0.0.0:%s/wd/hub", port);
service = new AppiumServiceBuilder()
.withArgument(() -> "--port", port)
.build();
String actualUrl = service.getUrl().toString();
assertEquals(expectedUrl, actualUrl);
service.start();
}

ServerBuilderTest.checkAbilityToAddLogMessageConsumer()

@Test public void checkAbilityToAddLogMessageConsumer() {
List<String> log = new ArrayList<>();
service = buildDefaultService();
service.clearOutPutStreams();
service.addLogMessageConsumer(log::add);
service.start();
assertTrue(log.size() > 0);
}

ServerBuilderTest.checkAbilityToChangeOutputStream()

@Test public void checkAbilityToChangeOutputStream() throws Exception {
testLogFile = new File("test");
testLogFile.createNewFile();
stream = new FileOutputStream(testLogFile);
service = buildDefaultService();
service.addOutPutStream(stream);
service.start();
assertThat(testLogFile.length(), greaterThan(0L));
}

ThreadSafetyTest$3.perform()

@Override protected String perform() {
service.stop();
return "OK";
}
};

ThreadSafetyTest$1.perform()

@Override protected String perform() {
service.start();
return "OK";
}
};

AppiumDriver.<init>(…)

public AppiumDriver(HttpClient.Factory httpClientFactory, Capabilities desiredCapabilities) {
this(AppiumDriverLocalService.buildDefaultService(), httpClientFactory,
desiredCapabilities);
}

AppiumDriverLocalService.start()

lock.lock();
try {
if (isRunning()) {
return;
process.copyOutputTo(stream);
process.executeAsync();
ping(startupTimeout, timeUnit);
} catch (Throwable e) {
destroyProcess();
String msgTxt = "The local appium server has not been started. "
+ "The given Node.js executable: " + this.nodeJSExec.getAbsolutePath()

ThreadSafetyTest.whenFewTreadsDoTheSameWork()

assertTrue(service.isRunning());
assertFalse(service.isRunning());
} finally {
if (service.isRunning()) {
service.stop();

ThreadSafetyTest$2.perform()

@Override protected Boolean perform() {
return service.isRunning();
}
};

AppiumServer.start()

/**
* @author wasiq.bhamla
* @since 12-Apr-2017 5:23:19 PM
*/
public void start () {
log.trace ("Starting Appium Service...");
if (!this.setting.isExternal () && !this.setting.isCloud ()) {
this.service = AppiumDriverLocalService.buildService (this.builder);
try {
this.service.start ();
} catch (final AppiumServerHasNotBeenStartedLocallyException e) {
fail (AppiumServerNotStartingError.class,
"Error occured while starting Appium server", e);
} catch (final Exception e) {
fail (AppiumServerAlreadyRunningError.class, "Appium server is running already.",
e);
}
log.trace ("Appium Service Started...");
} else {
if (isRunning ()) {
log.trace ("Appium Service is already running...");
}
}
}

FingerPrintTest.initDriver()

private static void initDriver() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability("appPackage", "com.android.settings");
capabilities.setCapability("appActivity", "Settings");
driver = new AndroidDriver<>(service.getUrl(), capabilities);
driver.manage().timeouts().implicitlyWait(15, SECONDS);
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Khemlall Mangal

Khemlall Mangal

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.