11import pytest
2+ from playwright .sync_api import expect
23
34from testapp .models import Book
45
@@ -24,19 +25,21 @@ def get_end_order(direction):
2425
2526@pytest .fixture
2627def adminpage (live_server , page , slug ):
27- page .goto (f'{ live_server .url } /admin/testapp/{ slug } /{ js_the_good_parts_id } /change/' )
28+ url = f'{ live_server .url } /admin/testapp/{ slug } /{ js_the_good_parts_id } /change/'
29+ page .goto (url )
2830 return page
2931
3032
31- def is_fieldset_ordered (inline_elem , direction ):
33+ def expect_fieldset_is_ordered (inline_elem , direction ):
34+ expect (inline_elem ).to_be_visible ()
3235 start_order = get_start_order (direction )
33- for counter , row in enumerate (inline_elem .query_selector_all ('div.inline-related.has_original' )):
34- order_field = row .query_selector ('fieldset input._reorder_' )
35- assert order_field is not None
36+ inline_related = inline_elem .locator ('div.inline-related.has_original' )
37+ for counter in range (inline_related .count ()):
38+ row = inline_related .nth (counter )
39+ order_field = row .locator ('fieldset input._reorder_' )
40+ expect (order_field ).to_be_hidden ()
3641 order = start_order + direction * counter
37- if order_field .input_value () != str (order ):
38- return False
39- return True
42+ expect (order_field ).to_have_value (str (order ))
4043
4144
4245@pytest .fixture
@@ -53,55 +56,68 @@ def chapter(slug):
5356 return '#chapter'
5457
5558
59+ @pytest .fixture
60+ def drag_selector (slug ):
61+ if slug in ['book5' ]:
62+ return '> td > p'
63+ return '> h3'
64+
65+
5666@pytest .mark .parametrize ('slug' , slugs )
57- def test_drag_down (adminpage , slug , direction , chapter ):
58- inline_locator = adminpage .locator (f'{ chapter } _set-group' )
59- assert is_fieldset_ordered ( inline_locator . element_handle () , direction )
67+ def test_drag_down (adminpage , slug , direction , chapter , drag_selector ):
68+ group_locator = adminpage .locator (f'{ chapter } _set-group' )
69+ expect_fieldset_is_ordered ( group_locator , direction )
6070 start_order = get_start_order (direction )
61- assert inline_locator .locator (f'{ chapter } _set-0 input._reorder_' ).input_value () == str (start_order )
62- drag_handle = inline_locator .locator (f'{ chapter } _set-0 :is(h3, p)' )
63- drag_handle .drag_to (inline_locator .locator (f'{ chapter } _set-4' ))
64- assert inline_locator .locator (f'{ chapter } _set-0 input._reorder_' ).input_value () == str (start_order + direction * 4 )
65- assert inline_locator .locator (f'{ chapter } _set-1 input._reorder_' ).input_value () == str (start_order )
66- assert is_fieldset_ordered (inline_locator .element_handle (), direction )
71+ expect (group_locator .locator (f'{ chapter } _set-0 input._reorder_' )).to_have_value (str (start_order ))
72+ if slug in ['book6' ]:
73+ adminpage .screenshot (path = f'../workdir/screenshot-{ slug } -before.png' )
74+ drag_kwargs = {'source_position' : {'x' : 200 , 'y' : 10 }, 'target_position' : {'x' : 200 , 'y' : 10 }}
75+ drag_handle = group_locator .locator (f'{ chapter } _set-0 { drag_selector } ' )
76+ expect (drag_handle ).to_be_visible ()
77+ drag_handle .drag_to (group_locator .locator (f'{ chapter } _set-4' ), ** drag_kwargs )
78+ if slug in ['book6' ]:
79+ adminpage .screenshot (path = f'../workdir/screenshot-{ slug } -after.png' )
80+ expect (group_locator .locator (f'{ chapter } _set-0 input._reorder_' )).to_have_value (str (start_order + direction * 4 ))
81+ expect (group_locator .locator (f'{ chapter } _set-1 input._reorder_' )).to_have_value (str (start_order ))
82+ expect_fieldset_is_ordered (group_locator , direction )
6783
6884
6985@pytest .mark .parametrize ('slug' , slugs )
70- def test_drag_up (adminpage , slug , direction , chapter ):
71- inline_locator = adminpage .locator (f'{ chapter } _set-group' )
72- assert is_fieldset_ordered ( inline_locator . element_handle () , direction )
86+ def test_drag_up (adminpage , slug , direction , chapter , drag_selector ):
87+ group_locator = adminpage .locator (f'{ chapter } _set-group' )
88+ expect_fieldset_is_ordered ( group_locator , direction )
7389 start_order = get_start_order (direction )
74- reorder_field = inline_locator .locator (f'{ chapter } _set-5 input._reorder_' )
75- assert reorder_field . input_value () == str ( start_order + direction * 5 )
76- drag_handle = inline_locator .locator (f'{ chapter } _set-5 :is(h3, p) ' )
77- drag_handle .drag_to (inline_locator .locator (f'{ chapter } _set-1' ))
78- assert inline_locator .locator (f'{ chapter } _set-5 input._reorder_' ). input_value () == str (start_order + direction )
79- assert inline_locator .locator (f'{ chapter } _set-1 input._reorder_' ). input_value () == str (start_order + direction * 2 )
80- assert is_fieldset_ordered ( inline_locator . element_handle () , direction )
90+ expect ( group_locator .locator (f'{ chapter } _set-5 input._reorder_' )). to_have_value ( str ( start_order + direction * 5 ) )
91+ drag_kwargs = { 'source_position' : { 'x' : 200 , 'y' : 10 }, 'target_position' : { 'x' : 200 , 'y' : 10 }}
92+ drag_handle = group_locator .locator (f'{ chapter } _set-5 { drag_selector } ' )
93+ drag_handle .drag_to (group_locator .locator (f'{ chapter } _set-1' ), ** drag_kwargs )
94+ expect ( group_locator .locator (f'{ chapter } _set-5 input._reorder_' )). to_have_value ( str (start_order + direction ) )
95+ expect ( group_locator .locator (f'{ chapter } _set-1 input._reorder_' )). to_have_value ( str (start_order + direction * 2 ) )
96+ expect_fieldset_is_ordered ( group_locator , direction )
8197
8298
8399@pytest .mark .parametrize ('slug' , ['book1' , 'book2' , 'book5' ])
84- def test_move_end (adminpage , slug , direction , chapter ):
100+ def test_move_end (adminpage , slug , direction , chapter , drag_selector ):
85101 inline_locator = adminpage .locator (f'{ chapter } _set-group' )
86- assert is_fieldset_ordered (inline_locator . element_handle () , direction )
102+ expect_fieldset_is_ordered (inline_locator , direction )
87103 start_order = get_start_order (direction )
88104 end_order = get_end_order (direction )
89- assert inline_locator .locator (f'{ chapter } _set-2 input._reorder_' ). input_value () == str (start_order + direction * 2 )
90- move_end_button = inline_locator .locator (f'{ chapter } _set-2 :is(h3, p) .move-end' ).element_handle ()
105+ expect ( inline_locator .locator (f'{ chapter } _set-2 input._reorder_' )). to_have_value ( str (start_order + direction * 2 ) )
106+ move_end_button = inline_locator .locator (f'{ chapter } _set-2 { drag_selector } .move-end' ).element_handle ()
91107 move_end_button .click ()
92- assert inline_locator .locator (f'{ chapter } _set-2 input._reorder_' ). input_value () == str (end_order )
93- assert inline_locator .locator (f'{ chapter } _set-3 input._reorder_' ). input_value () == str (start_order + direction * 2 )
94- assert is_fieldset_ordered (inline_locator . element_handle () , direction )
108+ expect ( inline_locator .locator (f'{ chapter } _set-2 input._reorder_' )). to_have_value ( str (end_order ) )
109+ expect ( inline_locator .locator (f'{ chapter } _set-3 input._reorder_' )). to_have_value ( str (start_order + direction * 2 ) )
110+ expect_fieldset_is_ordered (inline_locator , direction )
95111
96112
97113@pytest .mark .parametrize ('slug' , ['book1' , 'book2' , 'book5' ])
98- def test_move_begin (adminpage , slug , direction , chapter ):
114+ def test_move_begin (adminpage , slug , direction , chapter , drag_selector ):
99115 inline_locator = adminpage .locator (f'{ chapter } _set-group' )
100- assert is_fieldset_ordered (inline_locator . element_handle () , direction )
116+ expect_fieldset_is_ordered (inline_locator , direction )
101117 start_order = get_start_order (direction )
102- assert inline_locator .locator (f'{ chapter } _set-8 input._reorder_' ). input_value () == str (start_order + direction * 8 )
103- move_end_button = inline_locator .locator (f'{ chapter } _set-8 :is(h3, p) .move-begin' ). element_handle ( )
118+ expect ( inline_locator .locator (f'{ chapter } _set-8 input._reorder_' )). to_have_value ( str (start_order + direction * 8 ) )
119+ move_end_button = inline_locator .locator (f'{ chapter } _set-8 { drag_selector } .move-begin' )
104120 move_end_button .click ()
105- assert inline_locator .locator (f'{ chapter } _set-8 input._reorder_' ). input_value () == str (start_order )
106- assert inline_locator .locator (f'{ chapter } _set-3 input._reorder_' ). input_value () == str (start_order + direction * 4 )
107- assert is_fieldset_ordered (inline_locator . element_handle () , direction )
121+ expect ( inline_locator .locator (f'{ chapter } _set-8 input._reorder_' )). to_have_value ( str (start_order ) )
122+ expect ( inline_locator .locator (f'{ chapter } _set-3 input._reorder_' )). to_have_value ( str (start_order + direction * 4 ) )
123+ expect_fieldset_is_ordered (inline_locator , direction )
0 commit comments