• Stars
    star
    1,789
  • Rank 25,982 (Top 0.6 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Android UI test framework

Android Arsenal Android Weekly Android Weekly MavenCentral Build and Deploy Telegram Telegram

Kaspresso

Kaspresso is a framework for Android UI testing. Based on Espresso and UI Automator, Kaspresso provides a wide range of additional features, such as:

  • 100% stability, no flakiness.
  • Jetpack Compose support.
  • Significantly faster execution of UI Automator commands. With Kaspresso, some UI Automator commands run 10 times faster!
  • Excellent readability due to human DSL.
  • Useful interceptor mechanism to catch all actions and assertions in one place.
  • Full logging.
  • Ability to call ADB commands.
  • UI tests writing philosophy implemented with DSL.
  • Features screenshotting.
  • Robolectric support.
  • Allure support.

And many more!

Kaspresso

Integration

To integrate Kaspresso into your project:

  1. If the mavenCentral repository does not exist, include it to your root build.gradle file:
allprojects {
    repositories {
        mavenCentral()
    }
}
  1. Add a dependency to build.gradle:
dependencies {
    androidTestImplementation 'com.kaspersky.android-components:kaspresso:<latest_version>'
    // Allure support
    androidTestImplementation "com.kaspersky.android-components:kaspresso-allure-support:<latest_version>"
    // Jetpack Compose support
    androidTestImplementation "com.kaspersky.android-components:kaspresso-compose-support:<latest_version>"
}

If you are still using the old Android Support libraries, we strongly recommend to migrate to AndroidX.

The last version with Android Support libraries is:

dependencies {
    androidTestImplementation 'com.kaspersky.android-components:kaspresso:1.0.1-support'
}

Tutorial NEW

To make it easier to learn the framework, a step-by-step tutorial is available on our website.

Capabilities of Kaspresso

Readability

We like the syntax that Kakao applies to write UI tests. This wrapper over Espresso uses the Kotlin DSL approach, that makes the code significantly shorter and more readable. See the difference:

Espresso:

@Test
fun testFirstFeature() {
    onView(withId(R.id.toFirstFeature))
        .check(ViewAssertions.matches(
               ViewMatchers.withEffectiveVisibility(
                       ViewMatchers.Visibility.VISIBLE)))
    onView(withId(R.id.toFirstFeature)).perform(click())
}

Kakao:

@Test
fun testFirstFeature() {
    mainScreen {
        toFirstFeatureButton {
            isVisible()
            click()
        }
    }
}

We used the same approach to develop our own wrapper over UI Automator, and we called it Kautomator. Take a look at the code below:

UI Automator:

val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation()
val uiDevice = UiDevice.getInstance(instrumentation)
val uiObject = uiDevice.wait(
    Until.findObject(
        By.res(
            "com.kaspersky.kaspresso.sample_kautomator",
            "editText"
        )
    ),
    2_000
)
uiObject.text = "Kaspresso"
assertEquals(uiObject.text, "Kaspresso")

Kautomator:

MainScreen {
    simpleEditText {
        replaceText("Kaspresso")
        hasText("Kaspresso")
    }
}

Since Kakao and Kautomator provide almost identical APIs, you don’t have to care about what is under the hood of your tests, either Espresso or UI Automator. With Kaspresso, you write tests in the same style for both.

However, Kakao and Kautomator themselves don't help you to see the relation between the test and the corresponding test case. Also, a long test often becomes a giant piece of code that is impossible to split into smaller parts. That's why we have created an additional Kotlin DSL that allows you to read your test more easily.

See the example below:

@Test
fun shouldPassOnNoInternetScanTest() =
    beforeTest {
        activityTestRule.launchActivity(null)
        // some things with the state
    }.afterTest {
        // some things with the state
    }.run {
        step("Open Simple Screen") {
            MainScreen {
                nextButton {
                    isVisible()
                    click()
                }
            }
        }
        step("Click button_1 and check button_2") {
            SimpleScreen {
                button1 {
                    click()
                }
                button2 {
                    isVisible()
                }
            }
        }
        step("Click button_2 and check edit") {
            SimpleScreen {
                button2 {
                    click()
                }
                edit {
                    flakySafely(timeoutMs = 7000) { isVisible() }
                    hasText(R.string.text_edit_text)
                }
            }
        }
        step("Check all possibilities of edit") {
            scenario(
                CheckEditScenario()
            )
        }
    }

Stability

Sometimes your UI test passes ten times, then breaks on the eleventh attempt for some mysterious reason. It’s called flakiness.

The most popular reason for flakiness is the instability of the UI tests libraries, such as Espresso and UI Automator. To eliminate this instability, Kaspresso uses DSL wrappers and interceptors.

UI test libraries acceleration

Let’s watch some short video that shows the difference between the original UI Automator (on the right) and the accelerated one (on the left).

Here is a short explanation of why it is possible.

Interceptors

We developed Kaspresso behavior interceptors on the base of Kakao/Kautomator Interceptors to catch failures.

Thanks to interceptors, you can do a lot of useful things, such as:

  • add custom actions to each framework operation like writing a log or taking a screenshot;
  • overcome flaky operations by re-running failed actions, scrolling the parent layout or closing the android system dialog;

and many more (see the manual).

Writing readable logs

Kaspresso writes its own logs, detailed and readable:

Ability to call ADB commands

Espresso and UI Automator don't allow to call ADB commands from inside a test. To fix this problem, we developed AdbServer (see the wiki).

Ability to work with Android System

You can use Kaspresso classes to work with Android System.

For example, with the Device class you can:

  • push/pull files,
  • enable/disable network,
  • give permissions like a user does,
  • emulate phone calls,
  • take screenshots,
  • enable/disable GPS,
  • set geolocation,
  • enable/disable accessibility,
  • change the app language,
  • collect and parse the logcat output.

(see more about the Device class).

Features screenshotting

If you develop an application that is available across the world, you have to localize it into different languages. When UI is localized, it’s important for the translator to see the context of a word or a phrase, that is the specific screen.

With Kaspresso, translators can automatically take a screenshot of any screen. It’s incredibly fast, even for legacy screens, and you don't have to refactor or mock anything (see the manual).

Configurability

You can tune any part of Kaspresso (read more).

Robolectric support

You can run your UI-tests on the JVM environment. Additionally, almost all interceptors improving stability, readability and other will work. Read more.

Allure support

Kaspresso can generate very detailed Allure-reports for each test: More information is available here.

Jetpack Compose support

Now, you can write your Kaspresso tests for Jetpack Compose screens! DSL and all principles are the same. So, you will not see any difference between tests for View screens and for Compose screens. More information is available here.

Samples

All samples are available in the samples folder.

Most of the samples require AdbServer. To start AdbServer you should do the following steps:

  1. Go to the Kaspresso folder
cd ~/Workspace/Kaspresso
  1. Start adbserver-desktop.jar
java -jar artifacts/adbserver-desktop.jar

Runner

If you looking for a Runner to execute your UI tests we strongly recommend to use Marathon. Marathon is a fast, platform-independent test runner focused on performance and stability. It offers easy to use platform implementations for Android and iOS as well as an API for use with custom hardware farms and more techstacks. Marathon

Existing issues

All existing issues in Kaspresso can be found here.

Breaking changes

Breaking changes can be found here

Contribution

Kaspresso is an open source project, so you are welcome to contribute (see the Contribution Guidelines).

License

Kaspresso is available under the Apache License, Version 2.0.

More Repositories

1

TinyCheck

TinyCheck allows you to easily capture network communications from a smartphone or any device which can be associated to a Wi-Fi access point in order to quickly analyze them. This can be used to check if any suspect or malicious communication is outgoing from a smartphone, by using heuristics or specific Indicators of Compromise (IoCs). In order to make it working, you need a computer with a Debian-like operating system and two Wi-Fi interfaces. The best choice is to use a Raspberry Pi (2+) a Wi-Fi dongle and a small touch screen. This tiny configuration (for less than $50) allows you to tap any Wi-Fi device, anywhere.
Python
3,077
star
2

klara

Kaspersky's GReAT KLara
PHP
689
star
3

triangle_check

Python
509
star
4

iShutdown

Python
389
star
5

AdbServer

Adb Server for Espresso tests
Kotlin
119
star
6

ForensicsTools

Tools for DFIR
C++
117
star
7

VBscriptInternals

Scripts for disassembling VBScript p-code in the memory to aid in exploits analysis
Python
84
star
8

Apihashes

IDA Pro plugin for recognizing known hashes of API function names
Python
81
star
9

hrtng

C++
71
star
10

ActionScript3

Tools for static and dynamic analysis of ActionScript3 SWF files.
Python
45
star
11

BuildMigrator

C
34
star
12

uif

Integration Platform to build UI and Web Services
TypeScript
33
star
13

WinDbg-JS-Scripts

JavaScript
32
star
14

xtraining-re101

Code snippets for Reverse engineering training for xtraining platform
C
30
star
15

bitscout

Shell
20
star
16

OpenTIP-scanner

Open-source file scanner that sends requests and optionally uploads files to OpenTIP.kaspersky.com.
Python
17
star
17

Articles

C++
16
star
18

SafeBoard

Repository for general info and code samples for test tasks used in SafeBoard Hackatons in Kaspersky Lab.
C++
15
star
19

klogga

Opinionated logging-audit-tracing library. Data collected via klogga can be configured to be exported to different sources, including traditional text logs, but with emphasis on structured storages, primarily time-series databases and Open Telemetry.
Go
12
star
20

hb_dec

C
11
star
21

threat-intelligence

A repository dedicated to deliver a comprehensive set of tools for integration and convenient use of Kaspersky Threat Intelligence services
Python
9
star
22

grpc-kos

Shared C [core library], C++, Ruby, Python, PHP, C# (core library based), Objective-C
C++
4
star
23

RAM

Framework to manage the product state and configuration
Python
4
star
24

protobuf-kos

Protocol Buffers (a.k.a., protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data.
C++
3
star
25

abseil-cpp-kos

Abseil is an open source collection of C++ libraries drawn from the most fundamental pieces of Google’s internal codebase.
C++
2
star
26

c-ares-kos

c-ares is a C library for asynchronous DNS requests (including name resolves)
C++
1
star
27

boringssl-kos

BoringSSL is a fork of OpenSSL that is designed to meet Google's needs.
C++
1
star