1+ import re
12import time
3+ from typing import Any , cast
24
35from playwright .sync_api import expect , sync_playwright
46
@@ -12,7 +14,7 @@ def setup_all(cls):
1214 cls .context = cls .browser .new_context ()
1315 cls .context .tracing .start (screenshots = True , snapshots = True , sources = True )
1416
15- expect .set_options (timeout = 2 )
17+ expect .set_options (timeout = 200 )
1618
1719
1820def teardown_all (cls ):
@@ -21,7 +23,35 @@ def teardown_all(cls):
2123 cls .playwright .stop ()
2224
2325
26+ def drag_slider (page , offset_px = 80 ):
27+ """Drag the slider handle by offset_px (positive = right, negative = left)."""
28+ slider = page .get_by_role ("slider" )
29+ box = slider .bounding_box ()
30+ assert box , "Slider handle should have bounding box"
31+ center_x = box ["x" ] + box ["width" ] / 2
32+ center_y = box ["y" ] + box ["height" ] / 2
33+ page .mouse .move (center_x , center_y )
34+ page .mouse .down ()
35+ page .mouse .move (center_x + offset_px , center_y )
36+ page .mouse .up ()
37+ time .sleep (0.3 )
38+
39+
40+ def login (page , base_url = "http://localhost:3000/" ):
41+ """Log in with username_1 / Test1234 on the given page."""
42+ page .goto (base_url )
43+ page .get_by_role ("button" , name = "Log in" ).click ()
44+ page .get_by_placeholder ("username or email" ).fill ("username_1" )
45+ page .get_by_placeholder ("password" ).fill ("Test1234" )
46+ page .get_by_role ("button" , name = "Log in" , exact = True ).last .click ()
47+ time .sleep (1 )
48+
49+
2450class TestSimpleFunctionality :
51+ playwright = None
52+ browser = None
53+ context = None
54+
2555 @classmethod
2656 def setup_class (cls ):
2757 setup_all (cls )
@@ -30,23 +60,66 @@ def setup_class(cls):
3060 def teardown_class (cls ):
3161 teardown_all (cls )
3262
33- @classmethod
34- def test_login (cls ):
35- context = cls .context
63+ def teardown_method (self , method ):
64+ context = cast (Any , self .__class__ .context )
65+ if context is not None :
66+ for page in context .pages :
67+ page .close ()
68+ context .clear_cookies ()
69+
70+ def test_login (self ):
71+ context = cast (Any , self .__class__ .context )
72+ assert context is not None
3673 page = context .new_page ()
3774 page .on ("console" , lambda msg : print ("[Browser console] " , msg .text ))
3875
39- url = "http://localhost:3000/"
40- page .goto (url )
76+ login (page )
4177
42- page .get_by_role ("button" , name = "Log in" ).click ()
78+ page .get_by_role ("banner" ).get_by_role ("link" , name = "Questions" ).click ()
79+ page .get_by_role ("button" , name = "Filter" ).click ()
80+ page .get_by_role ("button" , name = "Binary" ).click ()
81+ page .get_by_role ("button" , name = "Open" ).click ()
82+ page .get_by_role ("button" , name = "Done" ).click ()
4383
44- page .get_by_placeholder ("username or email" ).fill ("username_1" )
45- page .get_by_placeholder ("password" ).fill ("Test1234" )
46- page .get_by_role ("button" , name = "Log in" , exact = True ).last .click ()
47- time .sleep (1 )
84+ def test_create_binary_forecast_via_ui (self ):
85+ context = cast (Any , self .__class__ .context )
86+ assert context is not None
87+
88+ page = context .new_page ()
89+ page .on ("console" , lambda msg : print ("[Browser console] " , msg .text ))
90+
91+ login (page )
4892 page .get_by_role ("banner" ).get_by_role ("link" , name = "Questions" ).click ()
4993 page .get_by_role ("button" , name = "Filter" ).click ()
5094 page .get_by_role ("button" , name = "Binary" ).click ()
5195 page .get_by_role ("button" , name = "Open" ).click ()
96+ time .sleep (1 )
5297 page .get_by_role ("button" , name = "Done" ).click ()
98+ time .sleep (1 )
99+ links = page .locator ("a[href^='/questions/']" )
100+ for i in range (links .count ()):
101+ href = links .nth (i ).get_attribute ("href" ) or ""
102+ if re .match (r"/questions/\d+/" , href ):
103+ links .nth (i ).click ()
104+ break
105+
106+ time .sleep (1 )
107+
108+ drag_slider (page , offset_px = 80 )
109+
110+ slider = page .get_by_role ("slider" )
111+ value_div = slider .locator ("div" )
112+ predicted_value = value_div .inner_text ()
113+
114+ predict_button = page .get_by_role (
115+ "button" , name = re .compile (r"(Predict|Save changes|Reaffirm)" , re .IGNORECASE )
116+ ).first
117+ predict_button .click ()
118+
119+ time .sleep (1 )
120+ page .reload ()
121+ time .sleep (1 )
122+
123+ slider_after = page .get_by_role ("slider" )
124+ value_div_after = slider_after .locator ("div" )
125+ expect (value_div_after ).to_have_text (predicted_value )
0 commit comments