@@ -2,7 +2,6 @@ module Lunapark.API where
22
33import Prelude
44
5−import Control.Monad.Aff (Aff)
65import Control.Monad.Aff as Aff
76import Control.Monad.Aff.Class (class MonadAff, liftAff)
87import Control.Monad.Eff.Ref as Ref
@@ -15,7 +14,6 @@ import Data.List (List(..), (:))
1514import Data.List as L
1615import Data.Map as Map
1716import Data.Maybe (Maybe(..))
18−import Data.Symbol (SProxy(..))
1917import Data.StrMap as SM
2018import Data.String as Str
2119import Data.Variant as V
@@ -24,13 +22,14 @@ import Data.Traversable as T
2422import Lunapark.Endpoint as LP
2523import Lunapark.Error as LE
2624import Lunapark.Types as LT
25+import Lunapark.LunaparkF (_lunapark, LUNAPARK, ElementF(..), LunaparkF(..), performActions, findElement)
26+import Lunapark.ActionF (_lunaparkActions, LUNAPARK_ACTIONS, ActionF(..), TouchF(..))
27+import Lunapark.Utils (liftAndRethrow, throwLeft, catch)
2728import Network.HTTP.Affjax (AJAX)
2829import Node.Buffer as B
2930import Node.FS.Aff as FS
3031import Run as R
3132import Run.Except (EXCEPT)
32−import Run.Except as RE
33−import Unsafe.Coerce (unsafeCoerce)
3433
3534type LunaparkEffects e =
3635 ( ajax ∷ AJAX
@@ -101,88 +100,6 @@ interpret
101100 ~> BaseRun e r
102101interpret input = runLunapark input <<< runLunaparkActions input
103102
104−data LunaparkF a
105− = Quit a
106− | Status (LT.ServerStatus → a)
107− | GetTimeouts (LT.Timeouts → a)
108− | SetTimeouts LT.Timeouts a
109− | GoTo String a
110− | GetUrl (String → a)
111− | Forward a
112− | Back a
113− | Refresh a
114− | GetTitle (String → a)
115− | GetWindowHandle (LT.WindowHandle → a)
116− | GetWindowHandles (Array LT.WindowHandle → a)
117− | CloseWindow a
118− | SwitchToWindow LT.WindowHandle a
119− | SwitchToFrame LT.FrameId a
120− | SwitchToParentFrame a
121− | GetWindowRectangle (LT.Rectangle → a)
122− | SetWindowRectangle LT.Rectangle a
123− | MaximizeWindow a
124− | MinimizeWindow a
125− | FullscreenWindow a
126− | ExecuteScript LT.Script (J.Json → a)
127− | ExecuteScriptAsync LT.Script (J.Json → a)
128− | GetAllCookies (Array LT.Cookie → a)
129− | GetCookie String (LT.Cookie → a)
130− | DeleteCookie String a
131− | DeleteAllCookies a
132− | AddCookie LT.Cookie a
133− | DismissAlert a
134− | AcceptAlert a
135− | GetAlertText (String → a)
136− | SendAlertText String a
137− | Screenshot String a
138− | GetActiveElement (LT.Element → a)
139− | FindElement LT.Locator (LT.Element → a)
140− | FindElements LT.Locator (Array LT.Element → a)
141− | OnElement LT.Element (ElementF a)
142− | PerformActions LT.ActionRequest a
143− | ReleaseActions a
144−
145−data ElementF a
146− = ChildElement LT.Locator (LT.Element → a)
147− | ChildElements LT.Locator (Array LT.Element → a)
148− | IsSelected (Boolean → a)
149− | GetAttribute String (String → a)
150− | GetProperty String (J.Json → a)
151− | GetCss String (String → a)
152− | GetText (String → a)
153− | GetTagName (String → a)
154− | GetRectangle (LT.Rectangle → a)
155− | IsEnabled (Boolean → a)
156− | ClickEl a
157− | ClearEl a
158− | SendKeysEl String a
159− | ScreenshotEl String a
160− | IsDisplayed (Boolean → a)
161− | Submit a
162−
163−data ActionF a
164− = Click LT.Button a
165− | ButtonDown LT.Button a
166− | ButtonUp LT.Button a
167− | DoubleClick LT.Button a
168− | SendKeys String a
169− | MoveTo LT.PointerMove a
170− | Touch (TouchF a)
171− | Pause Milliseconds a
172−
173−data TouchF a
174− = Tap a
175− | TouchDown a
176− | TouchUp a
177− | LongClick a
178− | Flick LT.PointerMove a
179− | Scroll LT.PointerMove a
180− | DoubleTap a
181−
182−derive instance functorLunaparkF ∷ Functor LunaparkF
183−derive instance functorElementF ∷ Functor ElementF
184−derive instance functorActionF ∷ Functor ActionF
185−derive instance functorTouchF ∷ Functor TouchF
186103
187104type Lunapark e r = BaseRun e (lunapark ∷ LUNAPARK, lunaparkActions ∷ LUNAPARK_ACTIONS|r)
188105
@@ -192,246 +109,27 @@ type BaseRun e r = R.Run
192109 , eff ∷ R.EFF (LunaparkEffects e)
193110 | r)
194111
195−_lunapark = SProxy ∷ SProxy "lunapark"
196−type LUNAPARK = R.FProxy LunaparkF
197−type WithLunapark r a = R.Run (lunapark ∷ LUNAPARK|r) a
198−
199−liftLunapark ∷ ∀ a r. LunaparkF a → WithLunapark r a
200−liftLunapark = R.lift _lunapark
201−
202−quit ∷ ∀ r. WithLunapark r Unit
203−quit = liftLunapark $ Quit unit
204−
205−status ∷ ∀ r. WithLunapark r LT.ServerStatus
206−status = liftLunapark $ Status id
207−
208−setTimeouts ∷ ∀ r. LT.Timeouts → WithLunapark r Unit
209−setTimeouts ts = liftLunapark $ SetTimeouts ts unit
210−
211−getTimeouts ∷ ∀ r. WithLunapark r LT.Timeouts
212−getTimeouts = liftLunapark $ GetTimeouts id
213−
214−go ∷ ∀ r. String → WithLunapark r Unit
215−go uri = liftLunapark $ GoTo uri unit
216−
217−getUrl ∷ ∀ r. WithLunapark r String
218−getUrl = liftLunapark $ GetUrl id
219−
220−forward ∷ ∀ r. WithLunapark r Unit
221−forward = liftLunapark $ Forward unit
222−
223−back ∷ ∀ r. WithLunapark r Unit
224−back = liftLunapark $ Back unit
225−
226−refresh ∷ ∀ r. WithLunapark r Unit
227−refresh = liftLunapark $ Refresh unit
228−
229−getTitle ∷ ∀ r. WithLunapark r String
230−getTitle = liftLunapark $ GetTitle id
231−
232−getWindowHandle ∷ ∀ r. WithLunapark r LT.WindowHandle
233−getWindowHandle = liftLunapark $ GetWindowHandle id
234−
235−getWindowHandles ∷ ∀ r. WithLunapark r (Array LT.WindowHandle)
236−getWindowHandles = liftLunapark $ GetWindowHandles id
237−
238−closeWindow ∷ ∀ r. WithLunapark r Unit
239−closeWindow = liftLunapark $ CloseWindow unit
240−
241−switchToWindow ∷ ∀ r. LT.WindowHandle → WithLunapark r Unit
242−switchToWindow w = liftLunapark $ SwitchToWindow w unit
243−
244−switchToFrame ∷ ∀ r. LT.FrameId → WithLunapark r Unit
245−switchToFrame f = liftLunapark $ SwitchToFrame f unit
246−
247−switchToParentFrame ∷ ∀ r. WithLunapark r Unit
248−switchToParentFrame = liftLunapark $ SwitchToParentFrame unit
249−
250−getWindowRectangle ∷ ∀ r. WithLunapark r LT.Rectangle
251−getWindowRectangle = liftLunapark $ GetWindowRectangle id
252−
253−setWindowRectangle ∷ ∀ r. LT.Rectangle → WithLunapark r Unit
254−setWindowRectangle r = liftLunapark $ SetWindowRectangle r unit
255−
256−maximizeWindow ∷ ∀ r. WithLunapark r Unit
257−maximizeWindow = liftLunapark $ MaximizeWindow unit
258−
259−minimizeWindow ∷ ∀ r. WithLunapark r Unit
260−minimizeWindow = liftLunapark $ MinimizeWindow unit
261−
262−fullscreenWindow ∷ ∀ r. WithLunapark r Unit
263−fullscreenWindow = liftLunapark $ FullscreenWindow unit
264−
265−executeScript ∷ ∀ r. LT.Script → WithLunapark r J.Json
266−executeScript script = liftLunapark $ ExecuteScript script id
267−
268−executeScriptAsync ∷ ∀ r. LT.Script → WithLunapark r J.Json
269−executeScriptAsync script = liftLunapark $ ExecuteScriptAsync script id
270−
271−getAllCookies ∷ ∀ r. WithLunapark r (Array LT.Cookie)
272−getAllCookies = liftLunapark $ GetAllCookies id
273−
274−getCookie ∷ ∀ r. String → WithLunapark r LT.Cookie
275−getCookie name = liftLunapark $ GetCookie name id
276−
277−addCookie ∷ ∀ r. LT.Cookie → WithLunapark r Unit
278−addCookie cookie = liftLunapark $ AddCookie cookie unit
279−
280−deleteCookie ∷ ∀ r. String → WithLunapark r Unit
281−deleteCookie name = liftLunapark $ DeleteCookie name unit
282−
283−deleteAllCookies ∷ ∀ r. WithLunapark r Unit
284−deleteAllCookies = liftLunapark $ DeleteAllCookies unit
285−
286−dismissAlert ∷ ∀ r. WithLunapark r Unit
287−dismissAlert = liftLunapark $ DismissAlert unit
288−
289−acceptAlert ∷ ∀ r. WithLunapark r Unit
290−acceptAlert = liftLunapark $ AcceptAlert unit
291−
292−getAlertText ∷ ∀ r. WithLunapark r String
293−getAlertText = liftLunapark $ GetAlertText id
294−
295−sendAlertText ∷ ∀ r. String → WithLunapark r Unit
296−sendAlertText txt = liftLunapark $ SendAlertText txt unit
297−
298−screenshot ∷ ∀ r. String → WithLunapark r Unit
299−screenshot fp = liftLunapark $ Screenshot fp unit
300−
301−elementScreenshot ∷ ∀ r. LT.Element → String → WithLunapark r Unit
302−elementScreenshot el fp = liftLunapark $ OnElement el $ ScreenshotEl fp unit
303−
304−findElement ∷ ∀ r. LT.Locator → WithLunapark r LT.Element
305−findElement l = liftLunapark $ FindElement l id
306−
307−findElements ∷ ∀ r. LT.Locator → WithLunapark r (Array LT.Element)
308−findElements l = liftLunapark $ FindElements l id
309−
310−childElement ∷ ∀ r. LT.Element → LT.Locator → WithLunapark r LT.Element
311−childElement el l = liftLunapark $ OnElement el $ ChildElement l id
312−
313−childElements ∷ ∀ r. LT.Element → LT.Locator → WithLunapark r (Array LT.Element)
314−childElements el l = liftLunapark $ OnElement el $ ChildElements l id
315−
316−isSelected ∷ ∀ r. LT.Element → WithLunapark r Boolean
317−isSelected el = liftLunapark $ OnElement el $ IsSelected id
318−
319−getAttribute ∷ ∀ r. LT.Element → String → WithLunapark r String
320−getAttribute el name = liftLunapark $ OnElement el $ GetAttribute name id
321−
322−getProperty ∷ ∀ r. LT.Element → String → WithLunapark r J.Json
323−getProperty el name = liftLunapark $ OnElement el $ GetProperty name id
324−
325−getCss ∷ ∀ r. LT.Element → String → WithLunapark r String
326−getCss el name = liftLunapark $ OnElement el $ GetCss name id
327−
328−getText ∷ ∀ r. LT.Element → WithLunapark r String
329−getText el = liftLunapark $ OnElement el $ GetText id
330−
331−getTagName ∷ ∀ r. LT.Element → WithLunapark r String
332−getTagName el = liftLunapark $ OnElement el $ GetTagName id
333−
334−getRectangle ∷ ∀ r. LT.Element → WithLunapark r LT.Rectangle
335−getRectangle el = liftLunapark $ OnElement el $ GetRectangle id
336−
337−isEnabled ∷ ∀ r. LT.Element → WithLunapark r Boolean
338−isEnabled el = liftLunapark $ OnElement el $ IsEnabled id
339−
340−clickElement ∷ ∀ r. LT.Element → WithLunapark r Unit
341−clickElement el = liftLunapark $ OnElement el $ ClickEl unit
342−
343−clearElement ∷ ∀ r. LT.Element → WithLunapark r Unit
344−clearElement el = liftLunapark $ OnElement el $ ClearEl unit
345−
346−sendKeysElement ∷ ∀ r. LT.Element → String → WithLunapark r Unit
347−sendKeysElement el txt = liftLunapark $ OnElement el $ SendKeysEl txt unit
348−
349−isDisplayed ∷ ∀ r. LT.Element → WithLunapark r Boolean
350−isDisplayed el = liftLunapark $ OnElement el $ IsDisplayed id
351−
352−submitElement ∷ ∀ r. LT.Element → WithLunapark r Unit
353−submitElement el = liftLunapark $ OnElement el $ Submit unit
354−
355−performActions ∷ ∀ r. LT.ActionRequest → WithLunapark r Unit
356−performActions req = liftLunapark $ PerformActions req unit
357−
358−releaseActions ∷ ∀ r. WithLunapark r Unit
359−releaseActions = liftLunapark $ ReleaseActions unit
360−
361−-- ACTIONS
362−_lunaparkActions = SProxy ∷ SProxy "lunaparkActions"
363−type LUNAPARK_ACTIONS = R.FProxy ActionF
364−type WithAction r = R.Run (lunaparkActions ∷ LUNAPARK_ACTIONS|r) Unit
365−
366−liftAction ∷ ∀ r. ActionF Unit → WithAction r
367−liftAction = R.lift _lunaparkActions
368−
369−click ∷ ∀ r. LT.Button → WithAction r
370−click btn = liftAction $ Click btn unit
371−
372−buttonDown ∷ ∀ r. LT.Button → WithAction r
373−buttonDown btn = liftAction $ ButtonDown btn unit
374−
375−buttonUp ∷ ∀ r. LT.Button → WithAction r
376−buttonUp btn = liftAction $ ButtonUp btn unit
377−
378−doubleClick ∷ ∀ r. LT.Button → WithAction r
379−doubleClick btn = liftAction $ DoubleClick btn unit
380−
381−sendKeys ∷ ∀ r. String → WithAction r
382−sendKeys txt = liftAction $ SendKeys txt unit
383−
384−moveTo ∷ ∀ r. LT.PointerMove → WithAction r
385−moveTo move = liftAction $ MoveTo move unit
386−
387−pause ∷ ∀ r. Milliseconds → WithAction r
388−pause ms = liftAction $ Pause ms unit
389−
390−tap ∷ ∀ r. WithAction r
391−tap = liftAction $ Touch $ Tap unit
392−
393−touchDown ∷ ∀ r. WithAction r
394−touchDown = liftAction $ Touch $ TouchDown unit
395−
396−touchUp ∷ ∀ r. WithAction r
397−touchUp = liftAction $ Touch $ TouchUp unit
398−
399−longTap ∷ ∀ r. WithAction r
400−longTap = liftAction $ Touch $ LongClick unit
401−
402−flick ∷ ∀ r. LT.PointerMove → WithAction r
403−flick move = liftAction $ Touch $ Flick move unit
404−
405−scroll ∷ ∀ r. LT.PointerMove → WithAction r
406−scroll move = liftAction $ Touch $ Scroll move unit
407−
408−doubleTap ∷ ∀ r. WithAction r
409−doubleTap = liftAction $ Touch $ DoubleTap unit
410−
411−-- Interpreters
412112runLunapark ∷ ∀ e r. HandleLunaparkInput → BaseRun e (lunapark ∷ LUNAPARK|r) ~> BaseRun e r
413113runLunapark input = do
414114 R.interpretRec (R.on _lunapark (handleLunapark input) R.send)
415115
416116runLunaparkActions ∷ ∀ e r. HandleLunaparkInput → Lunapark e r ~> BaseRun e (lunapark ∷ LUNAPARK|r)
417−runLunaparkActions input =
418− if input.actionsEnabled
419− then interpretW3CActions input Nil
420− else R.interpretRec (R.on _lunaparkActions (jsonWireActions input) R.send)
117+runLunaparkActions input
118+ | input.actionsEnabled = interpretW3CActions Nil
119+ | otherwise = R.interpretRec (R.on _lunaparkActions (jsonWireActions input) R.send)
421120
422121interpretW3CActions
423122 ∷ ∀ e r
424− . HandleLunaparkInput
425− → List LT.ActionSequence
123+ . List LT.ActionSequence
426124 → Lunapark e r
427125 ~> BaseRun e (lunapark ∷ LUNAPARK|r)
428−interpretW3CActions inp acc as = case R.peel as of
126+interpretW3CActions acc as = case R.peel as of
429127 Left la → case tag la of
430− Left a → w3cActions acc (interpretW3CActions inp) a
128+ Left a → w3cActions acc interpretW3CActions a
431129 Right others → do
432130 T.for_ (L.reverse acc) \s → performActions $ SM.singleton "dummy" s
433131 cont ← R.send others
434− interpretW3CActions inp Nil cont
132+ interpretW3CActions Nil cont
435133 Right a → pure a
436134 where
437135 tag = R.on _lunaparkActions Left Right
@@ -467,7 +165,7 @@ w3cActions acc loop = case _ of
467165 Pause ms next →
468166 let seq = [ LT.pause ms ]
469167 in loop (anywhere seq) next
470− Touch tch → case tch of
168+ InTouch tch → case tch of
471169 Tap next →
472170 let seq = [ LT.pointerDown LT.LeftBtn, LT.pointerUp LT.LeftBtn ]
473171 in loop (inTouch seq) next
@@ -561,7 +259,7 @@ jsonWireActions inp = case _ of
561259 Pause ms next → do
562260 R.liftAff $ Aff.delay ms
563261 pure next
564− Touch tch → case tch of
262+ InTouch tch → case tch of
565263 Tap next → do
566264 _ ← post_ (LP.Touch : LP.Click : Nil)
567265 pure next
@@ -861,15 +559,3 @@ handleLunapark inp = case _ of
861559
862560 inSession ∷ LP.EndpointPart
863561 inSession = LP.InSession inp.session
864−
865−liftAndRethrow ∷ ∀ e r ω. Aff (LunaparkEffects e) (Either LE.Error ω) → BaseRun e r ω
866−liftAndRethrow a = do
867− res ← R.liftAff a
868− RE.rethrow res
869−
870−throwLeft ∷ ∀ e r. Either String ~> BaseRun e r
871−throwLeft = RE.rethrow <<< lmap LE.unknownError
872−
873−-- Safe, since we actually want handler and result have same rows not, remove except
874−catch ∷ ∀ e r a. R.Run (except ∷ EXCEPT e|r) a → (e → R.Run (except ∷ EXCEPT e|r) a) → R.Run (except ∷ EXCEPT e|r) a
875−catch = unsafeCoerce $ flip RE.catch