{-# LANGUAGE OverloadedLists #-}
-- |

module Test.Sandwich.WebDriver.Internal.Capabilities (
  -- * Chrome
  chromeCapabilities
  , headlessChromeCapabilities

  -- * Firefox
  , firefoxCapabilities
  , headlessFirefoxCapabilities
  , getDefaultFirefoxProfile
  ) where

import Control.Monad.Trans.Control (MonadBaseControl)
import qualified Data.Aeson as A
import Data.Default
import Data.Function ((&))
import Test.WebDriver
import qualified Test.WebDriver.Firefox.Profile as FF

loggingPrefs :: A.Value
loggingPrefs :: Value
loggingPrefs = [Pair] -> Value
A.object [(Key
"browser", Value
"ALL")
                        , (Key
"client", Value
"WARNING")
                        , (Key
"driver", Value
"WARNING")
                        , (Key
"performance", Value
"ALL")
                        , (Key
"server", Value
"WARNING")
                        ]

-- * Chrome

-- | Default capabilities for regular Chrome.
-- Has the "browser" log level to "ALL" so that tests can collect browser logs.
chromeCapabilities :: Maybe FilePath -> Capabilities
chromeCapabilities :: Maybe FilePath -> Capabilities
chromeCapabilities Maybe FilePath
maybeChromePath =
  forall a. Default a => a
def {browser :: Browser
browser=Maybe FilePath
-> Maybe FilePath
-> [FilePath]
-> [ChromeExtension]
-> Object
-> Browser
Chrome forall a. Maybe a
Nothing Maybe FilePath
maybeChromePath [FilePath]
args [] forall a. Monoid a => a
mempty
      , additionalCaps :: [Pair]
additionalCaps=[(Key
"loggingPrefs", Value
loggingPrefs)
                       , (Key
"goog:loggingPrefs", Value
loggingPrefs)]
      }
  where args :: [FilePath]
args = [Item [FilePath]
"--verbose"]

-- | Default capabilities for headless Chrome.
headlessChromeCapabilities :: Maybe FilePath -> Capabilities
headlessChromeCapabilities :: Maybe FilePath -> Capabilities
headlessChromeCapabilities Maybe FilePath
maybeChromePath =
  forall a. Default a => a
def {browser :: Browser
browser=Maybe FilePath
-> Maybe FilePath
-> [FilePath]
-> [ChromeExtension]
-> Object
-> Browser
Chrome forall a. Maybe a
Nothing Maybe FilePath
maybeChromePath [FilePath]
args [] forall a. Monoid a => a
mempty
      , additionalCaps :: [Pair]
additionalCaps=[(Key
"loggingPrefs", Value
loggingPrefs)
                       , (Key
"goog:loggingPrefs", Value
loggingPrefs)]
      }
  where args :: [FilePath]
args = [Item [FilePath]
"--verbose", Item [FilePath]
"--headless"]

-- * Firefox

getDefaultFirefoxProfile :: MonadBaseControl IO m => FilePath -> m (FF.PreparedProfile FF.Firefox)
getDefaultFirefoxProfile :: forall (m :: * -> *).
MonadBaseControl IO m =>
FilePath -> m (PreparedProfile Firefox)
getDefaultFirefoxProfile FilePath
downloadDir = do
  Profile Firefox
FF.defaultProfile
    forall a b. a -> (a -> b) -> b
& forall a b. ToPref a => Text -> a -> Profile b -> Profile b
FF.addPref Text
"browser.download.folderList" (Int
2 :: Int)
    forall a b. a -> (a -> b) -> b
& forall a b. ToPref a => Text -> a -> Profile b -> Profile b
FF.addPref Text
"browser.download.manager.showWhenStarting" Bool
False
    forall a b. a -> (a -> b) -> b
& forall a b. ToPref a => Text -> a -> Profile b -> Profile b
FF.addPref Text
"browser.download.dir" FilePath
downloadDir
    forall a b. a -> (a -> b) -> b
& forall a b. ToPref a => Text -> a -> Profile b -> Profile b
FF.addPref Text
"browser.helperApps.neverAsk.saveToDisk" (FilePath
"*" :: String)
    forall a b. a -> (a -> b) -> b
& forall (m :: * -> *).
MonadBaseControl IO m =>
Profile Firefox -> m (PreparedProfile Firefox)
FF.prepareProfile

-- | Default capabilities for regular Firefox.
firefoxCapabilities :: Maybe FilePath -> Capabilities
firefoxCapabilities :: Maybe FilePath -> Capabilities
firefoxCapabilities Maybe FilePath
maybeFirefoxPath = forall a. Default a => a
def { browser :: Browser
browser=Browser
ff }
  where
    ff :: Browser
ff = Firefox { ffProfile :: Maybe (PreparedProfile Firefox)
ffProfile = forall a. Maybe a
Nothing
                 , ffLogPref :: LogLevel
ffLogPref = LogLevel
LogAll
                 , ffBinary :: Maybe FilePath
ffBinary = Maybe FilePath
maybeFirefoxPath
                 , ffAcceptInsecureCerts :: Maybe Bool
ffAcceptInsecureCerts = forall a. Maybe a
Nothing
                 }

-- | Default capabilities for headless Firefox.
headlessFirefoxCapabilities :: Maybe FilePath -> Capabilities
headlessFirefoxCapabilities :: Maybe FilePath -> Capabilities
headlessFirefoxCapabilities Maybe FilePath
maybeFirefoxPath = forall a. Default a => a
def { browser :: Browser
browser=Browser
ff, additionalCaps :: [Pair]
additionalCaps=[Pair]
additionalCaps }
  where
    ff :: Browser
ff = Firefox { ffProfile :: Maybe (PreparedProfile Firefox)
ffProfile = forall a. Maybe a
Nothing
                 , ffLogPref :: LogLevel
ffLogPref = LogLevel
LogAll
                 , ffBinary :: Maybe FilePath
ffBinary = Maybe FilePath
maybeFirefoxPath
                 , ffAcceptInsecureCerts :: Maybe Bool
ffAcceptInsecureCerts = forall a. Maybe a
Nothing
                 }

    additionalCaps :: [Pair]
additionalCaps = [(Key
"moz:firefoxOptions", [Pair] -> Value
A.object [(Key
"args", Array -> Value
A.Array [Value
"-headless"])])]