Skip to content

Commit

Permalink
adopt Playwright tests to use locator
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed May 15, 2024
1 parent 97e3ad5 commit 2395020
Showing 1 changed file with 57 additions and 41 deletions.
98 changes: 57 additions & 41 deletions testapp/test_e2e_inline.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from playwright.sync_api import expect

from testapp.models import Book

Expand All @@ -24,19 +25,21 @@ def get_end_order(direction):

@pytest.fixture
def adminpage(live_server, page, slug):
page.goto(f'{live_server.url}/admin/testapp/{slug}/{js_the_good_parts_id}/change/')
url = f'{live_server.url}/admin/testapp/{slug}/{js_the_good_parts_id}/change/'
page.goto(url)
return page


def is_fieldset_ordered(inline_elem, direction):
def expect_fieldset_is_ordered(inline_elem, direction):
expect(inline_elem).to_be_visible()
start_order = get_start_order(direction)
for counter, row in enumerate(inline_elem.query_selector_all('div.inline-related.has_original')):
order_field = row.query_selector('fieldset input._reorder_')
assert order_field is not None
inline_related = inline_elem.locator('div.inline-related.has_original')
for counter in range(inline_related.count()):
row = inline_related.nth(counter)
order_field = row.locator('fieldset input._reorder_')
expect(order_field).to_be_hidden()
order = start_order + direction * counter
if order_field.input_value() != str(order):
return False
return True
expect(order_field).to_have_value(str(order))


@pytest.fixture
Expand All @@ -53,55 +56,68 @@ def chapter(slug):
return '#chapter'


@pytest.fixture
def drag_selector(slug):
if slug in ['book5']:
return '> td > p'
return '> h3'


@pytest.mark.parametrize('slug', slugs)
def test_drag_down(adminpage, slug, direction, chapter):
inline_locator = adminpage.locator(f'{chapter}_set-group')
assert is_fieldset_ordered(inline_locator.element_handle(), direction)
def test_drag_down(adminpage, slug, direction, chapter, drag_selector):
group_locator = adminpage.locator(f'{chapter}_set-group')
expect_fieldset_is_ordered(group_locator, direction)
start_order = get_start_order(direction)
assert inline_locator.locator(f'{chapter}_set-0 input._reorder_').input_value() == str(start_order)
drag_handle = inline_locator.locator(f'{chapter}_set-0 :is(h3, p)')
drag_handle.drag_to(inline_locator.locator(f'{chapter}_set-4'))
assert inline_locator.locator(f'{chapter}_set-0 input._reorder_').input_value() == str(start_order + direction * 4)
assert inline_locator.locator(f'{chapter}_set-1 input._reorder_').input_value() == str(start_order)
assert is_fieldset_ordered(inline_locator.element_handle(), direction)
expect(group_locator.locator(f'{chapter}_set-0 input._reorder_')).to_have_value(str(start_order))
if slug in ['book6']:
adminpage.screenshot(path=f'../workdir/screenshot-{slug}-before.png')
drag_kwargs = {'source_position': {'x': 200, 'y': 10}, 'target_position': {'x': 200, 'y': 10}}
drag_handle = group_locator.locator(f'{chapter}_set-0 {drag_selector}')
expect(drag_handle).to_be_visible()
drag_handle.drag_to(group_locator.locator(f'{chapter}_set-4'), **drag_kwargs)
if slug in ['book6']:
adminpage.screenshot(path=f'../workdir/screenshot-{slug}-after.png')
expect(group_locator.locator(f'{chapter}_set-0 input._reorder_')).to_have_value(str(start_order + direction * 4))
expect(group_locator.locator(f'{chapter}_set-1 input._reorder_')).to_have_value(str(start_order))
expect_fieldset_is_ordered(group_locator, direction)


