Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Travis-CI and Espresso Test

I currently have Travis-CI set up so that on every build of my Android device it runs the gradle ConnectedCheck task and executes all of my unit tests. I have been able to set this up successfully. I am now trying to build some functional test with Espresso and I am currently running into alot of difficulty with setting Travis up in a manner so that my espresso tests can interact with Travis's emulator. How do I go about setting up Travis so that its emulator will work exactly like the one I use on my local workstation?

Here is a section of my .travis.yml that I use to build the emulator.

language: android
jdk: oraclejdk7
env:
  matrix:
    - ANDROID_TARGET=android-19  ANDROID_ABI=armeabi-v7a

android:
  components:
    - build-tools-20.0.0
    - build-tools-19.1.0

before_script:
  # Create and start emulator
  - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI
  - emulator -avd test -no-skin -no-audio -no-window &
  - android-wait-for-emulator
  - adb shell input keyevent 82 &
like image 870
Andre Perkins Avatar asked Sep 26 '14 17:09

Andre Perkins


2 Answers

Updated September 7th, 2015

It was incredibly frustrating, but I got Espresso in the android support library to run successfully on Travis CI. This is the exact configuration that worked for me. The specific sdk and library version numbers mattered, so do not change them or you will run into problems. The resolution strategy for the support annotations also was necessary, so do not remove that either. Since Travis CI's android support is still in beta, this answer could become outdated. Check the Travis CI website for updates here.

.travis.yml

language: android
jdk: openjdk7
android:
  components:
    - build-tools-22.0.1
    - android-20
    - extra
    - addon
    - sys-img-armeabi-v7a-android-19
before_script:
  - echo no | android create avd --force -n test -t android-19 --abi armeabi-v7a
  - emulator -avd test -no-skin -no-audio -no-window &
  - android-wait-for-emulator
  - adb shell input keyevent 82 &
script:
    - ./gradlew connectedAndroidTest

build.gradle

apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.application'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
    }
}

android {
    compileSdkVersion 20
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 11
        targetSdkVersion 20
        testApplicationId "com.example.app.test"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'com.android.support:support-v4:20.+'
    compile 'joda-time:joda-time:2.3'
    compile 'com.squareup.retrofit:retrofit:1.4.1'
    compile 'com.squareup.retrofit:retrofit-converters:1.9.0'
    compile 'com.squareup.retrofit:retrofit-mock:1.4.0'
    compile 'com.fasterxml.jackson.core:jackson-core:2.3.1'
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.3.0'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.3.1'
    compile 'com.google.guava:guava:16.0'
    androidTestCompile 'com.android.support:support-annotations:20.+'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
    androidTestCompile 'com.android.support.test:runner:0.3'
    androidTestCompile 'com.squareup:fest-android:1.0.7'
}

configurations.all {
    resolutionStrategy {
        // fail eagerly on version conflict (includes transitive dependencies)
        // e.g. multiple different versions of the same dependency (group and name are equal)
        failOnVersionConflict()

        // force certain versions of dependencies (including transitive)
        //  *append new forced modules:
        force 'com.android.support:support-annotations:20.+', 'com.squareup.retrofit:retrofit:1.4.1'
        //  *replace existing forced modules with new ones:
        forcedModules = ['com.android.support:support-annotations:20.+', 'com.squareup.retrofit:retrofit:1.4.1']

        // cache dynamic versions for 10 minutes
        cacheDynamicVersionsFor 10*60, 'seconds'
        // don't cache changing modules at all
        cacheChangingModulesFor 0, 'seconds'
    }
}

If you receive an error like this:

PerformException: Error performing 'single click' on view

Add this code to test:

closeSoftKeyboard();
Thread.sleep(1000);

Example

public void testThatSuccessDialogIsShownWithValidCardInput() throws Exception {
    onView(withId(R.id.card_number))
            .perform(typeText("4242424242424242"));
    closeSoftKeyboard();
    Thread.sleep(1000);
    onView(withId(R.id.card_exp_month))
            .perform(typeText("01"));
    onView(withId(R.id.card_exp_year))
            .perform(typeText("20"));
    onView(withId(R.id.card_cvc_code))
            .perform(typeText("313"));
    closeSoftKeyboard();
    Thread.sleep(1000);
    onView(withText("Submit"))
            .perform(click());
    onView(withText("Success!"))
            .check(matches(isDisplayed()));
    onView(withText("OK"))
            .perform(click());
    onView(withText("OK"))
            .check(doesNotExist());
}

Working Project

https://travis-ci.org/Kurry/Venmo-Android-Coding-Challenge

https://github.com/Kurry/Venmo-Android-Coding-Challenge

like image 138
kurryt Avatar answered Sep 23 '22 13:09

kurryt


Update:

VM images already include fixed android-wait-for-emulator script and android SDK tools version 24.0.0 by default and Espresso 2.0 released and moved to Android testing support library.

Build Environment Updates - 2014-12-09

Espresso 2.0 released - 2014-12-20

like image 38
albodelu Avatar answered Sep 22 '22 13:09

albodelu