2020-11-07 13:40:42 +03:00
|
|
|
module TestUtils
|
|
|
|
( assertForeignTrue
|
|
|
|
, withBrowser
|
|
|
|
, withBrowserPage
|
|
|
|
, testClickEvent
|
|
|
|
, cwd
|
2020-11-07 20:08:18 +03:00
|
|
|
, isNull
|
2020-11-07 13:40:42 +03:00
|
|
|
)
|
|
|
|
where
|
2020-11-01 13:16:43 +03:00
|
|
|
|
|
|
|
import Test.Unit.Assert as Assert
|
2020-11-07 13:40:42 +03:00
|
|
|
import Foreign (Foreign, readBoolean)
|
|
|
|
import Control.Monad.Except (runExcept)
|
|
|
|
import Data.Either (Either(..))
|
|
|
|
import Effect.Aff (Aff)
|
|
|
|
import Prelude (Unit, bind, discard, void, ($), (<>), (==))
|
2020-11-01 13:16:43 +03:00
|
|
|
import Playwright
|
|
|
|
import Control.Monad.Error.Class (withResource)
|
|
|
|
|
|
|
|
assertForeignTrue :: Foreign -> Aff Unit
|
|
|
|
assertForeignTrue value = do
|
|
|
|
let
|
|
|
|
eiBool = runExcept $ readBoolean value
|
|
|
|
Assert.assert "Foreign value is boolean true" (eiBool == Right true)
|
|
|
|
|
|
|
|
withBrowser :: forall a. (Browser -> Aff a) -> Aff a
|
2020-11-01 14:48:16 +03:00
|
|
|
withBrowser = withResource acquire release
|
2020-11-01 13:16:43 +03:00
|
|
|
where
|
|
|
|
acquire = launch chromium {}
|
|
|
|
release = close
|
|
|
|
|
|
|
|
withBrowserPage :: forall a. URL -> (Page -> Aff a) -> Aff a
|
|
|
|
withBrowserPage url action = do
|
|
|
|
withBrowser
|
|
|
|
\browser -> do
|
2020-11-01 14:48:16 +03:00
|
|
|
let
|
2020-11-07 20:08:18 +03:00
|
|
|
acquire = newPage browser { acceptDownloads: true }
|
2020-11-01 14:48:16 +03:00
|
|
|
release = close
|
|
|
|
withResource acquire release
|
|
|
|
\page -> do
|
|
|
|
void $ goto page url {}
|
|
|
|
action page
|
2020-11-01 13:16:43 +03:00
|
|
|
|
|
|
|
testClickEvent :: String -> (Page -> Selector -> {} -> Aff Unit) -> Page -> Aff Unit
|
|
|
|
testClickEvent event action page = do
|
|
|
|
void $ evaluate page ("setEventTester('" <> event <> "');")
|
|
|
|
action page (Selector $ "#event-" <> event) {}
|
|
|
|
result <- evaluate page $ "checkEventHappened('" <> event <> "');"
|
|
|
|
assertForeignTrue result
|
|
|
|
|
2020-11-07 20:08:18 +03:00
|
|
|
foreign import isNull :: forall a. a -> Boolean
|
|
|
|
|
2020-11-01 13:16:43 +03:00
|
|
|
foreign import cwd :: String
|