diff --git a/yesod-test/ChangeLog.md b/yesod-test/ChangeLog.md index 7188ca92..8fa21c84 100644 --- a/yesod-test/ChangeLog.md +++ b/yesod-test/ChangeLog.md @@ -1,5 +1,10 @@ # ChangeLog for yesod-test + +## 1.6.15 + +* Add `bySelectorLabelContain`. [#1781](https://github.com/yesodweb/yesod/pull/1781) + ## 1.6.14 * Fix quotes not matching in htmlContain* functions [#1768](https://github.com/yesodweb/yesod/pull/1768). diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index 8c580060..91884608 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -170,6 +170,7 @@ module Yesod.Test , byLabelContain , byLabelPrefix , byLabelSuffix + , bySelectorLabelContain , fileByLabel , fileByLabelExact , fileByLabelContain @@ -876,9 +877,36 @@ genericNameFromLabel match label = do case mres of Nothing -> failure "genericNameFromLabel: No response available" Just res -> return res + let body = simpleBody res + case genericNameFromHTML match label body of + Left e -> failure e + Right x -> pure x + +-- | +-- This looks up the name of a field based on a CSS selector and the contents of the label pointing to it. +genericNameFromSelectorLabel :: HasCallStack => (T.Text -> T.Text -> Bool) -> T.Text -> T.Text -> RequestBuilder site T.Text +genericNameFromSelectorLabel match selector label = do + mres <- fmap rbdResponse getSIO + res <- + case mres of + Nothing -> failure "genericNameSelectorFromLabel: No response available" + Just res -> return res + let body = simpleBody res + html <- + case findBySelector body selector of + Left parseError -> failure $ "genericNameFromSelectorLabel: Parse error" <> T.pack parseError + Right [] -> failure $ "genericNameFromSelectorLabel: No fragments match selector " <> selector + Right [matchingFragment] -> pure $ BSL8.pack matchingFragment + Right _matchingFragments -> failure $ "genericNameFromSelectorLabel: Multiple fragments match selector " <> selector + case genericNameFromHTML match label html of + Left e -> failure e + Right x -> pure x + +genericNameFromHTML :: (T.Text -> T.Text -> Bool) -> T.Text -> HtmlLBS -> Either T.Text T.Text +genericNameFromHTML match label html = let - body = simpleBody res - mlabel = parseHTML body + parsedHTML = parseHTML html + mlabel = parsedHTML $// C.element "label" >=> isContentMatch label mfor = mlabel >>= attribute "for" @@ -887,26 +915,26 @@ genericNameFromLabel match label = do | x `match` T.concat (c $// content) = [c] | otherwise = [] - case mfor of + in case mfor of for:[] -> do - let mname = parseHTML body + let mname = parsedHTML $// attributeIs "id" for >=> attribute "name" case mname of - "":_ -> failure $ T.concat + "":_ -> Left $ T.concat [ "Label " , label , " resolved to id " , for , " which was not found. " ] - name:_ -> return name - [] -> failure $ "No input with id " <> for + name:_ -> Right name + [] -> Left $ "No input with id " <> for [] -> case filter (/= "") $ mlabel >>= (child >=> C.element "input" >=> attribute "name") of - [] -> failure $ "No label contained: " <> label - name:_ -> return name - _ -> failure $ "More than one label contained " <> label + [] -> Left $ "No label contained: " <> label + name:_ -> Right name + _ -> Left $ "More than one label contained " <> label byLabelWithMatch :: (T.Text -> T.Text -> Bool) -- ^ The matching method which is used to find labels (i.e. exact, contains) -> T.Text -- ^ The text contained in the @\