簡體   English   中英

如何結合WebDriver和Scotty Monad

[英]How to combine WebDriver and Scotty monads

我是初學者,所以請多多包涵。

我有以下代碼:

{-# 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)

reddit上的好心人ForTheFunctionGod 回答了它 ,這是:

你有問題:

lift $ scottyT 3000 _ routes

由於scottyT的返回類型為

MonadIO n => n

您無需提起它-它可以放入任何可以執行IO操作的monad中。 WD是這樣的MonadIO請注意其MonadIO實例。 如果scottyT的返回類型是簡單的IO則只需要提升它即可。

諸如MonadIOMonadState等之類的類型類大多數不需要手動進行計算。 而你需要解除鍵入功能IO Int ,如果你想把它嵌入到StateT s IO Int ,你就不需要解除類型的函數MonadIO m => ma ,因為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

我還沒有嘗試過,但是類型應該簽出。 希望對您有所幫助!


它確實做到了:)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM