Selenium locators for Java/Kotlin that resemble the Testing Library (testing-library.com).

Overview

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 ids, classes, and similar. I'm a fan of the Testing Library because it encourages "testing as a user":

The more your tests resemble the way your software is used, the more confidence they can give you.

ℹ️ To set it up, pick the library's latest version at Maven Central. Then, copy the declaration for your build tool.


These are just a few examples. There are unit tests that illustrate all the usages.

Core API

The core API contains the selectors which are mapped into Selenium locators:

driver.findElements(ByAltText("first name"))
driver.findElements(ByDisplayValue("/john/i", matchTextBy = REGEX))
val active = driver.findElements(ByLabelText("active"))
val input = driver.findElements(ByPlaceholderText("first name", exact = false))
val firstName = input.text
driver.findElements(ByRole("heading", name = "/as a user/i", matchTextBy = REGEX))
val panel = driver.findElements(ByTestId("test-id"))
panel.click()
driver.findElements(ByText("present", exact = false, selector = "span"))
driver.findElements(ByTitle("title 1"))

An alternative API that does not use Selenium locators:

val result1 = driver.queryBy(AltText, "alt 1", mapOf("exact" to false))
val result2 = driver.getBy(DisplayValue, "incredibles")
val result3 = driver.queryAllBy(LabelText, "label x")
val result4 = driver.getAllBy(Role, "listbox")

ℹ️ All the Testing Library core functionality is available.

User Interactions

The Testing Library's user-event is also mapped:

driver.user.click(active)
driver.user.dblClick(panel)
driver.user.type(input, "foobar")
driver.user.selectOptions(driver.findElement(ByRole("listbox")), "C")

ℹ️ user-event's JavaScript only loads if it's used.


ℹ️ Want to know about the creation process? Read more about it at The Testing Library meets Selenium.

You might also like...
Android UI Testing
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

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

Morsa: Jetpack Compose UI Testing Framework

Morsa: Jetpack Compose UI Testing Framework Test library to ease UI testing with Jetpack Compose Purpose This library aims to add some useful wrappers

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

Comments
  • ByRole: can't use regex in name field

    ByRole: can't use regex in name field

    The example from the readme driver.findElements(ByRole("heading", name = "/as a user/i", matchTextBy = REGEX)) doesn't work.

    The matchTextBy param is applied to the role parameter and not to name, so the resulting call will be (heading, { name: '/as a user/i' }) instead of (heading, { name: /as a user/i })

    Test to reproduce this:

        @Test
        fun `role with regex in name parameter`() {
            driver.render(
                    """<h1>something as a user something</h1>"""
            )
    
            val result = driver.findElements(ByRole("heading", name = "/as a user/i", matchTextBy = REGEX))
    
            assertEquals(expected = "something as a user something", result.single().accessibleName)
        }
    
    opened by jmerkle 6
  • Can't map jest-dom matchers that receive parameters

    Can't map jest-dom matchers that receive parameters

    this part of the API - jest-dom - has hit a roadblock as I try to be able to execute jest-dom as a standalone so I can execute it from Selenium. more info

    opened by lsoares 0
Owner
Luís Soares
Luís Soares
Selenium WebDriver and Appium based Web, Mobile (Android, iOS) and Windows desktop Automation Framework with BDD & Non-BDD implementation support

Selenium WebDriver and Appium based Web, Mobile (Android, iOS) and Windows desktop Automation Framework with BDD & Non-BDD implementation support

null 10 Oct 14, 2022
Powerful, elegant and flexible test framework for Kotlin with additional assertions, property testing and data driven testing

Kotest is a flexible and comprehensive testing tool for Kotlin with multiplatform support. To learn more about Kotest, visit kotest.io or see our quic

Kotest 3.7k Nov 21, 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 Nov 20, 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 Nov 20, 2022
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.

Appmattus Limited 186 Nov 12, 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
Turbine is a small testing library for kotlinx.coroutines Flow.

A small testing library for kotlinx.coroutines Flow

Cash App 1.7k Nov 22, 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 Nov 10, 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 Nov 12, 2022
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 Nov 20, 2022