2017-02-02 17 views
0

私は初心者ですので、私にご負担ください。WebDriverとScottyのモナドを組み合わせる方法

私は次のコードを持っている:私もそれらの満たされた部分が正しいかどうかわからないのです

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

を - ウェブサーバ(scottyT一部をスピンアップ、セレンセッション(startMyBrowser)を開始することになっています)、Webサーバーが停止したら、Seleniumセッション(stopMyBrowser)を終了する必要があります。

タイプで手を加えた後、私は上記のコードに到達しました。そして、私は1つだけ欠けているようです - 穴。

解決策を説明したり、さらに多くの資料へのリンクを追加したりしてください。私はそれらの変圧器を理解するのが大好きです。

編集1:ここでは はエラーは以下のとおりです。redditのanswered it

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

答えて

0

種類の男ForTheFunctionGod、ここにある:

あなたの問題があります:

lift $ scottyT 3000 _ routes 

戻り値の型ので、 scottyTは、

MonadIO n => n 

持ち上げる必要はありません.IOアクションを実行できるすべてのモナドに適合します。 WDはそのようなモナドノートです。そのMonadIOインスタンスです。戻り値の型が単純IOの場合は、scottyTを持ち上げるだけです。

MonadIO,MonadStateなどの種類のクラスは、手動で計算を持ち上げる必要性をほとんど排除します。 StateT s IO Intに埋め込みたい場合は、IO Intを入力する必要がありますが、StateT s IOはすでにMonadIOのインスタンスであるため、MonadIO m => m aの関数を持ち上げる必要はないため、mをインスタンス化することができます。穴用として

:それは、この作品を作るための唯一の方法はrunSessionやその友人のいずれかを使用することであるタイプ

WD Response -> IO Response 

でなければなりません。私はよくセレンを知らない、しかし、おそらく、あなたはすでに開かれたセッションのIDを再使用することができます。

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

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

私はこれを試していないが、種類がチェックアウトする必要があります。私はそれが助けて欲しい!


間違いなく:)。

関連する問題