An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

![Test](https://github.com/steinfletcher/apitest-css-selector/workflows/Test/badge.svg)

# 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
})).
```