Skip to content

Commit

Permalink
💚 [#4051] Fix broken e2e tests
Browse files Browse the repository at this point in the history
Added a helper to put (JSON) expressions in the editors so that the
user interaction can be simulated. For speed reasons, it uses copy and
paste. Solution is taken from microsoft/playwright#14126
  • Loading branch information
sergei-maertens committed Jul 12, 2024
1 parent 7cd3ab9 commit b43ea71
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/openforms/forms/tests/e2e_tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json
from contextlib import contextmanager

from playwright.async_api import Page, expect
from playwright.async_api import Locator, Page, expect

from openforms.typing import JSONValue


@contextmanager
Expand Down Expand Up @@ -30,3 +33,26 @@ async def click_modal_button(page: Page, button_text: str, **kwargs):
async def close_modal(page: Page, button_text: str, **kwargs):
modal = await click_modal_button(page, button_text, **kwargs)
await expect(modal).to_be_hidden()


async def enter_json_in_editor(
page: Page, editor: Locator, expression: JSONValue
) -> None:
"""
Put some JSON into a monaca-json-editor instance.
:arg locator: The locator (`page.locator(".monaco-editor")`) pointing to the
editor instance.
:arg expression: The JSON expression. Will be serialized to JSON before putting it
in the input.
"""
await expect(editor).to_be_visible()
code = json.dumps(expression)
# put the code in the clipboard and do a paste event
await page.evaluate("text => navigator.clipboard.writeText(text)", code)
# click the editor to focus it
await editor.click()
# select all
await page.keyboard.press("ControlOrMeta+KeyA")
# and replace with paste
await page.keyboard.press("ControlOrMeta+KeyV")
4 changes: 3 additions & 1 deletion src/openforms/forms/tests/e2e_tests/test_form_designer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .helpers import (
click_modal_button,
close_modal,
enter_json_in_editor,
open_component_options_modal,
phase,
)
Expand Down Expand Up @@ -701,7 +702,8 @@ def get_formstep_uuid():
await page.locator("[name='triggerFromStep']").select_option(
label="Playwright test"
)
await page.locator("[name='jsonLogicTrigger']").fill('{"==": [1, 1]}')
editor = page.locator(".monaco-editor")
await enter_json_in_editor(page, editor, {"==": [1, 1]})

with phase("Save logic rule and check state"):
await page.get_by_text("Save and continue editing").click()
Expand Down
4 changes: 2 additions & 2 deletions src/openforms/forms/tests/e2e_tests/test_logic_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from ...models import FormLogic
from ..factories import FormFactory
from .helpers import enter_json_in_editor


class LogicTabTests(E2ETestCase):
Expand Down Expand Up @@ -47,9 +48,8 @@ def setUpTestData():
await page.get_by_text("Add rule").click()
await page.get_by_role("button", name="Advanced").click()

textarea = page.locator("[name='jsonLogicTrigger']")
await enter_json_in_editor(page, page.locator(".monaco-editor"), True)

await textarea.fill("true")
await page.get_by_text("Add Action").click()
await page.locator('select[name="action.type"]').select_option(
label="change a property of a component."
Expand Down

0 comments on commit b43ea71

Please sign in to comment.