Skip to content

Instantly share code, notes, and snippets.

@dckc
Last active November 8, 2016 16:26
Show Gist options
  • Save dckc/5bf50193dbf98d330679 to your computer and use it in GitHub Desktop.
Save dckc/5bf50193dbf98d330679 to your computer and use it in GitHub Desktop.
webdriver exception puzzle
{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
module AccountSite where
import Control.Monad.Trans (liftIO)
import Control.Exception (Exception, catch, throwIO)
import Control.Monad.Base (liftBase)
import qualified Data.Text as T
import Test.WebDriver (WDConfig(..), defaultConfig, chrome,
Capabilities(..), defaultCaps,
runSession,
openPage, findElem, Selector(..))
import Test.WebDriver.Commands.Wait (waitUntil)
import Test.WebDriver.Class (WebDriver)
import Test.WebDriver.Exceptions (FailedCommandType(NoSuchElement), FailedCommand(..))
import Text.Printf (printf, IsChar)
chromeConfig :: WDConfig
chromeConfig = defaultConfig { wdCapabilities = defaultCaps { browser = chrome }}
wait_time = 60.0
login :: WebDriver wd => String -> String -> wd ()
login home logged_in = do
openPage home
waitUntil wait_time (textAppears logged_in)
return ()
where
textAppears txt = do
(findElem (ByXPath expr) >> return ()) `catch` ignoreNoSuchElement
-- ignoreNoSuchElement :: FailedCommandType -> wd ()
-- ignoreNoSuchElement e = liftBase $ return ()
ignoreNoSuchElement (FailedCommand NoSuchElement _) = return ()
ignoreNoSuchElement e = liftBase (throwIO e)
-- ignoreNoSuchElement step = catchJust isNoSuchElement (step >> return ()) ignore
-- isNoSuchElement :: Exception e => e -> Maybe ()
-- isNoSuchElement NoSuchElement = Just ()
-- isNoSuchElement _ = Nothing
-- ignore _ = return ()
expr :: T.Text
expr = T.pack $ printf "//div[contains(normalize-space(.), '%s')]" logged_in
-- Initial txcollect.cabal generated by cabal init. For further
-- documentation, see http://haskell.org/cabal/users-guide/
name: txcollect
version: 0.1.0.0
synopsis: Collect Personal Finance Transactions
-- description:
homepage: http://www.madmode.com/
license: MIT
license-file: LICENSE
author: Dan Connolly
maintainer: [email protected]
-- copyright:
category: Web
build-type: Simple
cabal-version: >=1.8
-- executable txcollect
-- main-is: dm93.hs
-- other-modules: AccountSite
-- build-depends: base ==4.6.*, webdriver, text, transformers-base
library
exposed-modules: AccountSite
-- other-modules:
build-depends: base ==4.6.*, webdriver, text, transformers-base
@dckc
Copy link
Author

dckc commented Sep 3, 2014

I'm losing thusly:

cabal configure && cabal build
Resolving dependencies...
Configuring txcollect-0.1.0.0...
Building txcollect-0.1.0.0...
Preprocessing library txcollect-0.1.0.0...
[1 of 1] Compiling AccountSite      ( AccountSite.hs, dist/build/AccountSite.o )

AccountSite.hs:25:24:
    Could not deduce (wd ~ IO)
    from the context (WebDriver wd)
      bound by the type signature for
                 login :: WebDriver wd => String -> String -> wd ()
      at AccountSite.hs:22:10-50
      `wd' is a rigid type variable bound by
           the type signature for
             login :: WebDriver wd => String -> String -> wd ()
           at AccountSite.hs:22:10
    Expected type: wd ()
      Actual type: IO ()
    In the return type of a call of `textAppears'
    In the second argument of `waitUntil', namely
      `(textAppears logged_in)'
    In a stmt of a 'do' block:
      waitUntil wait_time (textAppears logged_in)

Compilation exited abnormally with code 1 at Wed Sep  3 00:57:39

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment