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 #47 from cryogenian/10-updates
Browse files Browse the repository at this point in the history
0.10.0 updates, unicode
  • Loading branch information
cryogenian authored Nov 3, 2016
2 parents a56a8a7 + 8a04f6f commit aa41c5f
Show file tree
Hide file tree
Showing 16 changed files with 508 additions and 388 deletions.
7 changes: 3 additions & 4 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
],
"license": "Apache-2.0",
"dependencies": {
"purescript-aff": "^1.1.0",
"purescript-aff-reattempt": "^1.0.0",
"purescript-base": "^1.0.0",
"purescript-dom": "^2.0.0"
"purescript-aff": "^2.0.1",
"purescript-aff-reattempt": "^2.0.0",
"purescript-dom": "^3.1.0"
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"test": "pulp test"
},
"dependencies": {
"chromedriver": "^2.22.0",
"selenium-webdriver": "2.53.2"
"chromedriver": "^2.25.1",
"selenium-webdriver": "3.0.0-beta-3"
},
"devDependencies": {
"pulp": "^9.0.1",
"purescript-psa": "^0.3.9",
"purescript": "^0.9.2",
"purescript": "^0.10.1",
"rimraf": "^2.5.4"
}
}
296 changes: 202 additions & 94 deletions src/Selenium.purs
Original file line number Diff line number Diff line change
Expand Up @@ -52,151 +52,259 @@ module Selenium

import Prelude

import Control.Monad.Aff (Aff(), attempt)
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.Foreign (Foreign)
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(..))
import Data.Unfoldable (class Unfoldable, unfoldr)

import Selenium.Types (SELENIUM, Driver, WindowHandle, Location, Window, Size, Element, FileDetector, Locator)
import Selenium.Types
( SELENIUM
, Driver
, WindowHandle
, Location
, Window
, Size
, Element
, FileDetector
, Locator
)

-- | Go to url
foreign import get :: forall e. Driver -> String -> Aff (selenium :: SELENIUM|e) Unit
foreign import get
e
. Driver
String
Aff (selenium SELENIUM|e) Unit

-- | Wait until first argument returns 'true'. If it returns false an error will be raised
foreign import wait :: forall e. Aff (selenium :: SELENIUM|e) Boolean ->
Int -> Driver ->
Aff (selenium :: SELENIUM|e) Unit
foreign import wait
e
. Aff (selenium SELENIUM|e) Boolean
Int
Driver
Aff (selenium SELENIUM|e) Unit

-- | Finalizer
foreign import quit :: forall e. Driver -> Aff (selenium :: SELENIUM|e) Unit

foreign import quit
e
. Driver
Aff (selenium SELENIUM|e) Unit

-- LOCATOR BUILDERS
foreign import byClassName :: forall e. String -> Aff (selenium :: SELENIUM|e) Locator
foreign import byCss :: forall e. String -> Aff (selenium :: SELENIUM|e) Locator
foreign import byId :: forall e. String -> Aff (selenium :: SELENIUM|e) Locator
foreign import byName :: forall e. String -> Aff (selenium :: SELENIUM|e) Locator
foreign import byXPath :: forall e. String -> Aff (selenium :: SELENIUM|e) Locator
foreign import byClassName
e. String Aff (selenium SELENIUM|e) Locator
foreign import byCss
e. String Aff (selenium SELENIUM|e) Locator
foreign import byId
e. String Aff (selenium SELENIUM|e) Locator
foreign import byName
e. String Aff (selenium SELENIUM|e) Locator
foreign import byXPath
e. String Aff (selenium SELENIUM|e) Locator
-- | Build locator from asynchronous function returning element.
-- | I.e. this locator will find first visible element with `.common-element` class
-- | ```purescript
-- | affLocator \el -> do
-- | commonElements <- byCss ".common-element" >>= findElements el
-- | flagedElements <- traverse (\el -> Tuple el <$> isVisible el) commonElements
-- | affLocator \el do
-- | commonElements byCss ".common-element" >>= findElements el
-- | flagedElements traverse (\el Tuple el <$> isVisible el) commonElements
-- | maybe err pure $ foldl foldFn Nothing flagedElements
-- | where
-- | err = throwError $ error "all common elements are not visible"
-- | foldFn Nothing (Tuple el true) = Just el
-- | foldFn a _ = a
-- | ```
foreign import affLocator :: forall e. (Element -> Aff (selenium :: SELENIUM|e) Element) -> Aff (selenium :: SELENIUM|e) Locator

