Advanced Topics

On this page we describe Selendroid advanced concepts.

Native events versus synthetic events

In Selendroid are by default native events used. There are scenarios where the Android Instrumentation Framework has limitations with regards to simulating user input with different locales. Native events should be used whenever it is possible. But when specific locales are causing issues, Selendroid offers you to disable native events.

Example: Use Synthetic events for native UIs

@Test
public void shouldSetJapaneseTextIntoNativeTextField() {
  //Initialize your Selendroid Driver and navigate to your main Activity
  Configuration configurable = (Configuration) driver();
  configurable.setConfiguration(DriverCommand.SEND_KEYS_TO_ELEMENT, "nativeEvents", false);
  String text = "ありがとう";
  WebElement input = driver().findElement(By.id("my_text_field"));
  input.sendKeys(text);
  Assert.assertEquals(text, input.getText());
}

This mechanism can be used for native UI and WebView elements. A full example you can find in our Selendroid test suite.

Dynamic Extendability

Selendroid is hte first Mobile Test Automation framework that can be extended by the users with your own code at runtime! We provide the ability to implement your own request handler that extends our BaseRequestHandler. We provide a full demo project that you can find at Github.

Implement it:

package io.selendroid.extension;
import io.selendroid.server.http.HttpRequest;
import io.selendroid.server.BaseRequestHandler;
import io.selendroid.server.Response;
import io.selendroid.server.SelendroidResponse;
import org.json.JSONException;
public class DemoExtensionHandler extends BaseRequestHandler {
  public DemoExtensionHandler(String mappedUri) {
    super(mappedUri);
  }
  @Override
  public Response handle(HttpRequest request) throws JSONException {
    return new SelendroidResponse(getSessionId(request), "I'm an extension!");
  }
}

Build it:

#!/bin/bash
mvn clean install
cd target
dx --dex --output=extension.dex extension-0.0.1-SNAPSHOT.jar

Use it:

@Test
public void extensionCallShouldSucceed() {
  SelendroidCapabilities capa = new SelendroidCapabilities("io.selendroid.testapp:1.0");
  capa.setSelendroidExtensions(myExtension.dex)
  WebDriver driver = new SelendroidDriver(capa);
  assertEquals("I'm an extension!",
    driver().callExtension("io.selendroid.extension.DemoExtensionHandler"));
}

Background App Functionality

Selendroid offers you to put the app under test into the background and resume it later.

//put the app to the background
driver().backgroundApp();
//resume app
driver().resumeApp();

Screen Brightness

Selendroid offers you to control the brightness of the screen.

ScreenBrightness brightness = (ScreenBrightness) driver();
brightness.setBrightness(0);
brightness.setBrightness(50);
brightness.setBrightness(100);

Screen Orientation

Selendroid offers you to control the orientation of the screen per activity.

driver().getOrientation();
driver().rotate(ScreenOrientation.LANDSCAPE);

Adb Connection

Selendroid offers you to interact with the device via ADB. The benefit is that the limitations of the Android Instrumentation framework of testing only one Android process can be avoided.

  • sendText(String text)
  • sendKeyEvent(int keyCode)
  • tap(int x, int y)
  • executeShellCommand(String command)