fix: screenshot marshaling

This commit is contained in:
orion 2023-10-05 09:24:21 -05:00
parent 3f2a4447a8
commit a1ead38c39
Signed by: orion
GPG Key ID: 6D4165AE4C928719
4 changed files with 26 additions and 11 deletions

View File

@ -23,6 +23,7 @@ package:
- node-path - node-path
- node-process - node-process
- node-streams - node-streams
- node-url
- nullable - nullable
- ordered-collections - ordered-collections
- parallel - parallel

View File

@ -83,14 +83,7 @@ offsetHeight = Eval.unsafeRunJs0 "e => e.offsetHeight"
attrs :: forall a. IsElement a => Handle a -> Aff (Map String String) attrs :: forall a. IsElement a => Handle a -> Aff (Map String String)
attrs = attrs =
let let
js = String.joinWith "\n" js = "e => Array.from(e.attributes).map(a => ({k: a.name, v: a.value}))"
[ "e => Array.from(e.attributes)"
, " .reduce("
, " (m, a) => [...m, {k: a.name, v: a.value}],"
, " [],"
, " )"
, " .filter(({k}) => k)"
]
in in
map FFI.makeMap <<< Eval.unsafeRunJs0 @(Array { k :: String, v :: String }) js map FFI.makeMap <<< Eval.unsafeRunJs0 @(Array { k :: String, v :: String }) js

View File

@ -59,5 +59,5 @@ prepareScreenshotOptions
, omitBackground: FFI.maybeToUndefined omitBackground , omitBackground: FFI.maybeToUndefined omitBackground
, optimizeForSpeed: FFI.maybeToUndefined optimizeForSpeed , optimizeForSpeed: FFI.maybeToUndefined optimizeForSpeed
, quality: FFI.maybeToUndefined quality , quality: FFI.maybeToUndefined quality
, format: FFI.maybeToUndefined $ map prepareScreenshotFormat format , type: FFI.maybeToUndefined $ map prepareScreenshotFormat format
} }

View File

@ -2,19 +2,23 @@ module Puppeteer.Handle.Spec where
import Prelude import Prelude
import Control.Monad.Error.Class (liftMaybe) import Control.Monad.Error.Class (liftMaybe, try)
import Control.Monad.Rec.Class (Step(..), tailRecM) import Control.Monad.Rec.Class (Step(..), tailRecM)
import Data.Array as Array import Data.Array as Array
import Data.Either (hush)
import Data.Filterable (filterMap)
import Data.Map as Map import Data.Map as Map
import Data.Set as Set
import Data.Maybe (isJust) import Data.Maybe (isJust)
import Data.Newtype (wrap) import Data.Newtype (wrap)
import Data.Set as Set
import Data.Traversable (for)
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import Effect (Effect) import Effect (Effect)
import Effect.Aff (Aff, forkAff, joinFiber) import Effect.Aff (Aff, forkAff, joinFiber)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Effect.Exception (error) import Effect.Exception (error)
import Node.Buffer as Buffer import Node.Buffer as Buffer
import Node.URL as Node.URL
import Puppeteer as Pup import Puppeteer as Pup
import Puppeteer.Base (timeoutThrow) import Puppeteer.Base (timeoutThrow)
import Puppeteer.Browser as Pup.Browser import Puppeteer.Browser as Pup.Browser
@ -91,6 +95,12 @@ html =
<div style="height: 100px; width: 100px; border: black solid 1px;" id="dragme" draggable="true"></div> <div style="height: 100px; width: 100px; border: black solid 1px;" id="dragme" draggable="true"></div>
<div style="height: 100px; width: 100px; border: red solid 1px;" id="dropme"></div> <div style="height: 100px; width: 100px; border: red solid 1px;" id="dropme"></div>
<div id="gone" style="position: fixed; top: 0; left: -100px; width: 10px; height: 10px;"></div> <div id="gone" style="position: fixed; top: 0; left: -100px; width: 10px; height: 10px;"></div>
<i id="attrs" visible disabled class="fart"></i>
<b></b>
<a href="http://foo.com"></a>
<a href="https://bar.com"></a>
<a href="https://baz.com"></a>
<a></a>
</div> </div>
</body> </body>
</html> </html>
@ -214,3 +224,14 @@ spec = withPage $ describe "Handle" do
describe "HTML" do describe "HTML" do
test "equals" findFirstOrHtmlEquals test "equals" findFirstOrHtmlEquals
test "attrs" \p -> do
anchors <- Pup.Page.findAll (S.anchor `S.hasAttr` "href") p
hrefs <- filterMap (Map.lookup "href") <$> for anchors Pup.Handle.HTML.attrs
urls <- liftEffect $ filterMap hush <$> for hrefs (try <<< Node.URL.new)
Array.length urls `shouldEqual` 3
anchors' <- Pup.Page.findAll (S.anchor `S.hasAttr` "href" `S.hasAttr` "disabled") p
hrefs' <- filterMap (Map.lookup "href") <$> for anchors' Pup.Handle.HTML.attrs
urls' <- liftEffect $ filterMap hush <$> for hrefs' (try <<< Node.URL.new)
Array.length urls' `shouldEqual` 0
pure unit