packages feed

sydtest-webdriver-0.1.0.0 / test / Test / Syd / WebdriverSpec.hs

{-# LANGUAGE ScopedTypeVariables #-}module Test.Syd.WebdriverSpec (spec) whereimport Control.Exceptionimport Data.List.NonEmpty (NonEmpty (..))import qualified Data.List.NonEmpty as NEimport qualified Network.HTTP.Types as HTTPimport Network.URIimport Test.Sydimport Test.Syd.OptParseimport Test.Syd.Waiimport Test.Syd.Webdriverimport Test.Syd.Webdriver.Appimport Test.WebDriver (Rect (..), getWindowRect)import Test.WebDriver.Capabilities (Capabilities (..), ChromeOptions (..))-- | An exception that a test throws itself, but that is classified as-- asynchronous, as @effectful@'s @Error@ effect does.-- See https://github.com/NorfairKing/sydtest/issues/110data SynchronousButClassifiedAsynchronous = SynchronousButClassifiedAsynchronous  deriving (Show, Eq)instance Exception SynchronousButClassifiedAsynchronous where  toException = asyncExceptionToException  fromException = asyncExceptionFromExceptionspec :: Specspec = do  describe "a test that throws an asynchronously-classified exception fails with that exception" $ do    let settings = defaultSettings {settingRetries = 1}    it "WebdriverTestM app ()" $ do      specForest <-        execTestDefM settings $          exampleAppSpec $            it "throws" (liftIO (throwIO SynchronousButClassifiedAsynchronous) :: WebdriverTestM () ())      resultForest <- runSpecForestSynchronously settings specForest      map        (NE.map (\r -> (testRunResultStatus r, testRunResultException r >>= fromException)) . testRunReportRawResults . timedValue . testDefVal . snd)        (flattenSpecForest (timedValue resultForest))        `shouldBe` [(TestFailed, Just SynchronousButClassifiedAsynchronous) :| []]    it "WebdriverTestM app (GoldenTest a)" $ do      specForest <-        execTestDefM settings $          exampleAppSpec $            it              "throws"              ( pure                  ( GoldenTest                      { goldenTestRead = pure (Just ()),                        goldenTestProduce = throwIO SynchronousButClassifiedAsynchronous,                        goldenTestWrite = \() -> pure (),                        goldenTestCompare = \() () -> pure Nothing                      }                  ) ::                  WebdriverTestM () (GoldenTest ())              )      resultForest <- runSpecForestSynchronously settings specForest      map        (NE.map (\r -> (testRunResultStatus r, testRunResultException r >>= fromException)) . testRunReportRawResults . timedValue . testDefVal . snd)        (flattenSpecForest (timedValue resultForest))        `shouldBe` [(TestFailed, Just SynchronousButClassifiedAsynchronous) :| []]  exampleAppSpec $ do    it "reports why a session could not be started" $ \env -> do      let caps = webdriverTestEnvCapabilities env      let brokenChromeOptions = (\o -> o {_chromeOptionsBinary = Just "/nonexistent/chromium"}) <$> _capabilitiesGoogChromeOptions caps      let brokenEnv = env {webdriverTestEnvCapabilities = caps {_capabilitiesGoogChromeOptions = brokenChromeOptions}}      result <- try $ runWebdriverTestM brokenEnv (pure ())      case result of        Right () -> expectationFailure "Should not have been able to start a session."        Left (e :: SomeException) ->          fmap webdriverSessionStartFailedStatus (fromException e) `shouldBe` Just HTTP.status500    it "can navigate to home" $      openPath "/"    it "can resize the window without moving it" $ do      openPath "/"      rectBefore <- getWindowRect      setWindowSize (800, 600)      rectAfter <- getWindowRect      liftIO $        rectAfter          `shouldBe` rectBefore            { rectWidth = 800,              rectHeight = 600            }exampleAppSpec :: WebdriverSpec () -> SpecexampleAppSpec = webdriverSpec $ \_ -> do  portNumber <- applicationSetupFunc exampleApplication  let uriStr = "http://127.0.0.1:" <> show portNumber  case parseURI uriStr of    Nothing -> liftIO $ expectationFailure $ "Failed to parse uri as string: " <> show uriStr    Just uri -> pure (uri, ())