~alcinnz/amphiarao

23e975191744b5c1f6692fd9322d385a1cdd9855 — Adrian Cochrane 2 years ago 9efe611
Add session/*/element WebDriver endpoint.
1 files changed, 18 insertions(+), 3 deletions(-)

M src/Webdriver.hs
M src/Webdriver.hs => src/Webdriver.hs +18 -3
@@ 4,7 4,7 @@ module Webdriver(serveWebdriver) where
import Happstack.Lite
import Control.Concurrent.MVar
import Data.Aeson
import Data.Text (Text)
import Data.Text (Text, unpack)
import GHC.Generics
import Data.ByteString.Lazy (ByteString)



@@ 56,7 56,8 @@ serveSession sessions = WD.withSession fail (\uuid session -> msum [
            dir "fullscreen" $ unsupportedOp
        ],
        dir "frame" $ msum [noSuchFrame, dir "parent" $ ok $ toResponse ()], -- Noops
        dir "elements" $ findFromRoot session
        dir "element" $ findFromRoot session,
        dir "elements" $ findAllFromRoot session
    ]) sessions
  where
    fail uuid'| Just _ <- ID.fromString uuid' = errJSON 404 "invalid session ID" $ (


@@ 175,7 176,7 @@ unsupportedOp = do

----

findFromRoot session = do
findAllFromRoot session = do
    method POST
    nullDir
    req <- getJSON


@@ 186,3 187,17 @@ findFromRoot session = do
            Left (True, msg) -> errJSON 400 "invalid selector" msg
            Left (False, msg) -> errJSON 400 "invalid argument" msg
        Nothing -> errJSON 400 "invalid argument" "Failed to parse JSON"

findFromRoot session = do
    method POST
    nullDir
    req <- getJSON
    session' <- liftIO $ readMVar session
    case req of
        Just req' -> case WDE.find req' $ XC.fromDocument $ WD.document session' of
            Right (res:_) -> okJSON =<< liftIO (WD.serializeEl session res)
            Right [] | WDE.Find using query <- req' -> errJSON 404 "No such element" (
                "No elements match " ++ unpack using ++ " query: " ++ query)
            Left (True, msg) -> errJSON 400 "invalid selector" msg
            Left (False, msg) -> errJSON 400 "invalid argument" msg
        Nothing -> errJSON 400 "invalid argument" "Failed to parse JSON"