Skip to content

Commit

Permalink
Merge pull request #23 from tracsis/w3c
Browse files Browse the repository at this point in the history
w3c
  • Loading branch information
dten authored Dec 12, 2023
2 parents 3516aa7 + 699589a commit adc1a99
Show file tree
Hide file tree
Showing 13 changed files with 511 additions and 271 deletions.
2 changes: 2 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ library:
- zip-archive >= 0.1.1.8
exposed-modules:
- Test.WebDriver
- Test.WebDriver.Actions.Internal
- Test.WebDriver.Capabilities
- Test.WebDriver.Chrome.Extension
- Test.WebDriver.Class
Expand All @@ -87,6 +88,7 @@ library:
- Test.WebDriver.Firefox.Profile
- Test.WebDriver.Internal
- Test.WebDriver.JSON
- Test.WebDriver.JSON.Internal
- Test.WebDriver.Monad
- Test.WebDriver.Session
- Test.WebDriver.Session.History
Expand Down
151 changes: 151 additions & 0 deletions src/Test/WebDriver/Actions/Internal.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}

module Test.WebDriver.Actions.Internal where

import Data.Aeson
import Data.Aeson.TH
import Data.Text (Text)
import Test.WebDriver.Commands.Internal (Element)
import Test.WebDriver.JSON.Internal (lower1)

data Actions = Actions
{ actionsId :: Text
, actionsType :: ActionsType
, actionsParameters :: Maybe ActionsParameters
, actionsActions :: [Action]
} deriving (Eq, Show)

data ActionsType =
ActionsPointer
| ActionsKey
| ActionsNone
deriving (Eq, Show)

data ActionsParameters = ActionsParameters
{ paramsPointerType :: Maybe PointerType
} deriving (Eq, Show)

data Action = Action
{ actionType :: ActionType
, actionDurtion :: Maybe Int
, actionX :: Maybe Int
, actionY :: Maybe Int
, actionOrigin :: Maybe MoveOrigin
, actionValue :: Maybe Text
, actionButton :: Maybe MouseButton
} deriving (Eq, Show)

data PointerType =
PointerMouse
| PointerPen
| PointerTouch
deriving (Eq, Show)

data MoveOrigin =
OriginViewport
| OriginPointer
| OriginElement Element
deriving (Eq, Show)

instance ToJSON MoveOrigin where
toJSON OriginViewport = String "viewport"
toJSON OriginPointer = String "pointer"
toJSON (OriginElement e) = toJSON e

data ActionType =
ActionPause
| ActionKeyUp
| ActionKeyDown
| ActionPointerUp
| ActionPointerDown
| ActionPointerMove
| ActionPointerCancel
| ActionScroll
deriving (Eq, Show)

-- |A mouse button
data MouseButton =
LeftButton
| MiddleButton
| RightButton
deriving (Eq, Show, Ord, Bounded, Enum)

instance ToJSON MouseButton where
toJSON = toJSON . fromEnum

instance FromJSON MouseButton where
parseJSON v = do
n <- parseJSON v
case n :: Integer of
0 -> return LeftButton
1 -> return MiddleButton
2 -> return RightButton
err -> fail $ "Invalid JSON for MouseButton: " ++ show err

$(deriveToJSON (defaultOptions{constructorTagModifier = lower1 . drop 7}) ''PointerType)
$(deriveToJSON (defaultOptions{constructorTagModifier = lower1 . drop 6}) ''ActionType)
$(deriveToJSON (defaultOptions{constructorTagModifier = lower1 . drop 7}) ''ActionsType)
$(deriveToJSON (defaultOptions{fieldLabelModifier = lower1 . drop 6, omitNothingFields = True}) ''Action)
$(deriveToJSON (defaultOptions{fieldLabelModifier = lower1 . drop 6, omitNothingFields = True}) ''ActionsParameters)
$(deriveToJSON (defaultOptions{fieldLabelModifier = lower1 . drop 7, omitNothingFields = True}) ''Actions)

pointerMoveAction :: (Int, Int) -> MoveOrigin -> Action
pointerMoveAction (x, y) origin =
Action
{ actionType = ActionPointerMove
, actionDurtion = Nothing
, actionX = Just x
, actionY = Just y
, actionOrigin = Just origin
, actionValue = Nothing
, actionButton = Nothing
}

