Espresso Test Guide - forem/ForemWebView-android GitHub Wiki

Overview

Espresso tests help us write automation test cases for user interface (UI) testing. Some key points to keep in mind related to Espresso tests:

  • These tests are device dependent ,i.e., based on how a test has been written it can pass or fail depending upon the device.
  • These tests are run on emulator and therefore we won't be adding them to CI checks as they will end up consuming a lot of resources.
  • These are specifically written for unit testing only.
  • These tests can check things only within the app and not outside that scope. For example espresso tests cannot interact with other apps and cannot check notifications.

Example test

In the below test case testMainActivity_loadDevVideoArticle_playVideo_opensVideoActivity we are following below mentioned steps in the test case:

  1. Using launchActivity we are starting our MainActivity with the specific url.
  2. Using onWebView we are locating a specific ID so that per can perform a click event on that view.
  3. As soon as we click on the above view the app starts the VideoPlayerActivity where the video is played in full screen to check that we are using intended to make sure that correct activity is opened and also correct information is transferred to that activity.
@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    private val context: Context = getInstrumentation().targetContext

    @Before
    fun setup() {
        Intents.init()
        EspressoIdlingResource.increment()
    }

    @After
    fun tearDown() {
        Intents.release()
        EspressoIdlingResource.decrement()
    }

    @Test
    fun testMainActivity_loadDevVideoArticle_playVideo_opensVideoActivity() {
        launchActivity<MainActivity>(createMainActivityIntent("https://dev.to/ben/why-forem-is-special-with-ben-halpern-1d61"))

        onWebView()
            .withElement(findElement(Locator.ID, "video-player-407788"))
            .perform(webClick())

        intended(hasComponent(VideoPlayerActivity::class.java.name))
        intended(
            hasExtra(
                VideoPlayerActivity.VIDEO_URL_INTENT_EXTRA,
                "https://dw71fyauz7yz9.cloudfront.net/video-upload__a8eaf9049e79b4def2008c6c4b9bff09/video-upload__a8eaf9049e79b4def2008c6c4b9bff09.m3u8"
            )
        )
        intended(hasExtra(VideoPlayerActivity.VIDEO_TIME_INTENT_EXTRA, "0"))
    }

    private fun createMainActivityIntent(foremUrl: String): Intent {
        return MainActivity.newInstance(context = context, url = foremUrl)
    }
}

Guidelines

Convention

File Name: If we are writing test case of MainActivity the corresponding espresso test file name should be MainActivityTest, i.e. <file_name>Test.kt.

Test Name: testAction_withOneCondition_withSecondCondition_hasExpectedOutcome. Example: testMainActivity_loadDevArticle_clickOnMoreOptions_opensShareIntent in this testMainActivity is test action, loadDevArticle & clickOnMoreOptions are two different conditions and opensShareIntent is the expected outcome

Tests should only be written to verify the behaviour of public methods/functions. Private functions should not be used in behavioural tests.

Changes in settings

Sometimes because of animations the Espresso tests can fail therefore its safe to make before changes on the device on which the tests are being run.

Turn of animations

  1. Enable the developer options in your android device by following these steps.
  2. Go to Settings
  3. Search for animations
  4. Click on either one of these Window animation scale, Transition animation scale or Animator duration scale.
  5. Or you can even find the above options directly in Developer Options.
  6. Change the value of all these options to Animation Off.

Now its safe to run test cases.

Running espresso test in Android Studio

Note: Espresso test requires a device to run so make sure you have an emulator setup or a device connected on which the tests will be run.

If your test files are under following directory: app/src/androidTest/java then you can notice the Play icon next to your test file name or a particular test, example:

Screenshot 2022-06-30 at 5 05 24 PM Screenshot 2022-06-30 at 5 05 29 PM

If these icons/buttons are not available to run the test then you can perform following steps:

  1. Click on Run -> Edit Configurations
  2. Click on + icon on left-hand side and select Android Instrumentation Tests
  3. In the window select a module, Test Type like Class, Package, Method, etc. and other details similar to below screenshot.
  4. Click on Apply / OK
  5. Now you can run the espresso tests.
Screenshot 2022-06-30 at 5 08 52 PM

Resources

⚠️ **GitHub.com Fallback** ⚠️