Skip to content
This repository has been archived by the owner on Jan 17, 2020. It is now read-only.

Commit

Permalink
Merge pull request #50 from garyb/ps-0.11
Browse files Browse the repository at this point in the history
Update for PureScript 0.11
  • Loading branch information
garyb authored Apr 24, 2017
2 parents 2b02572 + a7f80f3 commit ab56644
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 131 deletions.
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
],
"license": "Apache-2.0",
"dependencies": {
"purescript-aff": "^2.0.1",
"purescript-aff-reattempt": "^2.0.0",
"purescript-dom": "^3.1.0"
"purescript-aff": "^3.0.0",
"purescript-aff-reattempt": "^3.0.0",
"purescript-dom": "^4.3.1"
}
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"selenium-webdriver": "3.0.0-beta-3"
},
"devDependencies": {
"pulp": "^9.0.1",
"purescript-psa": "^0.3.9",
"purescript": "^0.10.1",
"rimraf": "^2.5.4"
"pulp": "^11.0.0",
"purescript-psa": "^0.5.1",
"purescript": "^0.11.4",
"rimraf": "^2.6.1"
}
}
5 changes: 2 additions & 3 deletions src/Selenium.purs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,14 @@ module Selenium
) where

import Prelude

import Control.Monad.Aff (Aff, attempt)
import Control.Monad.Eff.Exception (error)
import Control.Monad.Error.Class (throwError)

import Data.Array (uncons)
import Data.Either (either)
import Data.Foreign (Foreign)
import Data.Maybe (Maybe(..))
import Data.Time.Duration (Milliseconds)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (class Unfoldable, unfoldr)

Expand All @@ -86,7 +85,7 @@ foreign import get
foreign import wait
e
. Aff (selenium SELENIUM|e) Boolean
Int
Milliseconds
Driver
Aff (selenium SELENIUM|e) Unit

Expand Down
2 changes: 1 addition & 1 deletion src/Selenium/Capabilities.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Prelude

import Data.Monoid (class Monoid)

foreign import data Capabilities *
foreign import data Capabilities Type
foreign import emptyCapabilities Capabilities
foreign import appendCapabilities Capabilities Capabilities Capabilities

Expand Down
12 changes: 5 additions & 7 deletions src/Selenium/Combinators.purs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
module Selenium.Combinators where

import Prelude

import Control.Alt ((<|>))
import Control.Monad.Eff.Exception (error)
import Control.Monad.Error.Class (throwError)
import Control.Monad.Trans.Class (lift)

import Data.Either (Either(..), either)
import Data.Maybe (Maybe, isJust, maybe)

