Selecting HTML elements.
Matches elements that have the given tag.
import Html
import Html.Attributes as Attr
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (tag, text)
test "the welcome <h1> says hello!" <|
\() ->
Html.div []
[ Html.h1 [ Attr.id "welcome" ] [ Html.text "Hello!" ] ]
|> Query.fromHtml
|> Query.find [ tag "h1" ]
|> Query.has [ text "Hello!" ]Matches elements whose descendants match the given selectors.
(You will get the element and not the descendant.)
This is especially useful to find elements which contain specific text somewhere in their descendants.
import Html
import Html.Events exposing (onClick)
import Test exposing (test)
import Test.Html.Event as Event
import Test.Html.Query as Query
import Test.Html.Selector exposing (containing, tag)
test : Test
test =
test "..." <|
Html.div []
[ Html.button [ onClick NopeMsg ] [ Html.text "not me" ]
, Html.button [ onClick ClickedMsg ] [ Html.text "click me" ]
]
|> Query.fromHtml
|> Query.find
[ tag "button"
, containing [ text "click me" ]
]
|> Event.simulate Event.click
|> Event.expect ClickedMsgMatches elements that have the given attribute in a way that makes sense
given their semantics in Html.
Combine the given selectors into one which requires all of them to match.
import Html
import Html.Attributes as Attr
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (class, text, all, Selector)
replyBtnSelector : Selector
replyBtnSelector =
all [ class "btn", text "Reply" ]
test "Button has the class 'btn' and the text 'Reply'" <|
\() ->
Html.button [ Attr.class "btn btn-large" ] [ Html.text "Reply" ]
|> Query.fromHtml
|> Query.has [ replyBtnSelector ]Matches elements that have the given id attribute.
import Html
import Html.Attributes as Attr
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (id, text)
test "the welcome <h1> says hello!" <|
\() ->
Html.div []
[ Html.h1 [ Attr.id "welcome" ] [ Html.text "Hello!" ] ]
|> Query.fromHtml
|> Query.find [ id "welcome" ]
|> Query.has [ text "Hello!" ]Matches elements that have the given class (and possibly others as well).
To match multiple classes at once, use classes instead.
To match the element's exact class attribute string, use exactClassName.
import Html
import Html.Attributes as Attr
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (class)
test "Button has the class btn-large" <|
\() ->
Html.button [ Attr.class "btn btn-large" ] [ Html.text "Reply" ]
|> Query.fromHtml
|> Query.has [ class "btn-large" ]Matches elements that have all the given classes (and possibly others as well).
When you only care about one class instead of several, you can use
class instead of passing this function a list with one value in it.
To match the element's exact class attribute string, use exactClassName.
import Html
import Html.Attributes as Attr
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (classes)
test "Button has the classes btn and btn-large" <|
\() ->
Html.button [ Attr.class "btn btn-large" ] [ Html.text "Reply" ]
|> Query.fromHtml
|> Query.has [ classes [ "btn", "btn-large" ] ]Matches the element's exact class attribute string.
This is used less often than class, classes or
attribute, which check for the presence of a class as opposed
to matching the entire class attribute exactly.
import Html
import Html.Attributes as Attr
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (exactClassName)
test "Button has the exact class 'btn btn-large'" <|
\() ->
Html.button [ Attr.class "btn btn-large" ] [ Html.text "Reply" ]
|> Query.fromHtml
|> Query.has [ exactClassName "btn btn-large" ]Matches elements that have the given style properties (and possibly others as well).
import Html
import Html.Attributes as Attr
import Test.Html.Query as Query
import Test exposing (test)
import Test.Html.Selector exposing (classes)
test "the Reply button has red text" <|
\() ->
Html.div []
[ Html.button
[ Attr.style "color" "red" ]
[ Html.text "Reply" ]
]
|> Query.has [ style "color" "red" ]
A selector used to filter sets of elements.