pointerDownAction :: Action
pointerDownAction =
Action
{ actionType = ActionPointerDown
, actionDurtion = Nothing
, actionX = Nothing
, actionY = Nothing
, actionOrigin = Nothing
, actionValue = Nothing
, actionButton = Nothing
}

pointerUpAction :: Action
pointerUpAction =
Action
{ actionType = ActionPointerUp
, actionDurtion = Nothing
, actionX = Nothing
, actionY = Nothing
, actionOrigin = Nothing
, actionValue = Nothing
, actionButton = Nothing
}

keyDownAction :: Text -> Action
keyDownAction c =
Action
{ actionType = ActionKeyDown
, actionDurtion = Nothing
, actionX = Nothing
, actionY = Nothing
, actionOrigin = Nothing
, actionValue = Just c
, actionButton = Nothing
}

keyUpAction :: Text -> Action
keyUpAction c =
Action
{ actionType = ActionKeyUp
, actionDurtion = Nothing
, actionX = Nothing
, actionY = Nothing
, actionOrigin = Nothing
, actionValue = Just c
, actionButton = Nothing
}
26 changes: 11 additions & 15 deletions src/Test/WebDriver/Capabilities.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Test.WebDriver.JSON
import Data.Aeson
import Data.Aeson.Types (Parser, typeMismatch, Pair)

import Data.Text (Text, toLower, toUpper)
import Data.Text (Text, toLower)
import Data.Default.Class (Default(..))
import Data.Word (Word16)
import Data.Maybe (fromMaybe, catMaybes)
Expand Down Expand Up @@ -176,7 +176,7 @@ instance ToJSON Capabilities where
object $ filter (\p -> snd p /= Null)
$ [ "browserName" .= browser
, "version" .= version
, "platform" .= platform
, "platformName" .= platform
, "proxy" .= proxy
, "javascriptEnabled" .= javascriptEnabled
, "takesScreenshot" .= takesScreenshot
Expand Down Expand Up @@ -204,7 +204,7 @@ instance ToJSON Capabilities where
]
Chrome {..}
-> catMaybes [ opt "chrome.chromedriverVersion" chromeDriverVersion ]
++ [ "chromeOptions" .= object (catMaybes
++ [ "goog:chromeOptions" .= object (catMaybes
[ opt "binary" chromeBinary
] ++
[ "args" .= chromeOptions
Expand Down Expand Up @@ -264,12 +264,7 @@ instance FromJSON Capabilities where
browser <- req "browserName"
Capabilities <$> getBrowserCaps browser
<*> opt "version" Nothing
<*> do
p <- o .:? "platform"
pN <- o .:? "platformName"
case p <|> pN of
Just p' -> return p'
Nothing -> throw . BadJSON $ "platform or platformName required"
<*> opt "platformName" Any
<*> opt "proxy" NoProxy
<*> b "javascriptEnabled"
<*> b "takesScreenshot"
Expand Down Expand Up @@ -615,7 +610,8 @@ data Platform = Windows | XP | Vista | Mac | Linux | Unix | Any
deriving (Eq, Show, Ord, Bounded, Enum)

instance ToJSON Platform where
toJSON = String . toUpper . fromString . show
toJSON Any = Null
toJSON p = String . toLower . fromString $ show p

instance FromJSON Platform where
parseJSON (String jStr) = case toLower jStr of
Expand Down Expand Up @@ -664,17 +660,17 @@ instance FromJSON ProxyType where
instance ToJSON ProxyType where
toJSON pt = object $ case pt of
NoProxy ->
["proxyType" .= ("DIRECT" :: String)]
["proxyType" .= ("direct" :: String)]
UseSystemSettings ->
["proxyType" .= ("SYSTEM" :: String)]
["proxyType" .= ("system" :: String)]
AutoDetect ->
["proxyType" .= ("AUTODETECT" :: String)]
["proxyType" .= ("autodetect" :: String)]
PAC{autoConfigUrl = url} ->
["proxyType" .= ("PAC" :: String)
["proxyType" .= ("pac" :: String)
,"proxyAutoconfigUrl" .= url
]
Manual{ftpProxy = ftp, sslProxy = ssl, httpProxy = http} ->
["proxyType" .= ("MANUAL" :: String)
["proxyType" .= ("manual" :: String)
,"ftpProxy" .= ftp
,"sslProxy" .= ssl
,"httpProxy" .= http
Expand Down
Loading

0 comments on commit adc1a99

Please sign in to comment.