import Data.Time.Duration (class Duration, Milliseconds(..))
import Selenium.Monad (Selenium, getCurrentUrl, wait, attempt, findExact, tryRepeatedlyTo, tryRepeatedlyTo', findElement, byCss, later, byClassName, byName, byId, byXPath)
import Selenium.Types (Element, Locator)

Expand All @@ -32,7 +30,7 @@ tryFind probablyLocator =
(byName probablyLocator >>= findExact) <|>
(byClassName probablyLocator >>= findExact)

waitUntilJust e o a. Selenium e o (Maybe a) Int Selenium e o a
waitUntilJust d e o a. Duration d Selenium e o (Maybe a) d Selenium e o a
waitUntilJust check time = do
wait (checker $ isJust <$> check) time
check >>= maybe (throwError $ error $ "Maybe was not Just after waiting for isJust") pure
Expand All @@ -42,7 +40,7 @@ checker ∷ ∀ e o. Selenium e o Boolean → Selenium e o Boolean
checker check =
check >>= if _
then pure true
else later 500 $ checker check
else later (Milliseconds 500.0) $ checker check

getElementByCss e o. String Selenium e o Element
getElementByCss cls =
Expand All @@ -62,7 +60,7 @@ contra check = do

-- | Repeatedly attempts to find an element using the provided selector until the
-- | provided timeout elapses.
tryToFind' e o. Int Selenium e o Locator Selenium e o Element
tryToFind' d e o. Duration d d Selenium e o Locator Selenium e o Element
tryToFind' timeout locator = tryRepeatedlyTo' timeout $ locator >>= findExact

-- | Repeatedly tries to find an element using the provided selector until
Expand All @@ -74,7 +72,7 @@ tryToFind locator = tryRepeatedlyTo $ locator >>= findExact
-- | finishes when check evaluates to true.
-- | If there is an error during check or it constantly returns `false`
-- | throws error with message (second arg)
await e o. Int Selenium e o Boolean Selenium e o Unit
await d e o. Duration d d Selenium e o Boolean Selenium e o Unit
await timeout check = do
ei ← attempt $ wait (checker check) timeout
case ei of
Expand Down
4 changes: 2 additions & 2 deletions src/Selenium/FFProfile.purs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import Selenium.Types (SELENIUM)

import Unsafe.Coerce (unsafeCoerce)

foreign import data FFProfile *
foreign import data FFPreference *
foreign import data FFProfile Type
foreign import data FFPreference Type

data Command
= SetPreference String FFPreference
Expand Down
46 changes: 18 additions & 28 deletions src/Selenium/Monad.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,30 @@
module Selenium.Monad where

import Prelude

import Control.Monad.Aff as A
import Control.Monad.Aff.Reattempt (reattempt)
import Control.Monad.Eff.Console (CONSOLE)
import Control.Monad.Eff.Exception (Error)
import Control.Monad.Eff.Ref (REF)
import Control.Monad.Reader.Trans (ReaderT(..), lift, ask, runReaderT)

import Data.Either (Either)
import Data.Foreign (Foreign)
import Data.Int as Int
import Data.List (List, fromFoldable)
import Data.Maybe (Maybe)

import Data.Time.Duration (class Duration, fromDuration, Milliseconds(..))
import DOM (DOM)

import Selenium as S
import Selenium.ActionSequence as AS
import Selenium.Types
( WindowHandle
, XHRStats
, Element
, Locator
, Driver
, FileDetector
, Location
, Size
, Window
, SELENIUM
)
import Selenium.Types (Driver, Element, FileDetector, Location, Locator, SELENIUM, Size, Window, WindowHandle, XHRStats)
import Selenium.XHR as XHR

-- | `Driver` is field of `ReaderT` context
-- | Usually selenium tests are run with tons of configs (i.e. xpath locators,
-- | timeouts) all those configs can be putted to `Selenium e o a`
type Selenium e o =
ReaderT
{driver Driver, defaultTimeout Int |o}
{driver Driver, defaultTimeout Milliseconds |o}
(A.Aff (consoleCONSOLE, seleniumSELENIUM, domDOM, refREF |e))

-- | get driver from context
Expand Down Expand Up @@ -77,28 +64,29 @@ attempt ∷ ∀ e o a. Selenium e o a → Selenium e o (Either Error a)
attempt check = ReaderT \r →
A.attempt $ runReaderT check r

later e o a. Int Selenium e o a Selenium e o a
later time check = ReaderT \r →
A.later' time $ runReaderT check r

later d e o a. Duration d d Selenium e o a Selenium e o a
later time check = ReaderT \r → do
A.delay (fromDuration time)
runReaderT check r

-- LIFT `Selenium` funcs to `Selenium.Monad`
get e o. String Selenium e o Unit
get url =
getDriver >>= lift <<< flip S.get url

wait e o. Selenium e o Boolean Int Selenium e o Unit
wait d e o. Duration d Selenium e o Boolean d Selenium e o Unit
wait check time = ReaderT \r →
S.wait (runReaderT check r) time r.driver
S.wait (runReaderT check r) (fromDuration time) r.driver

-- | Tries the provided Selenium computation repeatedly until the provided timeout expires
tryRepeatedlyTo' a e o. Int Selenium e o a Selenium e o a
tryRepeatedlyTo' d a e o. Duration d d Selenium e o a Selenium e o a
tryRepeatedlyTo' time selenium = ReaderT \r →
reattempt time (runReaderT selenium r)
reattempt (fromDuration time) (runReaderT selenium r)

-- | Tries the provided Selenium computation repeatedly until `Selenium`'s defaultTimeout expires
tryRepeatedlyTo a e o. Selenium e o a Selenium e o a
tryRepeatedlyTo selenium = ask >>= \r → tryRepeatedlyTo' r.defaultTimeout selenium
tryRepeatedlyTo selenium = ask >>= \r →
tryRepeatedlyTo' r.defaultTimeout selenium

byCss e o. String Selenium e o Locator
byCss = lift <<< S.byCss
Expand Down Expand Up @@ -210,15 +198,17 @@ sequence seq = do
-- | Same as `sequence` but takes function of `ReaderT` as an argument
actions
e o
. ({driverDriver, defaultTimeoutInt |o} AS.Sequence Unit)
. ({ driverDriver, defaultTimeoutMilliseconds | o } AS.Sequence Unit)
Selenium e o Unit
actions seqFn = do
ctx ← ask
sequence $ seqFn ctx

-- | Stop computations
stop e o. Selenium e o Unit
stop = wait (later top $ pure false) top
stop = wait (later msMax $ pure false) msMax
where
msMax = Milliseconds (Int.toNumber (top Int))

refresh e o. Selenium e o Unit
refresh = getDriver >>= S.refresh >>> lift
Expand Down
108 changes: 46 additions & 62 deletions src/Selenium/Types.purs
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
module Selenium.Types where

import Prelude

import Control.Monad.Eff (kind Effect)
import Control.Monad.Error.Class (throwError)

import Data.Foreign (readString, ForeignError(..))
import Data.Foreign.Class (class IsForeign)
import Data.Maybe (Maybe)
import Data.Foreign (F, Foreign, ForeignError(..), readString)
import Data.List.NonEmpty as NEL
import Data.Maybe (Maybe)
import Data.String (toLower)

foreign import data Builder *
foreign import data SELENIUM !
foreign import data Driver *
foreign import data Window *
foreign import data Until *
foreign import data Element *
foreign import data Locator *
foreign import data ActionSequence *
foreign import data MouseButton *
foreign import data ChromeOptions *
foreign import data ControlFlow *
foreign import data FirefoxOptions *
foreign import data IEOptions *
foreign import data LoggingPrefs *
foreign import data OperaOptions *
foreign import data ProxyConfig *
foreign import data SafariOptions *
foreign import data ScrollBehaviour *
foreign import data FileDetector *
foreign import data WindowHandle *
foreign import data Builder Type
foreign import data SELENIUM Effect
foreign import data Driver Type
foreign import data Window Type
foreign import data Until Type
foreign import data Element Type
foreign import data Locator Type
foreign import data ActionSequence Type
foreign import data MouseButton Type
foreign import data ChromeOptions Type
foreign import data ControlFlow Type
foreign import data FirefoxOptions Type
foreign import data IEOptions Type
foreign import data LoggingPrefs Type
foreign import data OperaOptions Type
foreign import data ProxyConfig Type
foreign import data SafariOptions Type
foreign import data ScrollBehaviour Type
foreign import data FileDetector Type
foreign import data WindowHandle Type

-- | Copied from `purescript-affjax` because the only thing we
-- | need from `affjax` is `Method`
Expand All @@ -46,35 +44,21 @@ data Method
| CustomMethod String

derive instance eqMethodEq Method
{-
instance eqMethod ∷ Eq Method where
eq DELETE DELETE = true
eq GET GET = true
eq HEAD HEAD = true
eq OPTIONS OPTIONS = true
eq PATCH PATCH = true
eq POST POST = true
eq PUT PUT = true
eq MOVE MOVE = true
eq COPY COPY = true
eq (CustomMethod a) (CustomMethod b) = a == b
eq _ _ = false
-}

instance methodIsForeignIsForeign Method where
read f = do
str ← readString f
pure $ case toLower str of
"delete"DELETE
"get"GET
"head"HEAD
"options"OPTIONS
"patch"PATCH
"post"POST
"put"PUT
"move"MOVE
"copy"COPY
a → CustomMethod a
readMethod Foreign F Method
readMethod f = do
str ← readString f
pure $ case toLower str of
"delete"DELETE
"get"GET
"head"HEAD
"options"OPTIONS
"patch"PATCH
"post"POST
"put"PUT
"move"MOVE
"copy"COPY
a → CustomMethod a

data XHRState
= Stale
Expand All @@ -83,14 +67,14 @@ data XHRState

derive instance eqXHRStateEq XHRState

instance xhrStateIsForeignIsForeign XHRState where
read f = do
str ← readString f
case str of
"stale" → pure Stale
"opened" → pure Opened
"loaded" → pure Loaded
_ → throwError $ NEL.singleton $ TypeMismatch "xhr state" "string"
readXHRState Foreign F XHRState
readXHRState f = do
str ← readString f
case str of
"stale" → pure Stale
"opened" → pure Opened
"loaded" → pure Loaded
_ → throwError $ NEL.singleton $ TypeMismatch "xhr state" "string"


type Location =
Expand All @@ -102,8 +86,8 @@ type Size =
{ width Int
, height Int
}
newtype ControlKey = ControlKey String

newtype ControlKey = ControlKey String

type XHRStats =
{ method Method
Expand Down
Loading

0 comments on commit ab56644

Please sign in to comment.