Skip to content

lsoares/selenium-testing-library

Repository files navigation

Selenium Testing Library

.github/workflows/test.yml Download

The goal is to provide the Testing Library selectors as Selenium locators. The reason is that when I use Selenium, I want to be independent of ids, classes, and similar. I'm a fan of the Testing Library because it encourages "testing as a user". Read more.

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

To get started, use the library's latest version:

implementation("com.luissoares:selenium-testing-library:4.1.3")

Now you can use the library:

  • Core API contains the selectors which are mapped into Selenium locators:

    driver.findElement(altText("first name")) // or findElements
    driver.findElement(displayValue(JsFunction("c => c.startsWith('selen')")))
    driver.findElement(labelText("active"))
    driver.findElement(placeholderText("first name", exact = false))
    driver.findElement(role(Heading, nameAsFunction = JsFunction("c => c.startsWith('something')")))
    driver.findElement(role(Button, nameAsRegex = Pattern.compile("confirm")))
    driver.findElement(testId("test-id"))
    driver.findElement(text("present", exact = false, selector = "span"))
    driver.findElement(title("title 1"))
    driver.findElement(title(Pattern.compile("FOO")))
    // fluent API alternative (useful in Java):
    driver.findElement(text("I accept").exact(true).ignore(false).selector("span"))
    driver.findElement(role(Heading).name(Pattern.compile("Something", CASE_INSENSITIVE)).level(1))
    

    see all examples

  • user-event triggers events from user interactions:

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

    see all examples

  • fireEvent is a lower-level way to trigger events:

    input.fireEvent(Change, mapOf(Target to mapOf("value" to "2020-05-24")))
  • jest-dom matchers are available indirectly:

    val formValues = registrationForm.formValues
    val userAgrees = checkboxMarketing.isChecked
    val name = element.accessibleName
    val displayedValue = element.displayValue

    see all examples