# Android native automation testing

{% hint style="info" %}
Appfigurate doesn't currently support automation testing Flutter or React Native apps.
{% endhint %}

Appfigurate can change the configuration of an Android app being automation tested using using Espresso.

An example UI automation test using Espresso is available in `AppfigurateExample`. To test, ensure `MainActivityTest` is selected as the Run Configuration, then tap the run button.

<figure><img src="https://1008176080-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fw1fcw3dvtSrfUh3YtO9Z%2Fuploads%2FfP34UIyG2lIyfFX4HHFR%2FMainActivityTestKotlin.png?alt=media&#x26;token=ce671135-282b-44de-93b3-33ac90890b02" alt=""><figcaption></figcaption></figure>

## Ensure allowInvalidSignatures returns true

In your [<mark style="color:blue;">`nz.co.electricbolt.appfiguratelibrary.Configuration`</mark>](https://www.electricbolt.co.nz/api/android/nz/co/electricbolt/appfiguratelibrary/Configuration.html) subclass, ensure that your [<mark style="color:blue;">`allowInvalidSignatures`</mark>](https://www.electricbolt.co.nz/api/android/nz/co/electricbolt/appfiguratelibrary/Configuration.html#allowInvalidSignatures\(\)) method returns `true` when running automation tests.

## Test setup

Before each test, call the [<mark style="color:blue;">`automationReset`</mark>](https://www.electricbolt.co.nz/api/android/nz/co/electricbolt/appfiguratelibrary/Configuration.html#automationReset\(\)) method to ensure the app's configuration is reset to factory defaults.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
private var config: ExampleConfiguration? = null
    
@Before
fun setUp() {
    config = Configuration.sharedConfiguration() as ExampleConfiguration
    config?.automationReset()
}
```

{% endtab %}

{% tab title="Java" %}

```java
private ExampleConfiguration config;

@Before
public void setUp() {
    config = (ExampleConfiguration) Configuration.sharedConfiguration();
    config.automationReset();
}
```

{% endtab %}
{% endtabs %}

## Applying configuration to an automation test

In your test method, set your configuration properties to the values you require, then call the [<mark style="color:blue;">`automationApply`</mark>](https://www.electricbolt.co.nz/api/android/nz/co/electricbolt/appfiguratelibrary/Configuration.html#automationApply\(\)) method.

{% tabs %}
{% tab title="Kotlin" %}

```kotlin
@Test
fun yourTest() {
    config?.bool = false
    config?.string_Textfield = "thursday"
    config?.automationApply()
    ...
```

{% endtab %}

{% tab title="Java" %}

```java
@Test
public void yourTest() {
    config.bool = false;
    config.string_Textfield = "thursday";
    config.automationApply();
    ...
```

{% endtab %}
{% endtabs %}

## Additional automation launch methods

See also [<mark style="color:blue;">`automationAction`</mark>](https://www.electricbolt.co.nz/api/android/nz/co/electricbolt/appfiguratelibrary/Configuration.html#automationAction\(java.lang.String\)) method of [<mark style="color:blue;">`nz.co.electricbolt.appfiguratelibrary.Configuration`</mark>](https://www.electricbolt.co.nz/api/android/nz/co/electricbolt/appfiguratelibrary/Configuration.html)

## Applying configuration at runtime to the app under test

{% hint style="info" %}
Android's Espresso tests run in the same process as the app under test, and therefore the iOS specific APIs [<mark style="color:blue;">`automationSendConfiguration`</mark>](https://www.electricbolt.co.nz/api/Classes/APLConfiguration.html#/c:objc\(cs\)APLConfiguration\(im\)automationSendConfiguration), [<mark style="color:blue;">`automationSendConfigurationReset`</mark>](https://www.electricbolt.co.nz/api/Classes/APLConfiguration.html#/c:objc\(cs\)APLConfiguration\(im\)automationSendConfigurationReset), [<mark style="color:blue;">`automationSendConfigurationWithAction`</mark>](https://www.electricbolt.co.nz/api/Classes/APLConfiguration.html#/c:objc\(cs\)APLConfiguration\(im\)automationSendConfigurationWithAction:), [<mark style="color:blue;">`automationSendReadConfiguration`</mark>](https://www.electricbolt.co.nz/api/Classes/APLConfiguration.html#/c:objc\(cs\)APLConfiguration\(im\)automationSendReadConfiguration), [<mark style="color:blue;">`APLAutomationSendMessage`</mark>](https://www.electricbolt.co.nz/api/Functions.html#/c:@F@APLAutomationSendMessage) and [<mark style="color:blue;">`APLAutomationMessageReceivedBlock`</mark>](https://www.electricbolt.co.nz/api/Functions.html#/c:@F@APLAutomationMessageReceivedBlock) functions are not required and not implemented in Android.
{% endhint %}
