Giter VIP home page Giter VIP logo

morsa's Introduction

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<LoginMorsaScreen>(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.

morsa's People

Contributors

adriangl avatar frangsierra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

morsa's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.