From 49e7dbe71e7ab35adc22b87e343b897f3f778d29 Mon Sep 17 00:00:00 2001 From: Stanislav Smirnov Date: Tue, 27 Aug 2024 09:53:33 +0300 Subject: [PATCH] Yesod test add select by label (#1845) * Remove some redundants in yesod-test * Code optimizations * Add selectByLabel --- yesod-test/ChangeLog.md | 4 +++ yesod-test/Yesod/Test.hs | 68 +++++++++++++++++++++++++++++-------- yesod-test/test/main.hs | 25 ++++++++++++-- yesod-test/yesod-test.cabal | 2 +- 4 files changed, 82 insertions(+), 17 deletions(-) diff --git a/yesod-test/ChangeLog.md b/yesod-test/ChangeLog.md index d64364d2d..8fcbf1839 100644 --- a/yesod-test/ChangeLog.md +++ b/yesod-test/ChangeLog.md @@ -1,5 +1,9 @@ # ChangeLog for yesod-test +## 1.6.19 + +* Add `selectByLabel` to yesod-test. [#1845](https://github.com/yesodweb/yesod/pull/1845) + ## 1.6.18 * Add `checkByLabel` to yesod-test. [#1843](https://github.com/yesodweb/yesod/pull/1843) diff --git a/yesod-test/Yesod/Test.hs b/yesod-test/Yesod/Test.hs index 67183b636..968345520 100644 --- a/yesod-test/Yesod/Test.hs +++ b/yesod-test/Yesod/Test.hs @@ -179,6 +179,7 @@ module Yesod.Test , fileByLabelSuffix , chooseByLabel , checkByLabel + , selectByLabel -- *** CSRF Tokens -- | In order to prevent CSRF exploits, yesod-form adds a hidden input @@ -267,7 +268,6 @@ import Data.Time.Clock (getCurrentTime) import Control.Applicative ((<$>)) import Text.Show.Pretty (ppShow) import Data.Monoid (mempty) -import Data.Semigroup (Semigroup(..)) #if MIN_VERSION_base(4,9,0) import GHC.Stack (HasCallStack) #elif MIN_VERSION_base(4,8,1) @@ -279,7 +279,7 @@ type HasCallStack = (() :: Constraint) #endif import Data.ByteArray.Encoding (convertToBase, Base(..)) import Network.HTTP.Types.Header (hContentType) -import Data.Aeson (FromJSON, eitherDecode') +import Data.Aeson (eitherDecode') import Control.Monad (unless) import Yesod.Test.Internal (getBodyTextPreview, contentTypeHeaderIsUtf8) @@ -910,12 +910,7 @@ genericNameFromLabel match label = do -- 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 + body <- htmlBody "genericNameSelectorFromLabel" html <- case findBySelector body selector of Left parseError -> failure $ "genericNameFromSelectorLabel: Parse error" <> T.pack parseError @@ -1751,16 +1746,52 @@ checkByLabel label = do value <- genericValueFromLabel (==) label addPostParam name value +-- | Finds the @\