|
3 | 3 | [](https://github.com/lsoares/selenium-testing-library/actions/workflows/test.yml)
|
4 | 4 | [](https://search.maven.org/artifact/com.luissoares/selenium-testing-library)
|
5 | 5 |
|
6 |
| -It brings the [Testing Library](http://testing-library.com) selectors as Selenium locators. |
7 |
| -Why? When I use Selenium, I want to be independent of ids, classes, and similar. |
8 |
| -I'm a fan of the Testing Library because it encourages "testing as a user": |
| 6 | +The goal is to provide the [Testing Library](http://testing-library.com) selectors as Selenium locators. |
| 7 | +The reason is that when I use Selenium, I want to be independent of ids, classes, and similar. |
| 8 | +I'm a fan of the Testing Library because it encourages "testing as a |
| 9 | +user". [Read more](https://medium.com/codex/the-testing-library-meets-selenium-5f74cc712114). |
9 | 10 |
|
10 | 11 | > The more your tests resemble the way your software is used,
|
11 | 12 | > the more confidence they can give you.
|
12 | 13 |
|
13 |
| -Use the [library's latest version](https://search.maven.org/artifact/com.luissoares/selenium-testing-library): |
| 14 | +To get started, use |
| 15 | +the [library's latest version](https://search.maven.org/artifact/com.luissoares/selenium-testing-library): |
14 | 16 |
|
15 | 17 | ```kotlin
|
16 | 18 | implementation("com.luissoares:selenium-testing-library:4.1.1")
|
17 | 19 | ```
|
18 | 20 |
|
19 |
| ---- |
20 |
| -These are just a few examples. Check [the tests](/lib/src/test/kotlin/seleniumtestinglib) |
| 21 | +Now you can use the library: |
| 22 | +- [Core API](https://testing-library.com/docs) contains the selectors which are mapped into Selenium locators: |
| 23 | + ```kotlin |
| 24 | + driver.findElement(altText("first name")) // or findElements |
| 25 | + driver.findElement(displayValue(JsFunction("c => c.startsWith('selen')"))) |
| 26 | + driver.findElement(labelText("active")) |
| 27 | + driver.findElement(placeholderText("first name", exact = false)) |
| 28 | + driver.findElement(role(Heading, nameAsFunction = JsFunction("c => c.startsWith('something')"))) |
| 29 | + driver.findElement(role(Button, nameAsRegex = Pattern.compile("confirm"))) |
| 30 | + driver.findElement(testId("test-id")) |
| 31 | + driver.findElement(text("present", exact = false, selector = "span")) |
| 32 | + driver.findElement(title("title 1")) |
| 33 | + driver.findElement(title(Pattern.compile("FOO"))) |
| 34 | + ``` |
| 35 | + |
| 36 | +- [user-event](https://testing-library.com/docs/user-event/intro) represents the User Interactions: |
| 37 | + ```kotlin |
| 38 | + driver.user.click(active) |
| 39 | + driver.user.dblClick(panel) |
| 40 | + driver.user.type(input, "foobar") |
| 41 | + driver.user.selectOptions(letterSelector, driver.findElement(ByRole(ListBox, name = "C"))) |
| 42 | + ``` |
| 43 | + |
| 44 | +- [fireEvent](https://testing-library.com/docs/dom-testing-library/api-events) is lower-level way to trigger events: |
| 45 | + ```kotlin |
| 46 | + input.fireEvent(Change, mapOf(Target to mapOf("value" to "2020-05-24"))) |
| 47 | + ``` |
| 48 | + |
| 49 | +- [jest-dom](https://testing-library.com/docs/ecosystem-jest-dom) is available indirectly: |
| 50 | + ```kotlin |
| 51 | + val formValues = registrationForm.formValues |
| 52 | + val userAgrees = checkboxMarketing.isChecked |
| 53 | + val name = element.accessibleName |
| 54 | + val displayedValue = element.displayValue |
| 55 | + ``` |
| 56 | + |
| 57 | +Check [the tests](/lib/src/test/kotlin/seleniumtestinglib) |
21 | 58 | that [illustrate](https://medium.com/codex/towards-self-documenting-code-371364bdccbb) all the usages
|
22 |
| -([there are examples in Java](/lib/src/test/java)). |
23 |
| - |
24 |
| -## Core API |
25 |
| - |
26 |
| -The [core API](https://testing-library.com/docs) contains the selectors which are mapped into Selenium locators: |
27 |
| - |
28 |
| -```kotlin |
29 |
| -driver.findElements(altText("first name")) |
30 |
| -driver.findElement(displayValue(JsFunction("c => c.startsWith('selen')"))) |
31 |
| -driver.findElements(labelText("active")) |
32 |
| -driver.findElements(placeholderText("first name", exact = false)) |
33 |
| -driver.findElements(role(Heading, nameAsFunction = JsFunction("c => c.startsWith('something')"))) |
34 |
| -driver.findElements(role(Button, nameAsRegex = Pattern.compile("confirm"))) |
35 |
| -driver.findElements(testId("test-id")) |
36 |
| -driver.findElements(text("present", exact = false, selector = "span")) |
37 |
| -driver.findElement(title("title 1")) |
38 |
| -driver.findElement(title(Pattern.compile("FOO"))) |
39 |
| -``` |
40 |
| - |
41 |
| -## User Interactions |
42 |
| - |
43 |
| -The Testing Library's [user-event](https://testing-library.com/docs/user-event/intro) is also mapped: |
44 |
| - |
45 |
| -```kotlin |
46 |
| -driver.user.click(active) |
47 |
| -driver.user.dblClick(panel) |
48 |
| -driver.user.type(input, "foobar") |
49 |
| -driver.user.selectOptions(letterSelector, driver.findElement(ByRole(ListBox, name = "C"))) |
50 |
| -``` |
51 |
| - |
52 |
| -[fireEvent](https://testing-library.com/docs/dom-testing-library/api-events) is also available: |
53 |
| - |
54 |
| -```kotlin |
55 |
| -input.fireEvent(Change, mapOf(Target to mapOf("value" to "2020-05-24"))) |
56 |
| -``` |
57 |
| - |
58 |
| -## jest-dom |
59 |
| - |
60 |
| -[jest-dom](https://testing-library.com/docs/ecosystem-jest-dom) matchers are available with a similar API, although it |
61 |
| -makes more sense to use the corresponding utilities and assert with JUnit (or an assertion library): |
62 |
| - |
63 |
| -```kotlin |
64 |
| -// API similar to the original version: |
65 |
| -expect(button.toHaveAccessibleDescription("Register")) |
66 |
| -expect(checkboxMarketing).toBeChecked() |
67 |
| -assertEquals(setOf("btn", "btn-danger", "extra"), deleteButton.classes) |
68 |
| -expect(element).not.toBePartiallyChecked() |
69 |
| - |
70 |
| -// utilities that can be used on their own: |
71 |
| -val formValues = registrationForm.formValues |
72 |
| -val userAgrees = checkboxMarketing.isChecked |
73 |
| -val name = element.accessibleName |
74 |
| -val displayedValue = element.displayValue |
75 |
| -``` |
76 |
| - |
77 |
| ---- |
78 |
| -ℹ️ _[Read more](https://medium.com/codex/the-testing-library-meets-selenium-5f74cc712114)._ |
| 59 | +([there are examples in Java as well](/lib/src/test/java)). |
0 commit comments