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

Commit c71a61f

Browse files
committed
Merge pull request #27 from beckyconning/ready/looseExact
Added looseExact function
2 parents a69d288 + 8d323ca commit c71a61f

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

docs/Selenium.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ affLocator \el -> do
7373
foldFn a _ = a
7474
```
7575

76+
#### `showLocator`
77+
78+
``` purescript
79+
showLocator :: Locator -> String
80+
```
81+
7682
#### `findExact`
7783

7884
``` purescript
@@ -94,6 +100,14 @@ findElement :: forall e. Driver -> Locator -> Aff (selenium :: SELENIUM | e) (Ma
94100
Tries to find an element starting from `document` will return `Nothing` if there
95101
is no element can be found by locator
96102

103+
#### `loseElement`
104+
105+
``` purescript
106+
loseElement :: forall e. Driver -> Locator -> Aff (selenium :: SELENIUM | e) Unit
107+
```
108+
109+
Tries to find element and throws an error if it succeeds.
110+
97111
#### `findElements`
98112

99113
``` purescript

docs/Selenium/Monad.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,14 @@ findExact :: forall e o. Locator -> Selenium e o Element
344344

345345
Tries to find element, if has no success throws an error
346346

347+
#### `loseElement`
348+
349+
``` purescript
350+
loseElement :: forall e o. Locator -> Selenium e o Unit
351+
```
352+
353+
Tries to find element and throws an error if it succeeds.
354+
347355
#### `childExact`
348356

349357
``` purescript

src/Selenium.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ function _exact(driver) {
132132
};
133133
}
134134

135+
exports.showLocator = function(locator) {
136+
return locator.toString();
137+
}
138+
135139
exports.findExact = _exact;
136140
exports.childExact = _exact;
137141

src/Selenium.purs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ module Selenium
99
, byXPath
1010
, affLocator
1111
, findElement
12+
, loseElement
1213
, findElements
1314
, findChild
1415
, findChildren
1516
, findExact
17+
, showLocator
1618
, childExact
1719
, navigateBack
1820
, navigateForward
@@ -46,9 +48,12 @@ module Selenium
4648

4749
import Prelude
4850
import Control.Monad.Eff (Eff())
51+
import Control.Monad.Eff.Exception (error)
52+
import Control.Monad.Error.Class (throwError)
4953
import Data.Maybe (Maybe())
54+
import Data.Either (either)
5055
import Control.Monad.Eff.Class (liftEff)
51-
import Control.Monad.Aff (Aff())
56+
import Control.Monad.Aff (Aff(), attempt)
5257
import Selenium.Types
5358
import Data.Unfoldable (Unfoldable, unfoldr)
5459
import Data.Foreign (Foreign())
@@ -88,6 +93,7 @@ foreign import byXPath :: forall e. String -> Aff (selenium :: SELENIUM|e) Locat
8893
-- | ```
8994
foreign import affLocator :: forall e. (Element -> Aff (selenium :: SELENIUM|e) Element) -> Aff (selenium :: SELENIUM|e) Locator
9095

96+
foreign import showLocator :: Locator -> String
9197

9298
foreign import _findElement :: forall e a. Maybe a -> (a -> Maybe a) ->
9399
Driver -> Locator -> Aff (selenium :: SELENIUM|e) (Maybe Element)
@@ -103,6 +109,14 @@ foreign import childExact :: forall e. Element -> Locator -> Aff (selenium :: SE
103109
findElement :: forall e. Driver -> Locator -> Aff (selenium :: SELENIUM|e) (Maybe Element)
104110
findElement = _findElement Nothing Just
105111

112+
-- | Tries to find element and throws an error if it succeeds.
113+
loseElement :: forall e. Driver -> Locator -> Aff (selenium :: SELENIUM|e) Unit
114+
loseElement driver locator = do
115+
result <- attempt $ findExact driver locator
116+
either (const $ pure unit) (const $ throwError $ error failMessage) result
117+
where
118+
failMessage = "Found element with locator: " ++ showLocator locator
119+
106120
-- | Finds elements by locator from `document`
107121
findElements :: forall e f. (Unfoldable f) => Driver -> Locator -> Aff (selenium :: SELENIUM|e) (f Element)
108122
findElements driver locator =

src/Selenium/Monad.purs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Data.Either (Either())
1010
import Data.Maybe (Maybe())
1111
import Data.Foreign (Foreign())
1212
import Data.Maybe.Unsafe (fromJust)
13+
import Data.Either (either)
1314
import Data.List
1415
import DOM
1516
import Selenium.Types
@@ -218,6 +219,10 @@ saveScreenshot name = getDriver >>= S.saveScreenshot name >>> lift
218219
findExact :: forall e o. Locator -> Selenium e o Element
219220
findExact loc = getDriver >>= flip S.findExact loc >>> lift
220221

222+
-- | Tries to find element and throws an error if it succeeds.
223+
loseElement :: forall e o. Locator -> Selenium e o Unit
224+
loseElement loc = getDriver >>= flip S.loseElement loc >>> lift
225+
221226
-- | Tries to find child, if has no success throws an error
222227
childExact :: forall e o. Element -> Locator -> Selenium e o Element
223228
childExact el loc = lift $ S.childExact el loc

0 commit comments

Comments
 (0)