https://github.com/steinfletcher/apitest-css-selector
CSS selectors for apitest. Useful for asserting on HTML in a HTTP response
https://github.com/steinfletcher/apitest-css-selector
Last synced: about 1 year ago
JSON representation
CSS selectors for apitest. Useful for asserting on HTML in a HTTP response
- Host: GitHub
- URL: https://github.com/steinfletcher/apitest-css-selector
- Owner: steinfletcher
- License: mit
- Created: 2020-03-21T14:33:37.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-10-19T14:53:48.000Z (over 3 years ago)
- Last Synced: 2025-02-02T10:37:30.093Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 30.3 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# apitest-css-selector
Assertions for [apitest](https://github.com/steinfletcher/apitest) using css selectors.
## Examples
### `selector.TextExists`
```go
apitest.New().
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`
My document
Header
Some text to match on
`,
))
w.WriteHeader(http.StatusOK)
}).
Get("/").
Expect(t).
Status(http.StatusOK).
Assert(selector.TextExists("Some text to match on")).
End()
```
### `selector.ContainsTextValue`
If you are selecting a data test id, a convenience method is provided to simplify the query.
```go
apitest.New().
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`
My document
some content
`,
))
w.WriteHeader(http.StatusOK)
}).
Get("/").
Expect(t).
Status(http.StatusOK).
Assert(selector.ContainsTextValue(selector.DataTestID("some-test-id"), "some content")).
End()
```
### `selector.FirstTextValue`
```go
apitest.New().
Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`
content`))
w.WriteHeader(http.StatusOK)
})).
Get("/").
Expect(t).
Status(http.StatusOK).
Assert(selector.FirstTextValue(`.myClass`, "content")).
End()
```
see also `selector.NthTextValue` and `selector.ContainsTextValue`
### `selector.Exists` `selector.NotExists`
```go
apitest.New().
Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(
`
text
text
text
`))
w.WriteHeader(http.StatusOK)
})).
Get("/").
Expect(t).
Status(http.StatusOK).
Assert(selector.Exists(".myClass", `div[data-test-id^="product-"]`, "#myId")).
Assert(selector.NotExists("#notExists")).
End()
```
### `selector.Selection`
This exposes `goquery`'s Selection api and offers more flexibility over the previous methods
```go
Assert(selector.Selection(".outerClass", func(selection *goquery.Selection) error {
if test.expectedText != selection.Find(".innerClass").Text() {
return fmt.Errorf("text did not match")
}
return nil
})).
```