foreign import showLocator :: Locator -> String

foreign import _findElement :: forall e a. Maybe a -> (a -> Maybe a) ->
Driver -> Locator -> Aff (selenium :: SELENIUM|e) (Maybe Element)
foreign import _findChild :: forall e a. Maybe a -> (a -> Maybe a) ->
Element -> Locator -> Aff (selenium :: SELENIUM|e) (Maybe Element)
foreign import _findElements :: forall e. Driver -> Locator -> Aff (selenium :: SELENIUM|e) (Array Element)
foreign import _findChildren :: forall e. Element -> Locator -> Aff (selenium :: SELENIUM|e) (Array Element)
foreign import findExact :: forall e. Driver -> Locator -> Aff (selenium :: SELENIUM|e) Element
foreign import childExact :: forall e. Element -> Locator -> Aff (selenium :: SELENIUM|e) Element
foreign import affLocator
e
. (Element Aff (selenium SELENIUM|e) Element)
Aff (selenium SELENIUM|e) Locator

foreign import showLocator
Locator
String

foreign import _findElement
e a
. Maybe a
(a Maybe a)
Driver
Locator
Aff (selenium SELENIUM|e) (Maybe Element)

foreign import _findChild
e a
. Maybe a
(a Maybe a)
Element
Locator
Aff (selenium SELENIUM|e) (Maybe Element)

foreign import _findElements
e
. Driver
Locator
Aff (selenium SELENIUM|e) (Array Element)

foreign import _findChildren
e
. Element
Locator
Aff (selenium SELENIUM|e) (Array Element)

foreign import findExact
e
. Driver
Locator
Aff (selenium SELENIUM|e) Element

foreign import childExact
e
. Element
Locator
Aff (selenium SELENIUM|e) Element

-- | Tries to find an element starting from `document`; will return `Nothing` if there
-- | is no element can be found by locator
findElement :: forall e. Driver -> Locator -> Aff (selenium :: SELENIUM|e) (Maybe Element)
findElement = _findElement Nothing Just
findElement
e. Driver Locator Aff (seleniumSELENIUM|e) (Maybe Element)
findElement =
_findElement Nothing Just

-- | Tries to find element and throws an error if it succeeds.
loseElement :: forall e. Driver -> Locator -> Aff (selenium :: SELENIUM|e) Unit
loseElement
e
. Driver
Locator
Aff (seleniumSELENIUM|e) Unit
loseElement driver locator = do
result <- attempt $ findExact driver locator
result attempt $ findExact driver locator
either (const $ pure unit) (const $ throwError $ error failMessage) result
where
failMessage = "Found element with locator: " <> showLocator locator

-- | Finds elements by locator from `document`
findElements :: forall e f. (Unfoldable f) => Driver -> Locator -> Aff (selenium :: SELENIUM|e) (f Element)
findElements driver locator = map fromArray $ _findElements driver locator
findElements
e f. (Unfoldable f) Driver Locator Aff (seleniumSELENIUM|e) (f Element)
findElements driver locator =
map fromArray $ _findElements driver locator

-- | Same as `findElement` but starts searching from custom element
findChild :: forall e. Element -> Locator -> Aff (selenium :: SELENIUM|e) (Maybe Element)
findChild = _findChild Nothing Just
findChild
e. Element Locator Aff (seleniumSELENIUM|e) (Maybe Element)
findChild =
_findChild Nothing Just

-- | Same as `findElements` but starts searching from custom element
findChildren :: forall e f. (Unfoldable f) => Element -> Locator -> Aff (selenium ::SELENIUM|e) (f Element)
findChildren el locator = map fromArray $ _findChildren el locator

foreign import setFileDetector :: forall e. Driver -> FileDetector -> Aff (selenium :: SELENIUM|e) Unit

foreign import navigateBack :: forall e. Driver -> Aff (selenium :: SELENIUM|e) Unit
foreign import navigateForward :: forall e. Driver -> Aff (selenium :: SELENIUM|e) Unit
foreign import refresh :: forall e. Driver -> Aff (selenium :: SELENIUM|e) Unit
foreign import navigateTo :: forall e. String -> Driver -> Aff (selenium :: SELENIUM|e) Unit
foreign import getCurrentUrl :: forall e. Driver -> Aff (selenium :: SELENIUM|e) String
foreign import getTitle :: forall e. Driver -> Aff (selenium :: SELENIUM|e) String
findChildren
e f
. (Unfoldable f)
Element
Locator
Aff (seleniumSELENIUM|e) (f Element)
findChildren el locator =
map fromArray $ _findChildren el locator

foreign import setFileDetector
e. Driver FileDetector Aff (selenium SELENIUM|e) Unit
foreign import navigateBack
e. Driver Aff (selenium SELENIUM|e) Unit
foreign import navigateForward
e. Driver Aff (selenium SELENIUM|e) Unit
foreign import refresh
e. Driver Aff (selenium SELENIUM|e) Unit
foreign import navigateTo
e. String Driver Aff (selenium SELENIUM|e) Unit
foreign import getCurrentUrl
e. Driver Aff (selenium SELENIUM|e) String
foreign import getTitle
e. Driver Aff (selenium SELENIUM|e) String
-- | Executes javascript script from `String` argument.
foreign import executeStr :: forall e. Driver -> String -> Aff (selenium :: SELENIUM|e) Foreign

foreign import sendKeysEl :: forall e. String -> Element -> Aff (selenium :: SELENIUM|e) Unit
foreign import clickEl :: forall e. Element -> Aff (selenium :: SELENIUM|e) Unit
foreign import getCssValue :: forall e. Element -> String -> Aff (selenium :: SELENIUM|e) String
foreign import _getAttribute :: forall e a. Maybe a -> (a -> Maybe a) ->
Element -> String -> Aff (selenium :: SELENIUM|e) (Maybe String)
foreign import executeStr
e. Driver String Aff (selenium SELENIUM|e) Foreign
foreign import sendKeysEl
e. String Element Aff (selenium SELENIUM|e) Unit
foreign import clickEl
e. Element Aff (selenium SELENIUM|e) Unit
foreign import getCssValue
e. Element String Aff (selenium SELENIUM|e) String
foreign import _getAttribute
e a
. Maybe a
(a Maybe a)
Element
String
Aff (selenium SELENIUM|e) (Maybe String)

-- | Tries to find an element starting from `document`; will return `Nothing` if there
-- | is no element can be found by locator
getAttribute :: forall e. Element -> String -> Aff (selenium :: SELENIUM|e) (Maybe String)
getAttribute
e. Element String Aff (seleniumSELENIUM|e) (Maybe String)
getAttribute = _getAttribute Nothing Just

foreign import getText :: forall e. Element -> Aff (selenium :: SELENIUM|e) String
foreign import isDisplayed :: forall e. Element -> Aff (selenium :: SELENIUM|e) Boolean
foreign import isEnabled :: forall e. Element -> Aff (selenium :: SELENIUM|e) Boolean
foreign import getInnerHtml :: forall e. Element -> Aff (selenium :: SELENIUM|e) String
foreign import getSize :: forall e. Element -> Aff (selenium :: SELENIUM|e) Size
foreign import getLocation :: forall e. Element -> Aff (selenium :: SELENIUM|e) Location
foreign import getText
e. Element Aff (selenium SELENIUM|e) String
foreign import isDisplayed
e. Element Aff (selenium SELENIUM|e) Boolean
foreign import isEnabled
e. Element Aff (selenium SELENIUM|e) Boolean
foreign import getInnerHtml
e. Element Aff (selenium SELENIUM|e) String
foreign import getSize
e. Element Aff (selenium SELENIUM|e) Size
foreign import getLocation
e. Element Aff (selenium SELENIUM|e) Location
-- | Clear `value` of element, if it has no value will do nothing.
-- | If `value` is weakly referenced by `virtual-dom` (`purescript-halogen`)
-- | will not work -- to clear such inputs one should use direct signal from
-- | `Selenium.ActionSequence`
foreign import clearEl :: forall e. Element -> Aff (selenium :: SELENIUM|e) Unit
foreign import clearEl
e. Element Aff (selenium SELENIUM|e) Unit

