Morsa: Jetpack Compose UI Testing Framework

Overview

Morsa: Jetpack Compose UI Testing Framework

Release

Test library to ease UI testing with Jetpack Compose

Purpose

This library aims to add some useful wrappers and abstractions over common Espresso functions used while testing UI in your Android apps so you can write UI tests as fast as you create Compose screens. The library will also help you write more readable tests, thus improving their maintainability.

Why morsa?

🚧 🚧 🚧 🚧 🚧

Setting Up

In your main build.gradle, add jitpack.io repository in the buildscript block and include the library as a dependency:

Groovy
buildscript {
    repositories { 
        maven { url "https://jitpack.io" }
    }
    dependencies {
        classpath "com.github.hyperdevs-team:morsa:0.0.2"
    }
}
Kotlin
buildscript {
    repositories { 
        maven("https://jitpack.io")
    }
    dependencies {
        classpath("com.github.hyperdevs-team:morsa:0.0.2")
    }
}

How to use

🚧 🚧 🚧 🚧 🚧 Morsa is verbose and easy to use. The library exposes function to find components based on testing tags, text values or content descriptions inside a Jetpack Compose scope making use of the ComposeContentTestRule class.

First of all, you'll need to mark your components with either:

  • Modifier.testTag(tag)
  • Modifier.contentDescription(contentDescription) Or just be sure that you can find them with a given text so Morsa can find them.

Then, in our test class we need to declare a MorsaScreen targeting the components that compose our screen. The components can be identified using the following functions:

  • withTag(tag) will search in your Compose component hierarchy for a component tagged with Modifier.testTag(tag).
  • withText(text) will search in your Compose component hierarchy for a component with the given text.
  • withContentDescription(contentDescription) will search in your Compose component hierarchy for a component marked with Modifier.contentDescription(contentDescription).
class LoginMorsaScreen(testRule: ComposeContentTestRule) : MorsaScreen
   (testRule) {
    
   val usernameTextField 
   = 
   MTextField { withTag(
   USERNAME_TEXT_FIELD_DEFAULT_TAG) }
    
   val passwordTextField 
   = 
   MTextField { withTag(
   PASSWORD_TEXT_FIELD_DEFAULT_TAG) }
    
   val loginButton 
   = 
   MText { withTag(
   LOGIN_SCREEN_LOGIN_BUTTON_TAG) }
    
   val errorView 
   = 
   MView { withTag(
   LOGIN_SCREEN_ERROR_BOX_TAG) }
}
  

When your screen definition is done, you can start doing tests with it:

class LoginContentTest {
    @get:Rule
    val composeTestRule = createComposeRule()

    val screen = LoginMorsaScreen(composeTestRule)

    @Test
    fun login_shows_error_box_on_error() {
        screen {
            //Set your compose view, this can be a whole screen or a single component
            setContent {
                MyMaterialTheme {
                    LoginContent()
                }
            }
            //These values should trigger an error on our view showing our errorView component
            usernameTextField { typeText("[email protected]") }
            passwordTextField { typeText("password123") }

            loginButton.click()
            //Assert over the view
            errorView {
                exist()
                isDisplayed()
            }
        }
    }
}

Acknowledgements

The ideas in this library were based on the awesome Kakao library that we used extensively to do UI Testing with the traditional View system in Android. Go check them out!

Authors & Collaborators

License

This project is licensed under the Apache Software License, Version 2.0.

   Copyright 2021 HyperDevs

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
You might also like...
Fixtures for Kotlin providing generated values for unit testing

A tool to generate well-defined, but essentially random, input following the idea of constrained non-determinism.

Lovely Systems Database Testing

Lovely DB Testing This repository provides opinionated testing helpers for database testing used at Lovely Systems. License This plugin is made availa

Project for testing intern candidate simple level
Project for testing intern candidate simple level

RickAndMorty-TestTask Тестовый проект 'Гайд по мультфильму Рик и Морти' для практикантов начального и продвинутого уровня. Структура проекта Структура

Very simple Morse API text translator. Mainly to do some testing with jitpack, API development etc.

Very simple Morse text translator API. Far from finish or even being reliable. Use for single sentence. Mainly to test github dependency for Android

A set of TestRules and ActivityScenarios to facilitate UI Testing under given configurations: FontSizes, Locales

AndroidUiTestingUtils A set of TestRules, ActivityScenarios and utils to facilit

Testify — Android Screenshot Testing
Testify — Android Screenshot Testing

Testify — Android Screenshot Testing Add screenshots to your Android tests Expand your test coverage by including the View-layer. Testify allows you t

PowerMock is a Java framework that allows you to unit test code normally regarded as untestable.
PowerMock is a Java framework that allows you to unit test code normally regarded as untestable.

Writing unit tests can be hard and sometimes good design has to be sacrificed for the sole purpose of testability. Often testability corresponds to go

A powerful test framework for Android

Cafe A powerful test framework for Android named Case Automated Framework for Everyone. Home Page http://baiduqa.github.com/Cafe/ How to make Cafe dow

A powerful test framework for Android

Cafe A powerful test framework for Android named Case Automated Framework for Everyone. Home Page http://baiduqa.github.com/Cafe/ How to make Cafe dow

Comments
  • Hm...taste like Kakao

    Hm...taste like Kakao

    Have a good day. @adriangl @FrangSierra appreciate for your project. But honestly it looks like a https://github.com/KakaoCup/Compose. Based on our experience with https://github.com/KakaoCup/Kakao we added similar framework for Compose support.

    enhancement 
    opened by Vacxe 3
Owner
HyperDevs
HyperDevs
Selenium locators for Java/Kotlin that resemble the Testing Library (testing-library.com).

Selenium Testing Library Testing Library selectors available as Selenium locators for Kotlin/Java. Why? When I use Selenium, I don't want to depend on

Luís Soares 5 Dec 15, 2022
A programmer-oriented testing framework for Java.

JUnit 4 JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. For more infor

JUnit 8.4k Jan 9, 2023
Android Unit Testing Framework

Robolectric is the industry-standard unit testing framework for Android. With Robolectric, your tests run in a simulated Android environment inside a

Robolectric 5.6k Jan 3, 2023
A programmer-oriented testing framework for Java.

JUnit 4 JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. For more infor

JUnit 8.4k Dec 28, 2022
Snapshot Testing framework for Kotlin.

KotlinSnapshot Snapshot Testing framework for Kotlin. What is this? Snapshot testing is an assertion strategy based on the comparision of the instance

Pedro Gómez 157 Nov 13, 2022
Android UI Testing

User scenario testing for Android Robotium is an Android test automation framework that has full support for native and hybrid applications. Robotium

null 2.8k Dec 14, 2022
A set of AssertJ helpers geared toward testing Android.

AssertJ Android A set of AssertJ assertions geared toward testing Android. Deprecated The support libraries and play services are developing at a rate

Square 1.6k Jan 3, 2023
Android UI Testing

User scenario testing for Android Robotium is an Android test automation framework that has full support for native and hybrid applications. Robotium

null 2.7k Apr 8, 2021
A sample repo describing best practices for snapshot testing on Android

Road to effective snapshot testing A sample repo describing best practices for snapshot testing on Android. This includes for now: Parameterized Tests

Sergio Sastre Flórez 86 Jan 1, 2023
Turbine is a small testing library for kotlinx.coroutines Flow.

A small testing library for kotlinx.coroutines Flow

Cash App 1.8k Jan 5, 2023