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

Commit 8268f76

Browse files
committed
Merge pull request #25 from beckyconning/ready/repeatedlyTryTo
Added repeatedlyTryTo function
2 parents 3e6381f + e1a0099 commit 8268f76

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
],
1818
"license": "Apache-2.0",
1919
"dependencies": {
20-
"purescript-base": "^0.2.0",
2120
"purescript-aff": "^0.11.3",
21+
"purescript-aff-reattempt": "^0.1.0",
22+
"purescript-base": "^0.2.0",
2223
"purescript-dom": "^0.2.4"
2324
}
2425
}

docs/Selenium/Monad.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ putting `Driver` to `ReaderT`
77
#### `Selenium`
88

99
``` purescript
10-
type Selenium e o a = ReaderT { driver :: Driver | o } (Aff (console :: CONSOLE, selenium :: SELENIUM, dom :: DOM | e)) a
10+
type Selenium e o a = ReaderT { driver :: Driver, defaultTimeout :: Int | o } (Aff (console :: CONSOLE, selenium :: SELENIUM, dom :: DOM, ref :: REF | e)) a
1111
```
1212

1313
`Driver` is field of `ReaderT` context
@@ -94,6 +94,22 @@ get :: forall e o. String -> Selenium e o Unit
9494
wait :: forall e o. Selenium e o Boolean -> Int -> Selenium e o Unit
9595
```
9696

97+
#### `tryRepeatedlyTo'`
98+
99+
``` purescript
100+
tryRepeatedlyTo' :: forall a e o. Int -> Selenium e o a -> Selenium e o a
101+
```
102+
103+
Tries the provided Selenium computation repeatedly until the provided timeout expires
104+
105+
#### `tryRepeatedlyTo`
106+
107+
``` purescript
108+
tryRepeatedlyTo :: forall a e o. Selenium e o a -> Selenium e o a
109+
```
110+
111+
Tries the provided Selenium computation repeatedly until `Selenium`'s defaultTimeout expires
112+
97113
#### `byCss`
98114

99115
``` purescript
@@ -283,7 +299,7 @@ Run sequence of actions
283299
#### `actions`
284300

285301
``` purescript
286-
actions :: forall e o. ({ driver :: Driver | o } -> Sequence Unit) -> Selenium e o Unit
302+
actions :: forall e o. ({ driver :: Driver, defaultTimeout :: Int | o } -> Sequence Unit) -> Selenium e o Unit
287303
```
288304

289305
Same as `sequence` but takes function of `ReaderT` as an argument

src/Selenium.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ foreign import get :: forall e. Driver -> String -> Aff (selenium :: SELENIUM|e)
6363
foreign import wait :: forall e. Aff (selenium :: SELENIUM|e) Boolean ->
6464
Int -> Driver ->
6565
Aff (selenium :: SELENIUM|e) Unit
66+
6667
-- | Finalizer
6768
foreign import quit :: forall e. Driver -> Aff (selenium :: SELENIUM|e) Unit
6869

src/Selenium/Combinators.purs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,6 @@ contra check = do
5959
(const $ pure unit)
6060
(const $ throwError $ error "check successed in contra") eR
6161

62-
-- | takes value and repeatedly tries to evaluate it for timeout of ms (second arg)
63-
-- | if it evaluates w/o error returns its value
64-
-- | else throws error
65-
waiter :: forall e o a. Selenium e o a -> Int -> Selenium e o a
66-
waiter getter timeout = do
67-
wait (checker $ (isRight <$> attempt getter)) timeout
68-
getter
69-
70-
waitExistentCss :: forall e o. String -> Int -> Selenium e o Element
71-
waitExistentCss css timeout =
72-
waiter (getElementByCss css) timeout
73-
74-
waitNotExistentCss :: forall e o. String -> Int -> Selenium e o Unit
75-
waitNotExistentCss css timeout =
76-
waiter (checkNotExistsByCss css) timeout
77-
78-
7962
-- | Repeatedly tries to evaluate check (third arg) for timeout ms (first arg)
8063
-- | finishes when check evaluates to true.
8164
-- | If there is an error during check or it constantly returns `false`
@@ -87,6 +70,5 @@ await timeout check = do
8770
Left _ -> throwError $ error "await has no success"
8871
Right _ -> pure unit
8972

90-
9173
awaitUrlChanged :: forall e o. String -> Selenium e o Boolean
9274
awaitUrlChanged oldURL = checker $ (oldURL /=) <$> getCurrentUrl

src/Selenium/Monad.purs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import Data.List
1414
import DOM
1515
import Selenium.Types
1616
import Control.Monad.Eff.Console (CONSOLE())
17+
import Control.Monad.Eff.Ref (REF())
1718
import Control.Monad.Trans
1819
import Control.Monad.Reader.Trans
1920
import Control.Monad.Reader.Class
2021
import qualified Control.Monad.Aff as A
22+
import qualified Control.Monad.Aff.Reattempt as A
2123
import qualified Selenium as S
2224
import qualified Selenium.ActionSequence as S
2325
import qualified Selenium.XHR as S
@@ -26,8 +28,8 @@ import qualified Selenium.XHR as S
2628
-- | timeouts) all those configs can be putted to `Selenium e o a`
2729
type Selenium e o a =
2830
ReaderT
29-
{driver :: Driver |o}
30-
(A.Aff (console :: CONSOLE, selenium :: SELENIUM, dom :: DOM |e)) a
31+
{driver :: Driver, defaultTimeout :: Int |o}
32+
(A.Aff (console :: CONSOLE, selenium :: SELENIUM, dom :: DOM, ref :: REF |e)) a
3133

3234
-- | get driver from context
3335
getDriver :: forall e o. Selenium e o Driver
@@ -77,6 +79,15 @@ wait :: forall e o. Selenium e o Boolean -> Int -> Selenium e o Unit
7779
wait check time = ReaderT \r ->
7880
S.wait (runReaderT check r) time r.driver
7981

82+
-- | Tries the provided Selenium computation repeatedly until the provided timeout expires
83+
tryRepeatedlyTo' :: forall a e o. Int -> Selenium e o a -> Selenium e o a
84+
tryRepeatedlyTo' time selenium = ReaderT \r ->
85+
A.reattempt time (runReaderT selenium r)
86+
87+
-- | Tries the provided Selenium computation repeatedly until `Selenium`'s defaultTimeout expires
88+
tryRepeatedlyTo :: forall a e o. Selenium e o a -> Selenium e o a
89+
tryRepeatedlyTo selenium = ask >>= \r -> tryRepeatedlyTo' r.defaultTimeout selenium
90+
8091
byCss :: forall e o. String -> Selenium e o Locator
8192
byCss = lift <<< S.byCss
8293

@@ -182,7 +193,7 @@ sequence seq = do
182193
getDriver >>= lift <<< flip S.sequence seq
183194

184195
-- | Same as `sequence` but takes function of `ReaderT` as an argument
185-
actions :: forall e o. ({driver :: Driver |o} -> S.Sequence Unit) -> Selenium e o Unit
196+
actions :: forall e o. ({driver :: Driver, defaultTimeout :: Int |o} -> S.Sequence Unit) -> Selenium e o Unit
186197
actions seqFn = do
187198
ctx <- ask
188199
sequence $ seqFn ctx

0 commit comments

Comments
 (0)