module Puppeteer.Mouse.Spec where import Prelude import Control.Monad.Error.Class (liftMaybe) import Data.Array as Array import Data.Map as Map import Data.Maybe (Maybe(..)) import Data.Newtype (wrap) import Data.Time.Duration (Milliseconds(..)) import Effect (Effect) import Effect.Aff (Aff, delay) import Effect.Class (liftEffect) import Effect.Exception (error) import Puppeteer as Pup import Puppeteer.Base (timeout') import Puppeteer.Browser as Pup.Browser import Puppeteer.Handle as Pup.Handle import Puppeteer.Handle.HTML as Pup.Handle.HTML import Puppeteer.Keyboard as Pup.Keyboard import Puppeteer.Mouse as Mouse import Puppeteer.Page as Pup.Page import Puppeteer.Page.Event (connectPageConsole) import Puppeteer.Page.Event.Spec as Spec.Page.Event import Puppeteer.Page.WaitFor as Pup.Page.WaitFor import Test.Spec (SpecT, afterAll, beforeAll, beforeWith, describe) import Test.Spec.Assertions (shouldEqual) import Test.Util (failOnPageError, test) content :: String content = """ Simple Page
""" spec :: SpecT Aff Unit Effect Unit spec = beforeAll (Pup.launch_ =<< Pup.new) $ afterAll Pup.Browser.close $ describe "Mouse" do test "move" \b -> do p <- Pup.Page.new b liftEffect $ connectPageConsole p failOnPageError p do Pup.Page.setContent content Pup.Load p mouse <- liftEffect $ Pup.Page.mouse p { x: fooX, y: fooY } <- liftMaybe (error "#foo does not have bounding box") =<< Pup.Handle.boundingBox =<< liftMaybe (error "#foo not found") =<< Pup.Page.findFirst "div#foo" p { x: barX, y: barY } <- liftMaybe (error "#bar does not have bounding box") =<< Pup.Handle.boundingBox =<< liftMaybe (error "#bar not found") =<< Pup.Page.findFirst "div#bar" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseenter" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseenter" p Mouse.moveTo { steps: 10.0 } mouse { x: fooX + 1.0, y: fooY + 1.0 } shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseenter" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseenter" p Mouse.moveTo { steps: 10.0 } mouse { x: barX + 1.0, y: barY + 1.0 } shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseenter" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseenter" p test "up / down" \b -> do p <- Pup.Page.new b liftEffect $ connectPageConsole p failOnPageError p do Pup.Page.setContent content Pup.Load p mouse <- liftEffect $ Pup.Page.mouse p { x: fooX, y: fooY } <- liftMaybe (error "#foo does not have bounding box") =<< Pup.Handle.boundingBox =<< liftMaybe (error "#foo not found") =<< Pup.Page.findFirst "div#foo" p { x: barX, y: barY } <- liftMaybe (error "#bar does not have bounding box") =<< Pup.Handle.boundingBox =<< liftMaybe (error "#bar not found") =<< Pup.Page.findFirst "div#bar" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.foo-mousedown" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseup" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mousedown" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseup" p Mouse.moveTo { steps: 10.0 } mouse { x: fooX + 1.0, y: fooY + 1.0 } Mouse.down Mouse.MouseLeft mouse shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mousedown" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseup" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mousedown" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseup" p Mouse.up Mouse.MouseLeft mouse shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mousedown" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseup" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mousedown" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseup" p Mouse.moveTo { steps: 10.0 } mouse { x: barX + 1.0, y: barY + 1.0 } Mouse.down Mouse.MouseLeft mouse shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mousedown" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseup" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.bar-mousedown" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseup" p Mouse.up Mouse.MouseLeft mouse shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mousedown" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseup" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.bar-mousedown" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseup" p test "click" \b -> do p <- Pup.Page.new b liftEffect $ connectPageConsole p failOnPageError p do Pup.Page.setContent content Pup.Load p mouse <- liftEffect $ Pup.Page.mouse p { x: fooX, y: fooY } <- liftMaybe (error "#foo does not have bounding box") =<< Pup.Handle.boundingBox =<< liftMaybe (error "#foo not found") =<< Pup.Page.findFirst "div#foo" p { x: barX, y: barY } <- liftMaybe (error "#bar does not have bounding box") =<< Pup.Handle.boundingBox =<< liftMaybe (error "#bar not found") =<< Pup.Page.findFirst "div#bar" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.foo-mousedown" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseup" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mousedown" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseup" p Mouse.click {} mouse { x: fooX + 1.0, y: fooY + 1.0 } shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mousedown" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseup" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mousedown" p shouldEqual 0 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseup" p Mouse.click {} mouse { x: barX + 1.0, y: barY + 1.0 } shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mousedown" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.foo-mouseup" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.bar-mousedown" p shouldEqual 1 =<< Array.length <$> Pup.Page.findAll "div.bar-mouseup" p