on master help sign in
Add some phantomjs tests
commit32fc361070e4c36267cb7d0b1a023c8d68c93f72 authorGreg Hale <imalsogreg@gmail.com> dateTue 20 Sep 2016, 00:09 (10 years ago) parent652b3ed
8 files changed+125−39
deps/reflex-platform+1−1 deps/servant-snap+1−1 exec/Example.hs+23−14 servant-reflex.cabal+6−6 test/Servant/Reflex.hs−12 test/Servant/ReflexSpec.hs+93 test/Spec.hs+1−1 test/Tests.hs−4
deps/reflex-platform+1−1

Submodule changed.

deps/servant-snap+1−1

Submodule changed.

exec/Example.hs+23−14
@@ -46,8 +46,8 @@ run = do
4646 client api (Proxy :: Proxy m) url
4747
4848 elClass "div" "demo-group" $ do
49− unitBtn <- button "Get unit"
50− intBtn <- button "Get int"
49+ unitBtn <- divClass "unit-button" $ button "Get unit"
50+ intBtn <- divClass "int-button" $ button "Get int"
5151
5252 unitResponse <- getUnit unitBtn
5353 intResponse :: Event t (ReqResult Int) <- getInt intBtn
@@ -58,47 +58,56 @@ run = do
5858 leftmost [fmapMaybe response unitResponse
5959 ,fmapMaybe response intResponse
6060 ]
61− dynText r >> el "br" (return ()) >> text "Total: " >> display score
61+ divClass "unit-int-response" $ el "p" $ dynText r >> el "br" (return ()) >> text "Total: " >> display score
6262
6363 elClass "div" "demo-group" $ do
6464
6565 text "Name"
6666 el "br" $ return ()
67− inp :: Dynamic t Text <- fmap value (textInput def)
67+ inp :: Dynamic t Text <- fmap value $ divClass "name-input" $ (textInput def)
6868 let checkedName = fmap (\i -> bool (QParamSome i) (QParamInvalid "Need a name") (T.null i)) inp
6969 el "br" $ return ()
7070
7171 text "Greetings (space-separated)"
7272 el "br" $ return ()
73− greetings <- fmap (fmap T.words . value) (textInput def)
73+ greetings <- fmap (fmap T.words . value) $
74+ divClass "greetings-input" $ (textInput def)
7475
7576 el "br" $ return ()
7677
77− gusto <- value <$> checkbox False def
78+ gusto <- fmap value $ divClass "gusto-input" $ checkbox False def
7879
7980 el "br" $ return ()
80− sayhiClicks :: Event t () <- button "Say hi"
81+ sayhiClicks :: Event t () <- divClass "hi-button" $ button "Say hi"
8182 let triggers = leftmost [sayhiClicks, () <$ updated inp]
8283
8384 resp <- sayhi checkedName greetings gusto triggers
84− dynText =<< holdDyn "No hi yet" (leftmost [fmapMaybe reqSuccess resp, fmapMaybe reqFailure resp])
85+ divClass "greeting-response" $ dynText =<<
86+ holdDyn "No hi yet" (leftmost [ fmapMaybe reqSuccess resp
87+ , fmapMaybe reqFailure resp])
8588
8689 elClass "div" "demo-group" $ do
8790 text "A Double to double"
8891 el "br" $ return ()
89− dblinp <- value <$> textInput def
90− dblBtn <- button "Double it"
91− dblResp <- dbl (fmap (note "read failure" . readMaybe . T.unpack) $ dblinp) dblBtn
92− dynText =<< holdDyn "(no errors)" (fmapMaybe reqFailure dblResp)
92+ dblinp <- fmap value $ divClass "double-input" $ textInput def
93+ dblBtn <- divClass "double-button" $ button "Double it"
94+ dblResp <- dbl (fmap (note "read failure" . readMaybe . T.unpack) $
95+ dblinp) dblBtn
96+ divClass "double-errors" $ dynText =<<
97+ holdDyn "(no errors)" (fmapMaybe reqFailure dblResp)
9398 el "br" (return ())
94− display =<< holdDyn "No number yet" (fmap tShow $ fmapMaybe reqSuccess dblResp)
99+ divClass "double-result" $ el "p" $ dynText =<<
100+ holdDyn "No number yet" (fmap tShow $
101+ fmapMaybe reqSuccess dblResp)
95102
96103 elClass "div" "demo-group" $ do
97104 text "Multi-part path"
98105 b <- value <$> checkbox False def
99106 mpGo <- button "Test"
100107 multiResp <- multi b mpGo
101− dynText =<< holdDyn "No res yet" (fmap tShow $ fmapMaybe reqSuccess $ multiResp)
108+ dynText =<< holdDyn "No res yet" (fmap tShow $
109+ fmapMaybe reqSuccess $
110+ multiResp)
102111
103112showXhrResponse :: XhrResponse -> Text
104113showXhrResponse (XhrResponse stat stattxt rbmay rtmay respHeaders) =
servant-reflex.cabal+6−6
@@ -51,16 +51,16 @@ executable example
5151 other-modules: API
5252 hs-source-dirs: exec
5353
54−test-suite testsuite
54+test-suite spec
5555 type: exitcode-stdio-1.0
56− main-is: Tests.hs
56+ main-is: Spec.hs
5757 hs-source-dirs: test
5858 ghc-prof-options: -auto
59− ghc-options: -Wall -fwarn-tabs -O2 -fhpc -threaded
59+ ghc-options: -Wall -fwarn-tabs -O2 -threaded
6060 other-modules:
6161 default-language: Haskell2010
6262 build-depends: base,
63+ hspec,
6364 hspec-webdriver,
64− reflex,
65− reflex-dom,
66− servant-reflex
65+ text,
66+ webdriver
test/Servant/Reflex.hsdeleted−12
@@ -1,12 +0,0 @@
1−{-# language OverloadedStrings #-}
2−module Servant.ReflexSpec where
3−
4−import Test.Hspec.WebDriver
5−
6−main :: IO ()
7−main = hspec $
8− describe "servant-reflex tests" $ do
9−
10− session "test page" $ using defaultConfig $ do
11− it "opens the page" $ runWD $
12− openPage "http://localhost:8000"
test/Servant/ReflexSpec.hsnew file+93
@@ -0,0 +1,93 @@
1+{-# language OverloadedStrings #-}
2+
3+module Servant.ReflexSpec where
4+
5+import Control.Concurrent (threadDelay)
6+import Control.Monad.IO.Class (liftIO)
7+import Data.Monoid ((<>))
8+import qualified Data.Text as T
9+import Data.Foldable
10+import Test.WebDriver.Commands.Wait
11+import Test.Hspec.WebDriver
12+
13+
14+clickingShouldCause :: Selector -> Selector -> Double -> (Element -> Element -> WD Bool) -> WD ()
15+clickingShouldCause inp outp timeLimit s = do
16+ inputEl <- findElem inp
17+ outputEl <- findElem outp
18+ click inputEl
19+ _ <- waitUntil timeLimit $ do
20+ r <- s inputEl outputEl
21+ return r
22+ return ()
23+
24+hasInfixText :: Element -> T.Text -> WD Bool
25+hasInfixText el t = getText el >>= \txt -> return $ t `T.isInfixOf` txt
26+
27+spec :: Spec
28+spec = do
29+ describe "servant-reflex tests" $ do
30+
31+ session "test page" $ using Chrome $ do
32+
33+ it "opens the page" $ runWD $
34+ openPage "http://localhost:8000"
35+
36+ it "has a div" $ runWD $ do
37+ e <- waitUntil 5 $ findElem $ ByCSS "div"
38+ e `shouldBeTag` "div"
39+
40+ it "has demo-group div" $ runWD $ do
41+ e <- findElem $ ByCSS "div.demo-group"
42+ e `shouldBeTag` "div"
43+
44+ it "doesn't have demo-group-nonexistent div" $ runWD $ do
45+ e <- findElems $ ByCSS "div.demo-group.nonexistent"
46+ e `shouldBe` []
47+
48+ it "can get an int or unit from the API" $ runWD $ do
49+ let outEl = ByCSS "div.unit-int-response p"
50+ clickingShouldCause (ByCSS "div.int-button > button") outEl 0.1
51+ (\i o -> o `hasInfixText` "Just \"100\"")
52+ clickingShouldCause (ByCSS "div.unit-button > button") outEl 0.1
53+ (\i o -> o `hasInfixText` "Just \"[[]]\"")
54+
55+ let setupGreeting = do
56+ iName <- findElem $ ByCSS ".name-input input"
57+ iGreetings <- findElem $ ByCSS ".greetings-input input"
58+ bGusto <- findElem $ ByCSS ".gusto-input input"
59+ goBtn <- findElem $ ByCSS ".hi-button button"
60+ greetingRes <- findElem $ ByCSS ".greeting-response"
61+ return (iName, iGreetings, bGusto, goBtn, greetingRes)
62+
63+ it "handle query params" $ runWD $ do
64+ (iName, iGreetings, bGusto, goBtn, greetingRes) <- setupGreeting
65+ sendKeys "Haskell" iName
66+ sendKeys "Hello Hi" iGreetings
67+ click goBtn
68+ greetingRes `shouldHaveText` "Hello, and Hi, Haskell"
69+
70+ it "handles queryflag" $ runWD $ do
71+ (iName, iGreetings, bGusto, goBtn, greetingRes) <- setupGreeting
72+ click bGusto
73+ click goBtn
74+ greetingRes `shouldHaveText` "HELLO, AND HI, HASKELL"
75+
76+ it "tags dyn promptly" $ runWD $ do
77+ (iName, iGreetings, bGusto, goBtn, greetingRes) <- setupGreeting
78+ sendKeys "!" iName
79+ greetingRes `shouldHaveText` "HELLO, AND HI, HASKELL!"
80+
81+{- Broken. Why?
82+ it "handles request bodies" $ runWD $ do
83+ dText <- findElem $ ByCSS ".double-input input"
84+ dSend <- findElem $ ByCSS ".double-button button"
85+ dRes <- findElem $ ByCSS ".double-result p"
86+ dErr <- findElem $ ByCSS ".double-errors"
87+ sendKeys "200" dText
88+ click dSend
89+ saveScreenshot "/home/ubuntu/screen.png"
90+ liftIO (threadDelay 1000000)
91+ dErr `shouldHaveText` "(no errors)"
92+ dRes `shouldHaveText` "400.0"
93+-}
test/Spec.hs+1−1
@@ -1 +1 @@
1−{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
1+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
test/Tests.hsdeleted−4
@@ -1,4 +0,0 @@
1−module Main where
2−
3−main :: IO ()
4−main = return ()