@pytest.mark.parametrize('slug', slugs)
def test_drag_up(adminpage, slug, direction, chapter):
inline_locator = adminpage.locator(f'{chapter}_set-group')
assert is_fieldset_ordered(inline_locator.element_handle(), direction)
def test_drag_up(adminpage, slug, direction, chapter, drag_selector):
group_locator = adminpage.locator(f'{chapter}_set-group')
expect_fieldset_is_ordered(group_locator, direction)
start_order = get_start_order(direction)
reorder_field = inline_locator.locator(f'{chapter}_set-5 input._reorder_')
assert reorder_field.input_value() == str(start_order + direction * 5)
drag_handle = inline_locator.locator(f'{chapter}_set-5 :is(h3, p)')
drag_handle.drag_to(inline_locator.locator(f'{chapter}_set-1'))
assert inline_locator.locator(f'{chapter}_set-5 input._reorder_').input_value() == str(start_order + direction)
assert inline_locator.locator(f'{chapter}_set-1 input._reorder_').input_value() == str(start_order + direction * 2)
assert is_fieldset_ordered(inline_locator.element_handle(), direction)
expect(group_locator.locator(f'{chapter}_set-5 input._reorder_')).to_have_value(str(start_order + direction * 5))
drag_kwargs = {'source_position': {'x': 200, 'y': 10}, 'target_position': {'x': 200, 'y': 10}}
drag_handle = group_locator.locator(f'{chapter}_set-5 {drag_selector}')
drag_handle.drag_to(group_locator.locator(f'{chapter}_set-1'), **drag_kwargs)
expect(group_locator.locator(f'{chapter}_set-5 input._reorder_')).to_have_value(str(start_order + direction))
expect(group_locator.locator(f'{chapter}_set-1 input._reorder_')).to_have_value(str(start_order + direction * 2))
expect_fieldset_is_ordered(group_locator, direction)


@pytest.mark.parametrize('slug', ['book1', 'book2', 'book5'])
def test_move_end(adminpage, slug, direction, chapter):
def test_move_end(adminpage, slug, direction, chapter, drag_selector):
inline_locator = adminpage.locator(f'{chapter}_set-group')
assert is_fieldset_ordered(inline_locator.element_handle(), direction)
expect_fieldset_is_ordered(inline_locator, direction)
start_order = get_start_order(direction)
end_order = get_end_order(direction)
assert inline_locator.locator(f'{chapter}_set-2 input._reorder_').input_value() == str(start_order + direction * 2)
move_end_button = inline_locator.locator(f'{chapter}_set-2 :is(h3, p) .move-end').element_handle()
expect(inline_locator.locator(f'{chapter}_set-2 input._reorder_')).to_have_value(str(start_order + direction * 2))
move_end_button = inline_locator.locator(f'{chapter}_set-2 {drag_selector} .move-end').element_handle()
move_end_button.click()
assert inline_locator.locator(f'{chapter}_set-2 input._reorder_').input_value() == str(end_order)
assert inline_locator.locator(f'{chapter}_set-3 input._reorder_').input_value() == str(start_order + direction * 2)
assert is_fieldset_ordered(inline_locator.element_handle(), direction)
expect(inline_locator.locator(f'{chapter}_set-2 input._reorder_')).to_have_value(str(end_order))
expect(inline_locator.locator(f'{chapter}_set-3 input._reorder_')).to_have_value(str(start_order + direction * 2))
expect_fieldset_is_ordered(inline_locator, direction)


@pytest.mark.parametrize('slug', ['book1', 'book2', 'book5'])
def test_move_begin(adminpage, slug, direction, chapter):
def test_move_begin(adminpage, slug, direction, chapter, drag_selector):
inline_locator = adminpage.locator(f'{chapter}_set-group')
assert is_fieldset_ordered(inline_locator.element_handle(), direction)
expect_fieldset_is_ordered(inline_locator, direction)
start_order = get_start_order(direction)
assert inline_locator.locator(f'{chapter}_set-8 input._reorder_').input_value() == str(start_order + direction * 8)
move_end_button = inline_locator.locator(f'{chapter}_set-8 :is(h3, p) .move-begin').element_handle()
expect(inline_locator.locator(f'{chapter}_set-8 input._reorder_')).to_have_value(str(start_order + direction * 8))
move_end_button = inline_locator.locator(f'{chapter}_set-8 {drag_selector} .move-begin')
move_end_button.click()
assert inline_locator.locator(f'{chapter}_set-8 input._reorder_').input_value() == str(start_order)
assert inline_locator.locator(f'{chapter}_set-3 input._reorder_').input_value() == str(start_order + direction * 4)
assert is_fieldset_ordered(inline_locator.element_handle(), direction)
expect(inline_locator.locator(f'{chapter}_set-8 input._reorder_')).to_have_value(str(start_order))
expect(inline_locator.locator(f'{chapter}_set-3 input._reorder_')).to_have_value(str(start_order + direction * 4))
expect_fieldset_is_ordered(inline_locator, direction)

0 comments on commit 2395020

Please sign in to comment.