@@ -1,21 +1,25 @@
11module Test.Main where
22
3−import Control.Monad.Error.Class (try)
43import Data.Either (isLeft)
54import Data.Maybe (Maybe(..))
65import Effect (Effect)
6+import Effect.Aff (launchAff_, try)
77import Effect.Class (liftEffect)
88import Effect.Ref as Ref
9+import Node.Encoding as Encoding
910import Node.FS.Aff as FS
11+import Node.Stream as Stream
1012import Playwright
1113import Playwright.ConsoleMessage as ConsoleMessage
12−import Playwright.Event as Event
14+import Playwright.Dialog as Dialog
15+import Playwright.Download as Download
1316import Playwright.Event (on)
14−import Prelude (Unit, bind, discard, void, ($), (/=), (<>), (=<<))
17+import Playwright.Event as Event
18+import Prelude
1519import Test.Unit (suite, test)
1620import Test.Unit.Assert as Assert
1721import Test.Unit.Main (runTest)
18−import TestUtils (cwd, testClickEvent, withBrowser, withBrowserPage)
22+import TestUtils (cwd, isNull, testClickEvent, withBrowser, withBrowserPage)
1923import Untagged.Union (fromOneOf)
2024
2125static :: String -> URL
@@ -110,3 +114,47 @@ main = runTest do
110114 """
111115 realType <- liftEffect $ Ref.read typeRef
112116 Assert.equal (Just ConsoleMessage.Debug) realType
117+ test "download" do
118+ withBrowserPage hello
119+ \page -> do
120+ downloadRef <- liftEffect $ Ref.new Nothing
121+ liftEffect $ on Event.download page $ \download -> launchAff_ do
122+ fname <- liftEffect $ Download.suggestedFilename download
123+ Assert.equal "hello.txt" fname
124+ mbStream <- Download.createReadStream download
125+ failureStatus <- Download.failure download
126+ Assert.equal true (isNull failureStatus)
127+ case mbStream of
128+ Nothing -> Assert.assert "Unable to get stream" false
129+ Just stream -> do
130+ liftEffect $ Stream.onDataString stream Encoding.UTF8 $ \string -> do
131+ Ref.write (Just string) downloadRef
132+ void $ evaluate page
133+ """
134+ function download(filename, text) {
135+ var element = document.createElement('a');
136+ element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
137+ element.setAttribute('download', filename);
138+ document.body.appendChild(element);
139+ element.click();
140+ document.body.removeChild(element);
141+ }
142+ download("hello.txt","hiiii");
143+ """
144+ waitForTimeout page 100
145+ downloadContent <- liftEffect $ Ref.read downloadRef
146+ Assert.equal (Just "hiiii") downloadContent
147+ test "dialog" do
148+ withBrowserPage hello
149+ \page -> do
150+ downloadRef <- liftEffect $ Ref.new Nothing
151+ liftEffect $ on Event.dialog page $ \dialog -> launchAff_ do
152+ defaultValue <- liftEffect $ Dialog.defaultValue dialog
153+ Assert.equal "hello" defaultValue
154+ message <- liftEffect $ Dialog.message dialog
155+ Assert.equal "hi" message
156+ Dialog.dismiss dialog
157+ void $ evaluate page
158+ """
159+ prompt("hi", "hello");
160+ """