git.iko.soy/rowtype-yoga/purescript-playwright
on main help sign in
Better tests.
caf48ffVladimir Kalnitsky
README.md+2−1
@@ -1,2 +1,3 @@
1# purescript-playwright
1# purescript-playwright
2+
2−PureScript bindings for Playwright
3+PureScript bindings for Playwright.
spago.dhall+1
@@ -8,6 +8,7 @@
8 , "test-unit"
8 , "test-unit"
9 , "untagged-union"
9 , "untagged-union"
10 , "node-buffer"
10 , "node-buffer"
11+ , "node-fs-aff"
11 ]
12 ]
12, packages = ./packages.dhall
13, packages = ./packages.dhall
13, sources = [ "src/**/*.purs", "test/**/*.purs" ]
14, sources = [ "src/**/*.purs", "test/**/*.purs" ]
src/Playwright.purs+37−23
@@ -1,38 +1,48 @@
1module Playwright
1module Playwright
2 ( launch
2 ( launch
3+ , close
3 , contexts
4 , contexts
4 , isConnected
5 , isConnected
5 , version
6 , version
6− , close
7 , newPage
7 , newPage
8+ , goForward
9+ , goBack
8 , goto
10 , goto
11+ , addCookies
12+ , hover
13+ , innerHTML
14+ , innerText
15+ , isClosed
16+ , keyboard
17+ , mainFrame
18+ , name
9 , query
19 , query
10 , queryMany
20 , queryMany
11 , screenshot
21 , screenshot
12 , textContent
22 , textContent
23+ , url
13 , module Playwright.Data
24 , module Playwright.Data
14 , module Playwright.Options
25 , module Playwright.Options
15 )
26 )
16where
27where
17
28
18−import Prelude
19import Effect (Effect)
29import Effect (Effect)
20−-- import Control.Promise (toAffE)
21import Effect.Aff (Aff)
30import Effect.Aff (Aff)
22−import Untagged.Coercible (class Coercible, coerce)
31+import Literals.Null (Null)
23−import Untagged.Union (type (|+|), UndefinedOr)
24import Node.Buffer (Buffer)
32import Node.Buffer (Buffer)
25import Playwright.Data
33import Playwright.Data
26−import Playwright.Options
27import Playwright.Internal (effCall, effProp, affCall)
34import Playwright.Internal (effCall, effProp, affCall)
28−import Literals.Null (Null)
35+import Playwright.Options
36+import Prelude
37+import Untagged.Coercible (class Coercible)
38+import Untagged.Union (type (|+|), UndefinedOr)
29
39
30launch
40launch
31 :: forall o
41 :: forall o
32 . Coercible o LaunchOptions
42 . Coercible o LaunchOptions
33 => BrowserType -> o -> Aff Browser
43 => BrowserType -> o -> Aff Browser
34launch bt opts =
44launch bt opts =
35− affCall "launch" typeInfo bt (coerce opts :: LaunchOptions)
45+ affCall "launch" typeInfo bt opts
36 where
46 where
37 typeInfo _ = launch
47 typeInfo _ = launch
38
48
@@ -62,14 +72,14 @@ newPage
62 => x -> o -> Aff Page
72 => x -> o -> Aff Page
63newPage sth opts =
73newPage sth opts =
64 affCall "newPage" typeInfo sth opts
74 affCall "newPage" typeInfo sth opts
65− where typeInfo _ = newPage :: x -> o -> Aff Page
75+ where typeInfo _ = newPage
66
76
67goForward
77goForward
68 :: forall o
78 :: forall o
69 . Coercible o GoOptions
79 . Coercible o GoOptions
70 => Page -> o -> Aff (Null |+| Response)
80 => Page -> o -> Aff (Null |+| Response)
71goForward page opts =
81goForward page opts =
72− affCall "goForward" typeInfo page (coerce opts :: GoOptions)
82+ affCall "goForward" typeInfo page opts
73 where typeInfo _ = goForward
83 where typeInfo _ = goForward
74
84
75goBack
85goBack
@@ -77,7 +87,7 @@ goBack
77 . Coercible o GoOptions
87 . Coercible o GoOptions
78 => Page -> o -> Aff (Null |+| Response)
88 => Page -> o -> Aff (Null |+| Response)
79goBack page opts =
89goBack page opts =
80− affCall "goBack" typeInfo page (coerce opts :: GoOptions)
90+ affCall "goBack" typeInfo page opts
81 where typeInfo _ = goBack
91 where typeInfo _ = goBack
82
92
83goto
93goto
@@ -86,7 +96,7 @@ goto
86 => Coercible o GotoOptions
96 => Coercible o GotoOptions
87 => x -> URL -> o -> Aff (Null |+| Response)
97 => x -> URL -> o -> Aff (Null |+| Response)
88goto x url' opts =
98goto x url' opts =
89− affCall "goto" typeInfo x url' (coerce opts :: GotoOptions)
99+ affCall "goto" typeInfo x url' opts
90 where typeInfo _ = goto
100 where typeInfo _ = goto
91
101
92type Cookie =
102type Cookie =
@@ -101,8 +111,7 @@ addCookies
101 -> Aff Unit
111 -> Aff Unit
102addCookies bc cookies =
112addCookies bc cookies =
103 affCall "addCookies" typeInfo bc cookies
113 affCall "addCookies" typeInfo bc cookies
104− where
105− typeInfo _ = addCookies
114+ where typeInfo _ = addCookies
106
115
107hover
116hover
108 :: forall x o
117 :: forall x o
@@ -118,8 +127,8 @@ innerHTML
118 . Coercible x (Page |+| Frame |+| ElementHandle)
127 . Coercible x (Page |+| Frame |+| ElementHandle)
119 => Coercible o InnerHTMLOptions
128 => Coercible o InnerHTMLOptions
120 => x -> Selector -> o -> Aff String
129 => x -> Selector -> o -> Aff String
121−innerHTML x selector o =
130+innerHTML x selector opts =
122− affCall "innerHTML" typeInfo x selector (coerce o :: InnerHTMLOptions)
131+ affCall "innerHTML" typeInfo x selector opts
123 where typeInfo _ = innerHTML
132 where typeInfo _ = innerHTML
124
133
125innerText
134innerText
@@ -130,10 +139,9 @@ innerText
130 -> Selector
139 -> Selector
131 -> o
140 -> o
132 -> Aff String
141 -> Aff String
133−innerText x sel o =
142+innerText x sel opts =
134− affCall "innerText" typeInfo x sel o
143+ affCall "innerText" typeInfo x sel opts
135− where typeInfo _ = innerText
144+ where typeInfo _ = innerText :: x -> Selector -> o -> Aff String
136− :: x -> Selector -> o -> Aff String
137
145
138isClosed :: Page -> Effect Boolean
146isClosed :: Page -> Effect Boolean
139isClosed = effCall "isClosed" \_ -> isClosed
147isClosed = effCall "isClosed" \_ -> isClosed
@@ -141,6 +149,12 @@ isClosed = effCall "isClosed" \_ -> isClosed
141keyboard :: Page -> Effect Keyboard
149keyboard :: Page -> Effect Keyboard
142keyboard = effProp "keyboard"
150keyboard = effProp "keyboard"
143
151
152+mainFrame :: Page -> Effect Frame
153+mainFrame = effCall "mainFrame" \_ -> mainFrame
154+
155+name :: Frame -> Effect String
156+name = effCall "name" \_ -> name
157+
144-- | `sth.$(selector)`
158-- | `sth.$(selector)`
145query
159query
146 :: forall x
160 :: forall x
@@ -180,6 +194,6 @@ url
180 :: forall x
194 :: forall x
181 . Coercible x (Page |+| Frame |+| Download |+| Request |+| Response |+| Worker)
195 . Coercible x (Page |+| Frame |+| Download |+| Request |+| Response |+| Worker)
182 => x
196 => x
183− -> Effect String
197+ -> Effect URL
184−url = effCall "url" typeInfo
198+url x = effCall "url" typeInfo x
185− where typeInfo _ = url :: x -> Effect String
199+ where typeInfo _ = url
test/Main.purs+51−10
@@ -5,9 +5,13 @@ import Playwright
5import Effect (Effect)
5import Effect (Effect)
6import Test.Unit (suite, test)
6import Test.Unit (suite, test)
7import Test.Unit.Main (runTest)
7import Test.Unit.Main (runTest)
8−import Untagged.Union (asOneOf, fromOneOf)
8+import Untagged.Union (fromOneOf)
9import Test.Unit.Assert as Assert
9import Test.Unit.Assert as Assert
10import Data.Maybe (Maybe(..))
10import Data.Maybe (Maybe(..))
11+import Node.FS.Aff as FS
12+import Effect.Aff (Aff)
13+import Effect.Class (liftEffect)
14+import Control.Monad.Error.Class (withResource)
11
15
12static :: String -> URL
16static :: String -> URL
13static file =
17static file =
@@ -15,19 +19,56 @@ static file =
15
19
16main :: Effect Unit
20main :: Effect Unit
17main = runTest do
21main = runTest do
22+ let hello = static "hello.html"
18 suite "browser" do
23 suite "browser" do
19− test "launch" do
24+ test "launch, close" do
20 browser <- launch chromium {}
25 browser <- launch chromium {}
21 close browser
26 close browser
27+ test "close/isClosed" do
28+ withBrowserPage hello
29+ \page -> do
30+ Assert.assertFalse "new page is closed" =<<
31+ liftEffect (isClosed page)
32+ close page
33+ Assert.assert "page is not closed" =<< liftEffect (isClosed page)
34+ test "version" do
35+ withBrowser
36+ \browser -> do
37+ v <- liftEffect $ version browser
38+ Assert.assert "version is empty" (v /= "")
39+ test "url" do
40+ withBrowserPage hello
41+ \page -> do
42+ Assert.equal hello =<< liftEffect (url page)
43+ test "screenshot" do
44+ withBrowserPage hello
45+ \page -> do
46+ void $ screenshot page { path: "screenshot.png" }
47+ FS.unlink "screenshot.png"
22 test "textContent" do
48 test "textContent" do
49+ withBrowserPage hello
50+ \page -> do
51+ text <- textContent page (Selector "body")
52+ Assert.equal (Just "hello\n") (fromOneOf text)
53+ test "mainFrame" do
54+ withBrowserPage hello
55+ \page -> do
56+ frame <- liftEffect $ mainFrame page
57+ frameName <- liftEffect $ name frame
58+ Assert.equal "" frameName
59+
60+withBrowser :: forall a. (Browser -> Aff a) -> Aff a
61+withBrowser action = withResource acquire release action
62+ where
23− browser <- launch chromium {}
63+ acquire = launch chromium {}
64+ release = close
65+
66+withBrowserPage :: forall a. URL -> (Page -> Aff a) -> Aff a
67+withBrowserPage url action = do
68+ withBrowser
69+ \browser -> do
24 page <- newPage browser {}
70 page <- newPage browser {}
25− void $ goto
71+ void $ goto page url {}
26− page
72+ action page
27− (static "hello.html")
28− {}
29− text <- textContent page (Selector "body")
30− Assert.equal (Just "hello\n") (fromOneOf text)
31− close browser
32
73
33foreign import cwd :: String
74foreign import cwd :: String