How to combine WebDriver and Scotty monads
我是初学者,所以请多包涵。
我有以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | {-# LANGUAGE OverloadedStrings #-} module Lib where import Control.Monad.IO.Class import Control.Monad.Trans.Class import Data.Monoid ((<>)) import qualified Data.Text as T import qualified Data.Text.Lazy as TL import Test.WebDriver --import Web.Scotty import Web.Scotty.Trans firefoxConfig :: WDConfig firefoxConfig = defaultConfig startMyBrowser :: WD a -> IO a startMyBrowser = runSession firefoxConfig stopMyBrowser = closeSession someFunc :: WD String someFunc = do openPage"http://maslo.cz" captionElem <- findElem (ByCSS"h2") text <- getText captionElem return $ T.unpack text helloAction :: ActionT TL.Text WD () helloAction = do a <- lift someFunc text $"got this for you:" <> TL.pack a routes :: ScottyT TL.Text WD () routes = get"/hello" helloAction startServer = startMyBrowser $ do lift $ scottyT 3000 _ routes stopMyBrowser |
我不确定那些填充部分是否正确-应该启动Selenium会话(
摆弄类型后,我到达了上面的代码,似乎我只错过了一个-洞。
请,如果您可以使用它,请尝试解释您的解决方案和/或添加一些指向更多材料的链接。我很想了解那些该死的变压器。
编辑1:
错误如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | ? Couldn't match type ‘t0 m0’ with ‘WD’ Expected type: WD () Actual type: t0 m0 () ? In a stmt of a 'do' block: lift $ scottyT 3000 _ routes In the second argument of ‘($)’, namely ‘do { lift $ scottyT 3000 _ routes; stopMyBrowser }’ In the expression: startMyBrowser $ do { lift $ scottyT 3000 _ routes; stopMyBrowser } ? Found hole: _ :: WD wai-3.2.1.1:Network.Wai.Internal.Response -> IO wai-3.2.1.1:Network.Wai.Internal.Response ? In the second argument of ‘scottyT’, namely ‘_’ In the second argument of ‘($)’, namely ‘scottyT 3000 _ routes’ In a stmt of a 'do' block: lift $ scottyT 3000 _ routes ? Relevant bindings include startServer :: IO () (bound at src/Lib.hs:37:1) |
reddit上的好心人ForTheFunctionGod回答了它,这是:
您的问题是:
1 | lift $ scottyT 3000 _ routes |
由于
1 | MonadIO n => n |
您不需要抬起它-它可以放入可以执行IO操作的任何monad中。
类型类,例如
关于孔:它必须是
类型
1 | WD Response -> IO Response |
进行此工作的唯一方法是使用
1 2 3 4 5 6 | runWD :: WDSession -> WD a -> IO a startServer = startMyBrowser $ do sessionID <- getSession scottyT 3000 (runWD sessionID) routes stopMyBrowser |
我还没有尝试过,但是类型应该签出。希望对您有所帮助!
它确实做到了:)。