~alcinnz/amphiarao

74b98fb8608f9e15c0ac7dbda43b1e3e3e18a90c — Adrian Cochrane 3 years ago 28b0650
Allowing retrieving element info via WebDriver.
1 files changed, 34 insertions(+), 1 deletions(-)

M src/Webdriver.hs
M src/Webdriver.hs => src/Webdriver.hs +34 -1
@@ 5,20 5,24 @@ import Happstack.Lite
import Control.Concurrent.MVar
import Data.Aeson
import Data.Text (Text, unpack)
import qualified Data.Text as Txt
import GHC.Generics
import Data.ByteString.Lazy (ByteString)

import qualified Data.HashMap.Strict as M
import qualified Data.Map.Strict as M'
import Data.UUID as ID
import Data.UUID.V4

import Control.Monad.IO.Class (liftIO)
import Control.Monad (mapM)
import Data.Maybe (fromMaybe, isJust)
import Data.String as Str

import qualified Network.URI as URI
import qualified Network.URI.Fetch as URI
import qualified Text.XML.Cursor as XC
import qualified Text.XML as X

import Capabilities (processCaps)
import JSON


@@ 209,7 213,13 @@ serveElement session elUUID = do
    case WD.getEl session' =<< ID.fromString elUUID of
        Just el -> msum [
            dir "element" $ findFromEl session el,
            dir "elements" $ findAllFromEl session el
            dir "elements" $ findAllFromEl session el,
            dir "attribute" $ path $ getAttribute el,
            dir "property" $ path $ getAttribute el, -- Don't want to implement the DOM abomination!
            -- TODO integrate CSS
            dir "text" $ getElText el,
            dir "name" $ getElName el,
            dir "rect" $ unsupportedOp -- Will be meaningful for Haphaestus!
          ]
        Nothing | Nothing <- ID.fromString elUUID ->
            errJSON 404 "no such element" "Invalid UUID"


@@ 243,3 253,26 @@ noSuchEl = do
    method GET
    nullDir
    errJSON 404 "no such element" "Rhapsode does not have active elements."

getAttribute el name = do
    method GET
    nullDir
    ok $ toResponse $ case XC.node el of
        X.NodeElement (X.Element _ attrs _) ->
            fromMaybe "" $ M'.lookup (Str.fromString name) attrs
        _ -> ""

getElText el = do -- TODO allow CSS to impact the response.
    method GET
    nullDir
    ok $ toResponse $ Txt.concat $ XC.content el

getElName el = do
    method GET
    nullDir
    ok $ toResponse $ case XC.node el of
        X.NodeElement (X.Element name _ _) -> name2text name
        _ -> ""

name2text (X.Name name _ (Just prefix)) = Txt.concat [prefix, ":", name]
name2text (X.Name name _ Nothing) = name