{-# LANGUAGE OverloadedStrings, BangPatterns #-}
module Web.Api.WebDriver.Endpoints (
runIsolated
, newSession
, newSession'
, deleteSession
, sessionStatus
, getTimeouts
, setTimeouts
, navigateTo
, navigateToStealth
, getCurrentUrl
, goBack
, goForward
, pageRefresh
, getTitle
, getWindowHandle
, closeWindow
, switchToWindow
, getWindowHandles
, switchToFrame
, switchToParentFrame
, getWindowRect
, setWindowRect
, maximizeWindow
, minimizeWindow
, fullscreenWindow
, findElement
, findElements
, findElementFromElement
, findElementsFromElement
, getActiveElement
, isElementSelected
, getElementAttribute
, getElementProperty
, getElementCssValue
, getElementText
, getElementTagName
, getElementRect
, isElementEnabled
, elementClick
, elementClear
, elementSendKeys
, getPageSource
, getPageSourceStealth
, executeScript
, executeAsyncScript
, getAllCookies
, getNamedCookie
, addCookie
, deleteCookie
, deleteAllCookies
, performActions
, performActionsStealth
, releaseActions
, dismissAlert
, acceptAlert
, getAlertText
, sendAlertText
, takeScreenshot
, takeElementScreenshot
, _WEB_ELEMENT_ID
, _WEB_WINDOW_ID
, _WEB_FRAME_ID
) where
import Data.Aeson
( Value(..), encode, object, (.=), toJSON )
import Data.Text
( Text, unpack, pack )
import Data.Text.Encoding
( encodeUtf8 )
import qualified Data.ByteString as SB
import qualified Data.ByteString.Base64 as B64
import qualified Network.URI.Encode as E
import Web.Api.WebDriver.Types
import Web.Api.WebDriver.Classes
import Web.Api.WebDriver.Monad
_WEB_ELEMENT_ID :: Text
_WEB_ELEMENT_ID = "element-6066-11e4-a52e-4f735466cecf"
_WEB_WINDOW_ID :: Text
_WEB_WINDOW_ID = "window-fcc6-11e5-b4f8-330a88ab9d7f"
_WEB_FRAME_ID :: Text
_WEB_FRAME_ID = "frame-075b-4da1-b6ba-e579c2d3230a"
theRemoteUrl :: (Monad m) => WebDriverT m String
theRemoteUrl = do
host <- fromEnv (_remoteHostname . _env)
port <- fromEnv (_remotePort . _env)
path <- fromEnv (_remotePath . _env)
return $ concat [ "http://", host, ":", show port, path]
theRemoteUrlWithSession :: (Monad m) => WebDriverT m String
theRemoteUrlWithSession = do
st <- fromState (_sessionId . _userState)
case st of
Nothing -> throwError NoSession
Just session_id -> do
baseUrl <- theRemoteUrl
return $ concat [ baseUrl, "/session/", session_id ]
setSessionId
:: Maybe String
-> S WDState
-> S WDState
setSessionId x st = st { _userState = (_userState st) { _sessionId = x } }
cleanupOnError
:: (Monad m)
=> WebDriverT m a
-> WebDriverT m a
cleanupOnError x = catchError x $ \e ->
deleteSession >> throwError e
runIsolated
:: (Monad m)
=> Capabilities
-> WebDriverT m a
-> WebDriverT m ()
runIsolated caps theSession = cleanupOnError $ do
session_id <- newSession caps
modifyState $ setSessionId (Just session_id)
theSession
deleteSession
modifyState $ setSessionId Nothing
newSession
:: (Monad m)
=> Capabilities
-> WebDriverT m SessionId
newSession = newSession' id
newSession'
:: (Monad m)
=> (Value -> Value)
-> Capabilities
-> WebDriverT m SessionId
newSession' f caps = do
baseUrl <- theRemoteUrl
format <- fromEnv (_responseFormat . _env)
let
!payload = encode $ f $ object
[ "capabilities" .= object
[ "alwaysMatch" .= toJSON caps ]
, "desiredCapabilities" .= toJSON caps
]
httpPost (baseUrl ++ "/session") payload
>>= (return . _responseBody)
>>= parseJson
>>= case format of
SpecFormat -> lookupKeyJson "value"
ChromeFormat -> return
>>= lookupKeyJson "sessionId"
>>= constructFromJson
>>= (return . unpack)
deleteSession
:: (Monad m) => WebDriverT m ()
deleteSession = do
(baseUrl, format) <- theRequestContext
httpDelete baseUrl
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
sessionStatus
:: (Monad m)
=> WebDriverT m (Bool, String)
sessionStatus = do
baseUrl <- theRemoteUrl
format <- fromEnv (_responseFormat . _env)
r <- httpGet (baseUrl ++ "/status")
>>= (return . _responseBody)
>>= parseJson
ready <- case format of
SpecFormat ->
lookupKeyJson "value" r
>>= lookupKeyJson "ready"
>>= constructFromJson
ChromeFormat -> return True
msg <- case format of
SpecFormat ->
lookupKeyJson "value" r
>>= lookupKeyJson "message"
>>= constructFromJson
>>= (return . unpack)
ChromeFormat -> return "chromedriver is not spec compliant :)"
return (ready, msg)
getTimeouts
:: (Monad m)
=> WebDriverT m TimeoutConfig
getTimeouts = do
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/timeouts")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
setTimeouts
:: (Monad m)
=> TimeoutConfig
-> WebDriverT m ()
setTimeouts timeouts = do
baseUrl <- theRemoteUrlWithSession
let !payload = encode timeouts
httpPost (baseUrl ++ "/timeouts") payload
return ()
navigateTo
:: (Monad m)
=> Url
-> WebDriverT m ()
navigateTo url = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object [ "url" .= url ]
httpPost (baseUrl ++ "/url") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
navigateToStealth
:: (Monad m)
=> Url
-> WebDriverT m ()
navigateToStealth url = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object [ "url" .= url ]
httpSilentPost (baseUrl ++ "/url") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
getCurrentUrl
:: (Monad m)
=> WebDriverT m Url
getCurrentUrl = do
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/url")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= (return . unpack)
goBack
:: (Monad m)
=> WebDriverT m ()
goBack = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object []
httpPost (baseUrl ++ "/back") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
goForward
:: (Monad m)
=> WebDriverT m ()
goForward = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object []
httpPost (baseUrl ++ "/forward") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
pageRefresh
:: (Monad m)
=> WebDriverT m ()
pageRefresh = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object []
httpPost (baseUrl ++ "/refresh") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
getTitle
:: (Monad m)
=> WebDriverT m String
getTitle = do
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/title")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= (return . unpack)
getWindowHandle
:: (Monad m)
=> WebDriverT m ContextId
getWindowHandle = do
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/window")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= (return . ContextId . unpack)
closeWindow
:: (Monad m)
=> WebDriverT m [ContextId]
closeWindow = do
baseUrl <- theRemoteUrlWithSession
httpDelete (baseUrl ++ "/window")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= (sequence . map constructFromJson)
>>= (return . map (ContextId . unpack))
switchToWindow
:: (Monad m, HasContextId t)
=> t
-> WebDriverT m ()
switchToWindow t = do
let contextId = contextIdOf t
baseUrl <- theRemoteUrlWithSession
let !payload = encode $ object [ "handle" .= show contextId ]
httpPost (baseUrl ++ "/window") payload
return ()
getWindowHandles
:: (Monad m)
=> WebDriverT m [ContextId]
getWindowHandles = do
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/window/handles")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= (sequence . map constructFromJson)
>>= (return . map (ContextId . unpack))
switchToFrame
:: (Monad m)
=> FrameReference
-> WebDriverT m ()
switchToFrame ref = do
(baseUrl, format) <- theRequestContext
let
!frame = case ref of
TopLevelFrame -> Null
FrameNumber k -> Number $ fromIntegral k
FrameContainingElement element_id -> String $ pack $ show element_id
!payload = encode $ object
[ "id" .= toJSON frame ]
httpPost (baseUrl ++ "/frame") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= case format of
SpecFormat -> expect (object [])
ChromeFormat -> expect Null
return ()
switchToParentFrame
:: (Monad m)
=> WebDriverT m ()
switchToParentFrame = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object []
httpPost (baseUrl ++ "/frame/parent") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
getWindowRect
:: (Monad m)
=> WebDriverT m Rect
getWindowRect = do
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/window/rect")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
setWindowRect
:: (Monad m)
=> Rect
-> WebDriverT m Rect
setWindowRect rect = do
baseUrl <- theRemoteUrlWithSession
let !payload = encode rect
httpPost (baseUrl ++ "/window/rect") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
maximizeWindow
:: (Monad m)
=> WebDriverT m Rect
maximizeWindow = do
baseUrl <- theRemoteUrlWithSession
let !payload = encode $ object []
httpPost (baseUrl ++ "/window/maximize") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
minimizeWindow
:: (Monad m)
=> WebDriverT m Rect
minimizeWindow = do
baseUrl <- theRemoteUrlWithSession
let !payload = encode $ object []
httpPost (baseUrl ++ "/window/minimize") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
fullscreenWindow
:: (Monad m)
=> WebDriverT m Rect
fullscreenWindow = do
baseUrl <- theRemoteUrlWithSession
let !payload = encode $ object []
httpPost (baseUrl ++ "/window/fullscreen") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
findElement
:: (Monad m)
=> LocationStrategy
-> Selector
-> WebDriverT m ElementRef
findElement strategy selector = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object [ "value" .= selector, "using" .= toJSON strategy ]
httpPost (baseUrl ++ "/element") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= case format of
SpecFormat -> lookupKeyJson _WEB_ELEMENT_ID
ChromeFormat -> lookupKeyJson "ELEMENT"
>>= constructFromJson
>>= (return . ElementRef . unpack)
findElements
:: (Monad m)
=> LocationStrategy
-> Selector
-> WebDriverT m [ElementRef]
findElements strategy selector = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object [ "value" .= selector, "using" .= toJSON strategy ]
httpPost (baseUrl ++ "/elements") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= case format of
SpecFormat -> mapM (lookupKeyJson _WEB_ELEMENT_ID)
ChromeFormat -> mapM (lookupKeyJson "ELEMENT")
>>= mapM constructFromJson
>>= (return . map (ElementRef . unpack))
findElementFromElement
:: (Monad m, HasElementRef t)
=> LocationStrategy
-> Selector
-> t
-> WebDriverT m ElementRef
findElementFromElement strategy selector root = do
(baseUrl, format) <- theRequestContext
let root_id = elementRefOf root
let !payload = encode $ object [ "value" .= selector, "using" .= toJSON strategy ]
httpPost (baseUrl ++ "/element/" ++ show root_id ++ "/element") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= case format of
SpecFormat -> lookupKeyJson _WEB_ELEMENT_ID
ChromeFormat -> lookupKeyJson "ELEMENT"
>>= constructFromJson
>>= (return . ElementRef . unpack)
findElementsFromElement
:: (Monad m, HasElementRef t)
=> LocationStrategy
-> Selector
-> t
-> WebDriverT m [ElementRef]
findElementsFromElement strategy selector root = do
(baseUrl, format) <- theRequestContext
let root_id = elementRefOf root
let !payload = encode $ object [ "value" .= selector, "using" .= toJSON strategy ]
httpPost (baseUrl ++ "/element/" ++ show root_id ++ "/elements") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= case format of
SpecFormat -> mapM (lookupKeyJson _WEB_ELEMENT_ID)
ChromeFormat -> mapM (lookupKeyJson "ELEMENT")
>>= mapM constructFromJson
>>= (return . map (ElementRef . unpack))
getActiveElement
:: (Monad m)
=> WebDriverT m ElementRef
getActiveElement = do
(baseUrl, format) <- theRequestContext
httpGet (baseUrl ++ "/element/active")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= case format of
SpecFormat -> lookupKeyJson _WEB_ELEMENT_ID
ChromeFormat -> lookupKeyJson "ELEMENT"
>>= constructFromJson
>>= (return . ElementRef . unpack)
isElementSelected
:: (Monad m, HasElementRef t)
=> t
-> WebDriverT m Bool
isElementSelected element = do
let elementRef = show $ elementRefOf element
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/element/" ++ elementRef ++ "/selected")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
getElementAttribute
:: (Monad m, HasElementRef t)
=> t
-> AttributeName
-> WebDriverT m (Either Bool String)
getElementAttribute element name = do
let elementRef = show $ elementRefOf element
baseUrl <- theRemoteUrlWithSession
x <- httpGet (baseUrl ++ "/element/" ++ elementRef ++ "/attribute/" ++ E.encode name)
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
case x of
Null -> return (Left False)
String "true" -> return (Left True)
String attr -> return (Right $ unpack attr)
_ -> throwJsonError $ JsonError "Invalid element attribute response"
getElementProperty
:: (Monad m, HasElementRef t)
=> t
-> PropertyName
-> WebDriverT m Value
getElementProperty element name = do
let elementRef = show $ elementRefOf element
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/element/" ++ elementRef ++ "/property/" ++ E.encode name)
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
getElementCssValue
:: (Monad m, HasElementRef t)
=> t
-> CssPropertyName
-> WebDriverT m String
getElementCssValue element name = do
let elementRef = show $ elementRefOf element
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/element/" ++ elementRef ++ "/css/" ++ name)
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
getElementText
:: (Monad m, HasElementRef t)
=> t
-> WebDriverT m String
getElementText element = do
let elementRef = show $ elementRefOf element
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/element/" ++ elementRef ++ "/text")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
getElementTagName
:: (Monad m, HasElementRef t)
=> t
-> WebDriverT m String
getElementTagName element = do
let elementRef = show $ elementRefOf element
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/element/" ++ elementRef ++ "/name")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
getElementRect
:: (Monad m, HasElementRef t)
=> t
-> WebDriverT m Rect
getElementRect element = do
let elementRef = show $ elementRefOf element
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/element/" ++ elementRef ++ "/rect")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
isElementEnabled
:: (Monad m, HasElementRef t)
=> t
-> WebDriverT m Bool
isElementEnabled element = do
let elementRef = show $ elementRefOf element
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/element/" ++ elementRef ++ "/enabled")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
elementClick
:: (Monad m, HasElementRef t)
=> t
-> WebDriverT m ()
elementClick element = do
(baseUrl, format) <- theRequestContext
let elementRef = show $ elementRefOf element
let !payload = encode $ object []
httpPost (baseUrl ++ "/element/" ++ elementRef ++ "/click") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
elementClear
:: (Monad m, HasElementRef t)
=> t
-> WebDriverT m ()
elementClear element = do
(baseUrl, format) <- theRequestContext
let elementRef = show $ elementRefOf element
let !payload = encode $ object []
httpPost (baseUrl ++ "/element/" ++ elementRef ++ "/clear") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
elementSendKeys
:: (Monad m, HasElementRef t)
=> t
-> String
-> WebDriverT m ()
elementSendKeys element text = do
let elementRef = show $ elementRefOf element
baseUrl <- theRemoteUrlWithSession
let !payload = encode $ object [ "text" .= text ]
httpPost (baseUrl ++ "/element/" ++ elementRef ++ "/value") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expect (object [])
return ()
getPageSource
:: (Monad m)
=> WebDriverT m String
getPageSource = do
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/source")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= (return . unpack)
getPageSourceStealth
:: (Monad m)
=> WebDriverT m String
getPageSourceStealth = do
baseUrl <- theRemoteUrlWithSession
httpSilentGet (baseUrl ++ "/source")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= (return . unpack)
executeScript
:: (Monad m)
=> Script
-> [Value]
-> WebDriverT m Value
executeScript script args = do
baseUrl <- theRemoteUrlWithSession
let !payload = encode $ object [ "script" .= script, "args" .= toJSON args ]
httpPost (baseUrl ++ "/execute/sync") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
executeAsyncScript
:: (Monad m)
=> Script
-> [Value]
-> WebDriverT m Value
executeAsyncScript script args = do
baseUrl <- theRemoteUrlWithSession
let !payload = encode $ object [ "script" .= script, "args" .= toJSON args ]
httpPost (baseUrl ++ "/execute/async") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
getAllCookies
:: (Monad m)
=> WebDriverT m [Cookie]
getAllCookies = do
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/cookie")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= mapM constructFromJson
getNamedCookie
:: (Monad m)
=> CookieName
-> WebDriverT m Cookie
getNamedCookie name = do
baseUrl <- theRemoteUrlWithSession
httpGet (baseUrl ++ "/cookie/" ++ E.encode name)
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
addCookie
:: (Monad m)
=> Cookie
-> WebDriverT m ()
addCookie cookie = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object [ "cookie" .= cookie ]
httpSilentPost (baseUrl ++ "/cookie") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
deleteCookie
:: (Monad m)
=> CookieName
-> WebDriverT m ()
deleteCookie name = do
(baseUrl, format) <- theRequestContext
httpDelete (baseUrl ++ "/cookie/" ++ E.encode name)
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
deleteAllCookies
:: (Monad m)
=> WebDriverT m ()
deleteAllCookies = do
(baseUrl, format) <- theRequestContext
httpDelete (baseUrl ++ "/cookie")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
performActions
:: (Monad m)
=> [Action]
-> WebDriverT m ()
performActions = _performActions False
performActionsStealth
:: (Monad m)
=> [Action]
-> WebDriverT m ()
performActionsStealth = _performActions True
_performActions
:: (Monad m)
=> Bool
-> [Action]
-> WebDriverT m ()
_performActions stealth action = do
baseUrl <- theRemoteUrlWithSession
let !payload = encode $ object [ "actions" .= toJSON action ]
let httpMethod = if stealth then httpSilentPost else httpPost
httpMethod (baseUrl ++ "/actions") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expect (object [])
return ()
releaseActions
:: (Monad m)
=> WebDriverT m ()
releaseActions = do
baseUrl <- theRemoteUrlWithSession
httpDelete (baseUrl ++ "/actions")
return ()
dismissAlert
:: (Monad m)
=> WebDriverT m ()
dismissAlert = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object []
httpPost (baseUrl ++ "/alert/dismiss") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
acceptAlert
:: (Monad m)
=> WebDriverT m ()
acceptAlert = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object []
httpPost (baseUrl ++ "/alert/accept") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
getAlertText
:: (Monad m)
=> WebDriverT m (Maybe String)
getAlertText = do
baseUrl <- theRemoteUrlWithSession
msg <- httpGet (baseUrl ++ "/alert/text")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
case msg of
Null -> return Nothing
String text -> return $ Just (unpack text)
_ -> throwJsonError $ JsonError "Invalid alert text response"
sendAlertText
:: (Monad m)
=> String
-> WebDriverT m ()
sendAlertText msg = do
(baseUrl, format) <- theRequestContext
let !payload = encode $ object [ "text" .= msg ]
httpPost (baseUrl ++ "/alert/text") payload
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= expectEmptyObject format
return ()
takeScreenshot
:: (Monad m)
=> WebDriverT m SB.ByteString
takeScreenshot = do
baseUrl <- theRemoteUrlWithSession
result <- httpGet (baseUrl ++ "/screenshot")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= (return . B64.decode . encodeUtf8)
case result of
Right img -> return img
Left str -> throwError $ ImageDecodeError str
takeElementScreenshot
:: (Monad m, HasElementRef t)
=> t
-> WebDriverT m SB.ByteString
takeElementScreenshot element = do
let elementRef = show $ elementRefOf element
baseUrl <- theRemoteUrlWithSession
result <- httpGet (baseUrl ++ "/element/" ++ elementRef ++ "/screenshot")
>>= (return . _responseBody)
>>= parseJson
>>= lookupKeyJson "value"
>>= constructFromJson
>>= (return . B64.decode . encodeUtf8)
case result of
Right img -> return img
Left str -> throwError $ ImageDecodeError str
expectEmptyObject
:: (Monad m)
=> ResponseFormat
-> Value
-> WebDriverT m Value
expectEmptyObject format value = case format of
SpecFormat -> expect (object []) value
ChromeFormat -> expect Null value
theRequestContext
:: (Monad m)
=> WebDriverT m (String, ResponseFormat)
theRequestContext = do
baseUrl <- theRemoteUrlWithSession
format <- fromEnv (_responseFormat . _env)
return (baseUrl, format)