0

我是初学者,所以请多多包涵。

我有以下代码:

{-# 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 会话(startMyBrowser),启动一个 Web 服务器(该scottyT部分),并且在 Web 服务器停止后它应该结束 Selenium 会话(stopMyBrowser)。

在摆弄了类型之后,我得到了上面的代码,似乎我只错过了一件 - 洞。

请,如果您成功了,请尝试解释您的解决方案和/或添加一些指向更多材料的链接。我很想了解那些该死的变形金刚。

编辑1:这是错误:

  • 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)
4

1 回答 1

0

reddit 上的 ForTheFunctionGod好心人回答了,这里是:

你的问题是:

lift $ scottyT 3000 _ routes

由于返回类型scottyT

MonadIO n => n

你不需要举起它——它可以适合任何可以执行 IO 操作的 monad。WD就是这样一个单子——注意它的MonadIO实例。scottyT如果它的返回类型是简单的,你只需要提升IO

MonadIO诸如,等类型的类MonadState主要消除了手动提升计算的需要。如果您想将一个函数IO Int嵌入到StateT s IO Int中,则需要提升一个函数来键入,而您不需要提升一个 type 的函数MonadIO m => m a,因为StateT s IO已经是 的一个实例MonadIO,所以m可以实例化它。

至于孔:它必须是类型

WD Response -> IO Response

完成这项工作的唯一方法是使用runSession它的朋友之一。我不太了解 Selenium,但是,大概,您可以重新使用您已经打开的会话的 ID:

runWD :: WDSession -> WD a -> IO a

startServer = startMyBrowser $ do
   sessionID <- getSession
   scottyT 3000 (runWD sessionID) routes
   stopMyBrowser

我还没有尝试过,但类型应该检查出来。我希望它有帮助!


它确实做到了:)。

于 2017-02-04T08:10:35.553 回答