Last active
November 8, 2016 16:26
-
-
Save dckc/5bf50193dbf98d330679 to your computer and use it in GitHub Desktop.
webdriver exception puzzle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm losing thusly: