git.iko.soy/rowtype-yoga/purescript-lunapark
on main help sign in
starting actions
commit0ee738bfadcc2789c9453f78e563535438fe43b2 authorMaxim Zimaliev <zimaliev@gmail.com> dateMon 9 Apr 2018, 17:05 (9 years ago) parentb17db2f
4 files changed+186−14
bower.json+2−1 src/Lunapark/.#API.purs−1 src/Lunapark/API.purs+99 src/Lunapark/Types.purs+85−12
bower.json+2−1
@@ -5,6 +5,7 @@
55 "purescript-debug": "^3.0.0",
66 "purescript-codec-argonaut": "^5.0.0",
77 "purescript-argonaut": "^3.1.0",
8− "purescript-css": "^3.4.0"
8+ "purescript-css": "^3.4.0",
9+ "purescript-node-fs-aff": "^5.0.0"
910 }
1011}
src/Lunapark/.#API.pursdeleted−1
@@ -1 +0,0 @@
1−User@DESKTOP-4VI4KLM.9476:1522320097
src/Lunapark/API.purs+99
@@ -353,3 +353,102 @@ getPageSource = do
353353 r ← ask
354354 res ← rethrow $ LA.getPageSource r.baseURI (un LT.SessionId r.session)
355355 wrapEither $ J.decodeJson res
356+
357+executeScript ∷ ∀ m e. LunaparkConstraints e m (LT.Script → m J.Json)
358+executeScript scr = do
359+ r ← ask
360+ rethrow $ LA.executeScript
361+ r.baseURI
362+ (un LT.SessionId r.session)
363+ (LT.encodeScript scr)
364+
365+executeAsyncScript ∷ ∀ m e. LunaparkConstraints e m (LT.Script → m J.Json)
366+executeAsyncScript scr = do
367+ r ← ask
368+ rethrow $ LA.executeAsyncScript
369+ r.baseURI
370+ (un LT.SessionId r.session)
371+ (LT.encodeScript scr)
372+
373+getAllCookies ∷ ∀ m e. LunaparkConstraints e m (m (Array LT.Cookie))
374+getAllCookies = do
375+ r ← ask
376+ res ← rethrow $ LA.getAllCookies r.baseURI $ un LT.SessionId r.session
377+ wrapEither $ T.traverse LT.decodeCookie =<< J.decodeJson res
378+
379+getNamedCookie ∷ ∀ m e. LunaparkConstraints e m (String → m LT.Cookie)
380+getNamedCookie name = do
381+ r ← ask
382+ res ← rethrow $ LA.getNamedCookie r.baseURI (un LT.SessionId r.session) name
383+ wrapEither $ LT.decodeCookie res
384+
385+addCookie ∷ ∀ m e. LunaparkConstraints e m (LT.Cookie → m Unit)
386+addCookie cookie = do
387+ r ← ask
388+ void $ rethrow $ LA.addCookie
389+ r.baseURI
390+ (un LT.SessionId r.session)
391+ (LT.encodeCookie cookie)
392+
393+deleteCookie ∷ ∀ m e. LunaparkConstraints e m (String → m Unit)
394+deleteCookie name = do
395+ r ← ask
396+ void $ rethrow $ LA.deleteCookie
397+ r.baseURI
398+ (un LT.SessionId r.session)
399+ name
400+
401+deleteAllCookies ∷ ∀ m e. LunaparkConstraints e m (m Unit)
402+deleteAllCookies = do
403+ r ← ask
404+ void $ rethrow $ LA.deleteAllCookies r.baseURI $ un LT.SessionId r.session
405+
406+{-
407+performActions
408+-}
409+
410+releaseActions ∷ ∀ m e. LunaparkConstraints e m (m Unit)
411+releaseActions = do
412+ r ← ask
413+ void $ rethrow $ LA.releaseActions r.baseURI $ un LT.SessionId r.session
414+
415+dismissAlert ∷ ∀ m e. LunaparkConstraints e m (m Unit)
416+dismissAlert = do
417+ r ← ask
418+ void $ rethrow $ LA.dismissAlert r.baseURI $ un LT.SessionId r.session
419+
420+acceptAlert ∷ ∀ m e. LunaparkConstraints e m (m Unit)
421+acceptAlert = do
422+ r ← ask
423+ void $ rethrow $ LA.acceptAlert r.baseURI $ un LT.SessionId r.session
424+
425+getAlertText ∷ ∀ m e. LunaparkConstraints e m (m String)
426+getAlertText = do
427+ r ← ask
428+ res ← rethrow $ LA.getAlertText r.baseURI $ un LT.SessionId r.session
429+ wrapEither $ J.decodeJson res
430+
431+sendAlertText ∷ ∀ m e. LunaparkConstraints e m (String → m Unit)
432+sendAlertText s = do
433+ r ← ask
434+ void $ rethrow $ LA.sendAlertText
435+ r.baseURI
436+ (un LT.SessionId r.session)
437+ (LT.encodeSendKeysRequest s)
438+
439+takeScreenshot ∷ ∀ m e. LunaparkConstraints e m (m LT.Screenshot)
440+takeScreenshot = do
441+ r ← ask
442+ res ← rethrow $ LA.takeScreenshot
443+ r.baseURI
444+ (un LT.SessionId r.session)
445+ wrapEither $ LT.decodeScreenshot res
446+
447+takeElementScreenshot ∷ ∀ m e. LunaparkConstraints e m (LT.Element → m LT.Screenshot)
448+takeElementScreenshot el = do
449+ r ← ask
450+ res ← rethrow LA.takeElementScreenshot
451+ r.baseURI
452+ (un LT.SessionId r.session)
453+ (un LT.Element el)
454+ wrapEither $ LT.decodeScreenshot res
src/Lunapark/Types.purs+85−12
@@ -5,14 +5,18 @@ import Prelude
55import Control.Alt ((<|>))
66import CSS.Selector as CSS
77import CSS.Render as CSSR
8−import Data.Argonaut (Json)
8+import Data.Argonaut (Json, (.?))
99import Data.Argonaut as J
10+import Data.Array as A
11+import Data.Foldable as F
1012import Data.Time.Duration (Milliseconds(..))
1113import Data.Either (Either(..))
14+import Data.Maybe (Maybe(..))
1215import Data.Newtype (class Newtype, un)
1316import Data.StrMap as SM
1417import Data.Tuple (Tuple(..))
1518import Data.XPath.Types as X
19+import Node.Encoding as NE
1620import Unsafe.Coerce (unsafeCoerce)
1721
1822newtype SessionId = SessionId String
@@ -28,7 +32,7 @@ derive instance newtypeElementId ∷ Newtype Element _
2832
2933decodeElement ∷ Json → Either String Element
3034decodeElement = J.decodeJson >=> \obj →
31− map Element $ obj J..? "element-6066-11e4-a52e-4f735466cecf" <|> obj J..? "ELEMENT"
35+ map Element $ obj .? "element-6066-11e4-a52e-4f735466cecf" <|> obj .? "ELEMENT"
3236
3337encodeElement ∷ Element → Json
3438encodeElement (Element eid) = J.encodeJson $ SM.fromFoldable
@@ -54,8 +58,8 @@ type CreateSessionResponse =
5458
5559decodeCreateSessionResponse ∷ Json → Either String CreateSessionResponse
5660decodeCreateSessionResponse = J.decodeJson >=> \obj → do
57− session ← decodeSessionId =<< obj J..? "sessionId"
58− capabilities ← decodeCapabilities =<< obj J..? "capabilities"
61+ session ← decodeSessionId =<< obj .? "sessionId"
62+ capabilities ← decodeCapabilities =<< obj .? "capabilities"
5963 pure { session, capabilities }
6064
6165type ServerStatus =
@@ -64,7 +68,7 @@ type ServerStatus =
6468 }
6569
6670decodeServerStatus ∷ Json → Either String ServerStatus
67−decodeServerStatus = J.decodeJson >=> \obj → { ready: _, message: _ } <$> obj J..? "ready" <*> obj J..? "message"
71+decodeServerStatus = J.decodeJson >=> \obj → { ready: _, message: _ } <$> obj .? "ready" <*> obj .? "message"
6872
6973type Timeouts =
7074 { script ∷ Milliseconds
@@ -74,9 +78,9 @@ type Timeouts =
7478
7579decodeTimeouts ∷ Json → Either String Timeouts
7680decodeTimeouts = J.decodeJson >=> \obj → do
77− script ← map Milliseconds $ obj J..? "script"
78− pageLoad ← map Milliseconds $ obj J..? "pageLoad"
79− implicit ← map Milliseconds $ obj J..? "implicit"
81+ script ← map Milliseconds $ obj .? "script"
82+ pageLoad ← map Milliseconds $ obj .? "pageLoad"
83+ implicit ← map Milliseconds $ obj .? "implicit"
8084 pure { script, pageLoad, implicit }
8185
8286encodeTimeouts ∷ Timeouts → Json
@@ -112,10 +116,10 @@ type Rectangle =
112116
113117decodeRectangle ∷ Json → Either String Rectangle
114118decodeRectangle = J.decodeJson >=> \obj → do
115− width ← obj J..? "width"
116− height ← obj J..? "height"
117− x ← obj J..? "x"
118− y ← obj J..? "y"
119+ width ← obj .? "width"
120+ height ← obj .? "height"
121+ x ← obj .? "x"
122+ y ← obj .? "y"
119123 pure { width, height, x, y }
120124
121125encodeRectangle ∷ Rectangle → Json
@@ -158,3 +162,72 @@ encodeLocator l = J.encodeJson $ SM.fromFoldable case l of
158162
159163encodeSendKeysRequest ∷ String → Json
160164encodeSendKeysRequest txt = J.encodeJson $ SM.fromFoldable [ Tuple "text" txt ]
165+
166+type Script =
167+ { script ∷ String
168+ , args ∷ Array Json
169+ }
170+
171+encodeScript ∷ Script → Json
172+encodeScript r = J.encodeJson $ SM.fromFoldable
173+ [ Tuple "script" $ J.encodeJson r.script
174+ , Tuple "args" $ J.encodeJson r.args
175+ ]
176+
177+type Cookie =
178+ { name ∷ String
179+ , value ∷ String
180+ , path ∷ Maybe String
181+ , domain ∷ Maybe String
182+ , secure ∷ Maybe Boolean
183+ , httpOnly ∷ Maybe Boolean
184+ , expiry ∷ Maybe Int
185+ }
186+
187+encodeCookie ∷ Cookie → Json
188+encodeCookie r = J.encodeJson $ SM.fromFoldable
189+ [ Tuple "cookie" $ SM.fromFoldable
190+ $ [ Tuple "name" $ J.encodeJson r.name
191+ , Tuple "value" $ J.encodeJson r.value
192+ ]
193+ <> maybeToAOfPair "path" r.path
194+ <> maybeToAOfPair "domain" r.domain
195+ <> maybeToAOfPair "secure" r.secure
196+ <> maybeToAOfPair "httpOnly" r.httpOnly
197+ <> maybeToAOfPair "expiry" r.expiry
198+ ]
199+ where
200+ maybeToAOfPair ∷ ∀ a. J.EncodeJson a ⇒ String → Maybe a → Array (Tuple String J.Json)
201+ maybeToAOfPair key mb = F.foldMap (A.singleton <<< Tuple key <<< J.encodeJson) mb
202+
203+decodeCookie ∷ Json → Either String Cookie
204+decodeCookie = J.decodeJson >=> \obj → do
205+ name ← obj .? "name"
206+ value ← obj .? "value"
207+ path ← maybify $ obj .? "path"
208+ domain ← maybify $ obj .? "domain"
209+ secure ← maybify $ obj .? "secure"
210+ httpOnly ← maybify $ obj .? "httpOnly"
211+ expiry ← maybify $ obj .? "expiry"
212+ pure
213+ { name
214+ , value
215+ , path
216+ , domain
217+ , secure
218+ , httpOnly
219+ , expiry
220+ }
221+ where
222+ maybify ∷ ∀ a b. Either a b → Either a (Maybe b)
223+ maybify e = map Just e <|> Right Nothing
224+
225+
226+type Screenshot =
227+ { content ∷ String
228+ , encoding ∷ NE.Encoding
229+ }
230+
231+decodeScreenshot ∷ Json → Either String Screenshot
232+decodeScreenshot j =
233+ { content: _, encoding: NE.Base64 } <$> J.decodeJson j