module Web.Api.WebDriver.Helpers (
stashCookies
, loadCookies
, press
, typeString
) where
import qualified Data.Aeson as Aeson
( encode )
import qualified Data.ByteString.Lazy.Char8 as BS
( pack )
import qualified Data.Digest.Pure.SHA as SHA
( showDigest, sha1 )
import Web.Api.WebDriver.Endpoints
import Web.Api.WebDriver.Monad
import Web.Api.WebDriver.Types
import Web.Api.WebDriver.Types.Keyboard
stashCookies
:: (Monad m)
=> String
-> WebDriverT m ()
stashCookies string =
let file = SHA.showDigest $ SHA.sha1 $ BS.pack string in
getAllCookies >>= writeCookieFile file
loadCookies
:: (Monad m)
=> String
-> WebDriverT m Bool
loadCookies string = do
let file = SHA.showDigest $ SHA.sha1 $ BS.pack string
contents <- readCookieFile file
case contents of
Nothing -> return False
Just cs -> do
mapM_ addCookie cs
return True
writeCookieFile
:: (Monad m)
=> FilePath
-> [Cookie]
-> WebDriverT m ()
writeCookieFile file cookies = do
path <- fromEnv (_dataPath . _env)
let fullpath = path ++ "/secrets/cookies/" ++ file
writeFilePath fullpath (Aeson.encode cookies)
readCookieFile
:: (Monad m)
=> FilePath
-> WebDriverT m (Maybe [Cookie])
readCookieFile file = do
path <- fromEnv (_dataPath . _env)
let fullpath = path ++ "/secrets/cookies/" ++ file
cookieFileExists <- fileExists fullpath
if cookieFileExists
then readFilePath fullpath
>>= parseJson
>>= constructFromJson
>>= mapM constructFromJson
>>= (return . Just)
else return Nothing
keypress :: Char -> ActionItem
keypress x = emptyActionItem
{ _actionType = Just KeyDownAction
, _actionValue = Just [x]
}
press :: Key -> Action
press key = emptyAction
{ _inputSourceType = Just KeyInputSource
, _inputSourceId = Just "kbd"
, _actionItems = [keypress (keyToChar key)]
}
typeString :: String -> Action
typeString x = emptyAction
{ _inputSourceType = Just KeyInputSource
, _inputSourceId = Just "kbd"
, _actionItems = map keypress x
}