module Test.WebDriver.Commands.Internal
(
doCommand
, doSessCommand, SessionId(..)
, doElemCommand, Element(..)
, doWinCommand, WindowHandle(..), currentWindow
, NoSessionId(..)
) where
import Test.WebDriver.Class
import Test.WebDriver.Session
import Test.WebDriver.Utils (urlEncode)
import Data.Aeson
import Data.Aeson.Types
import Data.Text (Text)
import qualified Data.Text as T
import Control.Exception.Lifted
import Data.Typeable
import Data.Default.Class
import Control.Applicative
import Prelude
newtype Element = Element Text
deriving (Eq, Ord, Show, Read)
instance FromJSON Element where
parseJSON (Object o) = Element <$> o .: "ELEMENT"
parseJSON v = typeMismatch "Element" v
instance ToJSON Element where
toJSON (Element e) = object ["ELEMENT" .= e]
newtype WindowHandle = WindowHandle Text
deriving (Eq, Ord, Show, Read,
FromJSON, ToJSON)
instance Default WindowHandle where
def = currentWindow
currentWindow :: WindowHandle
currentWindow = WindowHandle "current"
instance Exception NoSessionId
newtype NoSessionId = NoSessionId String
deriving (Eq, Show, Typeable)
doSessCommand :: (WebDriver wd, ToJSON a, FromJSON b) =>
Method -> Text -> a -> wd b
doSessCommand method path args = do
WDSession { wdSessId = mSessId } <- getSession
case mSessId of
Nothing -> throwIO . NoSessionId $ msg
where
msg = "doSessCommand: No session ID found for relative URL "
++ show path
Just (SessionId sId) -> doCommand method
(T.concat ["/session/", urlEncode sId, path]) args
doElemCommand :: (WebDriver wd, ToJSON a, FromJSON b) =>
Method -> Element -> Text -> a -> wd b
doElemCommand m (Element e) path a =
doSessCommand m (T.concat ["/element/", urlEncode e, path]) a
doWinCommand :: (WebDriver wd, ToJSON a, FromJSON b) =>
Method -> WindowHandle -> Text -> a -> wd b
doWinCommand m (WindowHandle w) path a =
doSessCommand m (T.concat ["/window/", urlEncode w, path]) a