module Test.WebDriver.Server (
withServer,
defaultSettings,
ServerLocation(..),
ServerSettings(..)
) where
import Control.Exception
import Network
import System.IO
import System.Process
import qualified Test.WebDriver.Server.Download as Download
import Test.WebDriver.Server.Poll
data ServerLocation = Local | Remote HostName PortID
deriving Show
data ServerSettings = ServerSettings
{ serverLocation :: ServerLocation
, jarLocation :: Maybe FilePath
, serverVersion :: Maybe String
} deriving Show
defaultSettings :: ServerSettings
defaultSettings = ServerSettings Local Nothing Nothing
withServer :: ServerSettings -> IO a -> IO a
withServer ss act = case serverLocation ss of
Remote{} -> error "not handled yet"
Local -> do
jarfile <- case jarLocation ss of
Nothing -> Download.downloadJar (serverVersion ss)
Just f -> return f
blackHole <- openFile
#ifdef WINDOWS
"NUL"
#else
"/dev/null"
#endif
AppendMode
bracket (do (_,_,_,ch) <- createProcess
(proc "java" ["-jar", jarfile, "-port", "4444"])
{ std_in = UseHandle blackHole
, std_out = UseHandle blackHole
, std_err = CreatePipe }
waitForServer
return ch)
(\h -> terminateProcess h >> hClose blackHole)
(const act)