-- | Returns png base64 encoded png image
foreign import takeScreenshot :: forall e. Driver -> Aff (selenium :: SELENIUM |e) String
foreign import takeScreenshot
e. Driver Aff (selenium SELENIUM |e) String

-- | Saves screenshot to path
foreign import saveScreenshot :: forall e. String -> Driver -> Aff (selenium :: SELENIUM |e) Unit

foreign import getWindow :: forall e. Driver -> Aff (selenium :: SELENIUM|e) Window

foreign import getWindowPosition :: forall e. Window -> Aff (selenium :: SELENIUM|e) Location

foreign import getWindowSize :: forall e. Window -> Aff (selenium :: SELENIUM|e) Size

foreign import maximizeWindow :: forall e. Window -> Aff (selenium :: SELENIUM|e) Unit

foreign import setWindowPosition :: forall e. Location -> Window -> Aff (selenium :: SELENIUM|e) Unit

foreign import setWindowSize :: forall e. Size -> Window -> Aff (selenium :: SELENIUM|e) Unit

foreign import getWindowScroll :: forall e. Driver -> Aff (selenium :: SELENIUM|e) Location

foreign import getWindowHandle :: forall e. Driver -> Aff (selenium :: SELENIUM|e) WindowHandle

foreign import _getAllWindowHandles :: forall e. Driver -> Aff (selenium :: SELENIUM|e) (Array WindowHandle)

getAllWindowHandles :: forall f e. (Unfoldable f) => Driver -> Aff (selenium :: SELENIUM |e) (f WindowHandle)
getAllWindowHandles driver = map fromArray $ _getAllWindowHandles driver


fromArray :: forall a f. (Unfoldable f) => Array a -> f a
fromArray = unfoldr (\xs -> (\rec -> Tuple rec.head rec.tail) <$> uncons xs)

foreign import switchTo :: forall e. WindowHandle -> Driver -> Aff (selenium :: SELENIUM |e) Unit

foreign import close :: forall e. Driver -> Aff (selenium :: SELENIUM |e) Unit
foreign import saveScreenshot
e. String Driver Aff (selenium SELENIUM |e) Unit
foreign import getWindow
e. Driver Aff (selenium SELENIUM|e) Window
foreign import getWindowPosition
e. Window Aff (selenium SELENIUM|e) Location
foreign import getWindowSize
e. Window Aff (selenium SELENIUM|e) Size
foreign import maximizeWindow
e. Window Aff (selenium SELENIUM|e) Unit
foreign import setWindowPosition
e. Location Window Aff (selenium SELENIUM|e) Unit
foreign import setWindowSize
e. Size Window Aff (selenium SELENIUM|e) Unit
foreign import getWindowScroll
e. Driver Aff (selenium SELENIUM|e) Location
foreign import getWindowHandle
e. Driver Aff (selenium SELENIUM|e) WindowHandle
foreign import _getAllWindowHandles
e. Driver Aff (selenium SELENIUM|e) (Array WindowHandle)

getAllWindowHandles
f e
. (Unfoldable f)
Driver
Aff (seleniumSELENIUM |e) (f WindowHandle)
getAllWindowHandles driver =
map fromArray $ _getAllWindowHandles driver

fromArray
a f. (Unfoldable f) Array a f a
fromArray =
unfoldr (\xs → (\rec → Tuple rec.head rec.tail) <$> uncons xs)

foreign import switchTo
e. WindowHandle Driver Aff (selenium SELENIUM |e) Unit
foreign import close
e. Driver Aff (selenium SELENIUM |e) Unit
Loading

0 comments on commit aa41c5f

Please sign in to comment.