Skip to content

raphiz/spring-playwright-feature-specs

Repository files navigation

Spring Playwright Feature Specs

Spring Playwright Feature Specs is a small, opinionated library to write readable feature specs for your Spring web applications in Kotlin.

It's just a bit of setup code and syntactic sugar.

This style of testing is inspired by Ruby's Capybara with RSpec. Read more about testing with feature specs in the Testing Rails book.

Example Usage

Annotate your test class with @FeatureSpec. This will add the @SpringBootTest and enable the jUnit 5 SpringWithPlaywrightExtension, which will prepare a real browser (chromium) for the tests.

Each test method (annotated with jUnits @Test) uses the withPage helper function. This function opens up a new browser context for each test and provides access to playwrights Page interface.

@FeatureSpec
class UserCreatesRecipe {

    @Test
    fun `they see the page for the created recipe`() = withPage {
        val recipeTitle = "Chicken Masala"

        navigate("/")

        getByText("Create new Recipe").click()

        getByLabel("Recipe Title").fill(recipeTitle)

        getByText("Submit!").click()

        assertThat(locator("body")).containsText(recipeTitle)
    }

    @Test
    fun `they see a useful error message when the form is invalid`() = withPage {
        navigate("/")

        getByText("Create new Recipe").click()

        getByText("Submit!").click()

        assertThat(locator("body")).containsText("title cannot be blank")
    }

}

Here is a full minimal working example.

Install

Just add spring-playwright-feature-specs as a test dependency to you project.

When using Gradle, I highly recommend to create a separate test suite for feature spec tests.

Gradle Kotlin DSL

testImplementation("io.github.raphiz:spring-playwright-feature-specs:0.2.0")

Gradle

testImplementation 'io.github.raphiz:spring-playwright-feature-specs:0.2.0'

Maven

<dependency>
  <groupId>io.github.raphiz</groupId>
  <artifactId>spring-playwright-feature-specs</artifactId>
  <version>0.2.0</version>
</dependency>

Configuration

The scope of this project is very small and not intended to be configurable.

If you have different needs, consider implementing your own version of the withPage function or the @FeatureSpec annotation. If you need even more, it's probably best to integrate this code into your own code base.

Contribution

If you have any suggestions, feedback or questions, feel free to open a GitHub issue.

About

A small, opinionated library to write readable feature specs for your Spring web applications in Kotlin.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages