git.iko.soy/rowtype-yoga/purescript-playwright
on main help sign in
Better tests.
commitcaf48ffcc2e9d9f00e996e2d25769c4485d0cfe8 authorVladimir Kalnitsky <klntsky@gmail.com> dateSat 26 Sep 2020, 16:56 (6 years ago) parentec738bf
4 files changed+91−34
README.md+2−1 spago.dhall+1 src/Playwright.purs+37−23 test/Main.purs+51−10
README.md+2−1
@@ -1,2 +1,3 @@
11# purescript-playwright
2−PureScript bindings for Playwright
2+
3+PureScript bindings for Playwright.
spago.dhall+1
@@ -8,6 +8,7 @@
88 , "test-unit"
99 , "untagged-union"
1010 , "node-buffer"
11+ , "node-fs-aff"
1112 ]
1213, packages = ./packages.dhall
1314, sources = [ "src/**/*.purs", "test/**/*.purs" ]
src/Playwright.purs+37−23
@@ -1,38 +1,48 @@
11module Playwright
22 ( launch
3+ , close
34 , contexts
45 , isConnected
56 , version
6− , close
77 , newPage
8+ , goForward
9+ , goBack
810 , goto
11+ , addCookies
12+ , hover
13+ , innerHTML
14+ , innerText
15+ , isClosed
16+ , keyboard
17+ , mainFrame
18+ , name
919 , query
1020 , queryMany
1121 , screenshot
1222 , textContent
23+ , url
1324 , module Playwright.Data
1425 , module Playwright.Options
1526 )
1627where
1728
18−import Prelude
1929import Effect (Effect)
20−-- import Control.Promise (toAffE)
2130import Effect.Aff (Aff)
22−import Untagged.Coercible (class Coercible, coerce)
23−import Untagged.Union (type (|+|), UndefinedOr)
31+import Literals.Null (Null)
2432import Node.Buffer (Buffer)
2533import Playwright.Data
26−import Playwright.Options
2734import 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)
2939
3040launch
3141 :: forall o
3242 . Coercible o LaunchOptions
3343 => BrowserType -> o -> Aff Browser
3444launch bt opts =
35− affCall "launch" typeInfo bt (coerce opts :: LaunchOptions)
45+ affCall "launch" typeInfo bt opts
3646 where
3747 typeInfo _ = launch
3848
@@ -62,14 +72,14 @@ newPage
6272 => x -> o -> Aff Page
6373newPage sth opts =
6474 affCall "newPage" typeInfo sth opts
65− where typeInfo _ = newPage :: x -> o -> Aff Page
75+ where typeInfo _ = newPage
6676
6777goForward
6878 :: forall o
6979 . Coercible o GoOptions
7080 => Page -> o -> Aff (Null |+| Response)
7181goForward page opts =
72− affCall "goForward" typeInfo page (coerce opts :: GoOptions)
82+ affCall "goForward" typeInfo page opts
7383 where typeInfo _ = goForward
7484
7585goBack
@@ -77,7 +87,7 @@ goBack
7787 . Coercible o GoOptions
7888 => Page -> o -> Aff (Null |+| Response)
7989goBack page opts =
80− affCall "goBack" typeInfo page (coerce opts :: GoOptions)
90+ affCall "goBack" typeInfo page opts
8191 where typeInfo _ = goBack
8292
8393goto
@@ -86,7 +96,7 @@ goto
8696 => Coercible o GotoOptions
8797 => x -> URL -> o -> Aff (Null |+| Response)
8898goto x url' opts =
89− affCall "goto" typeInfo x url' (coerce opts :: GotoOptions)
99+ affCall "goto" typeInfo x url' opts
90100 where typeInfo _ = goto
91101
92102type Cookie =
@@ -101,8 +111,7 @@ addCookies
101111 -> Aff Unit
102112addCookies bc cookies =
103113 affCall "addCookies" typeInfo bc cookies
104− where
105− typeInfo _ = addCookies
114+ where typeInfo _ = addCookies
106115
107116hover
108117 :: forall x o
@@ -118,8 +127,8 @@ innerHTML
118127 . Coercible x (Page |+| Frame |+| ElementHandle)
119128 => Coercible o InnerHTMLOptions
120129 => x -> Selector -> o -> Aff String
121−innerHTML x selector o =
122− affCall "innerHTML" typeInfo x selector (coerce o :: InnerHTMLOptions)
130+innerHTML x selector opts =
131+ affCall "innerHTML" typeInfo x selector opts
123132 where typeInfo _ = innerHTML
124133
125134innerText
@@ -130,10 +139,9 @@ innerText
130139 -> Selector
131140 -> o
132141 -> Aff String
133−innerText x sel o =
134− affCall "innerText" typeInfo x sel o
135− where typeInfo _ = innerText
136− :: x -> Selector -> o -> Aff String
142+innerText x sel opts =
143+ affCall "innerText" typeInfo x sel opts
144+ where typeInfo _ = innerText :: x -> Selector -> o -> Aff String
137145
138146isClosed :: Page -> Effect Boolean
139147isClosed = effCall "isClosed" \_ -> isClosed
@@ -141,6 +149,12 @@ isClosed = effCall "isClosed" \_ -> isClosed
141149keyboard :: Page -> Effect Keyboard
142150keyboard = effProp "keyboard"
143151
152+mainFrame :: Page -> Effect Frame
153+mainFrame = effCall "mainFrame" \_ -> mainFrame
154+
155+name :: Frame -> Effect String
156+name = effCall "name" \_ -> name
157+
144158-- | `sth.$(selector)`
145159query
146160 :: forall x
@@ -180,6 +194,6 @@ url
180194 :: forall x
181195 . Coercible x (Page |+| Frame |+| Download |+| Request |+| Response |+| Worker)
182196 => x
183− -> Effect String
184−url = effCall "url" typeInfo
185− where typeInfo _ = url :: x -> Effect String
197+ -> Effect URL
198+url x = effCall "url" typeInfo x
199+ where typeInfo _ = url
test/Main.purs+51−10
@@ -5,9 +5,13 @@ import Playwright
55import Effect (Effect)
66import Test.Unit (suite, test)
77import Test.Unit.Main (runTest)
8−import Untagged.Union (asOneOf, fromOneOf)
8+import Untagged.Union (fromOneOf)
99import Test.Unit.Assert as Assert
1010import 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)
1115
1216static :: String -> URL
1317static file =
@@ -15,19 +19,56 @@ static file =
1519
1620main :: Effect Unit
1721main = runTest do
22+ let hello = static "hello.html"
1823 suite "browser" do
19− test "launch" do
24+ test "launch, close" do
2025 browser <- launch chromium {}
2126 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"
2248 test "textContent" do
23− browser <- launch chromium {}
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
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
2470 page <- newPage browser {}
25− void $ goto
26− page
27− (static "hello.html")
28− {}
29− text <- textContent page (Selector "body")
30− Assert.equal (Just "hello\n") (fromOneOf text)
31− close browser
71+ void $ goto page url {}
72+ action page
3273
3374foreign import cwd :: String