diff --git a/vasl_templates/webapp/tests/test_ob.py b/vasl_templates/webapp/tests/test_ob.py index 9a1e1cf..ea79885 100644 --- a/vasl_templates/webapp/tests/test_ob.py +++ b/vasl_templates/webapp/tests/test_ob.py @@ -6,7 +6,8 @@ import types from selenium.webdriver.support.ui import Select from vasl_templates.webapp.tests.utils import \ - get_nationalities, get_clipboard, get_stored_msg, set_stored_msg_marker, select_tab, find_child, find_children, \ + get_nationalities, wait_for_clipboard, get_stored_msg, set_stored_msg_marker, select_tab, \ + find_child, find_children, \ add_simple_note, edit_simple_note, get_sortable_entry_count, drag_sortable_entry_to_trash, \ select_droplist_val, init_webapp, wait_for, adjust_html @@ -47,7 +48,7 @@ def _do_test_ob_entries( webapp, webdriver, ob_type ): elems[entry_no].click() if ob_type == "ob_notes": expected = re.sub( r" \(col=.*?\)", "", expected ) - assert adjust_html( get_clipboard() ) == expected + assert wait_for_clipboard( 2, expected, transform=adjust_html ) select_tab( "ob1" ) check_snippet( sortable1, 0, "[German] [{} #1] (col=[OBCOL:german/OBCOL2:german])".format( ob_type ) @@ -110,7 +111,7 @@ def test_nationality_specific( webapp, webdriver ): #pylint: disable=too-many-lo select_tab( "ob1" ) marker = set_stored_msg_marker( "_last-warning_" ) btn.click() - assert get_clipboard() == expected + wait_for_clipboard( 2, expected ) # check if a warning was issued last_warning = get_stored_msg( "_last-warning_" ) if warning: @@ -209,7 +210,7 @@ def test_nationality_specific( webapp, webdriver ): #pylint: disable=too-many-lo # make sure that the template works elem.click() if isinstance( expected[1], str ): - assert get_clipboard() == expected[1] + wait_for_clipboard( 2, expected[1] ) elif isinstance( expected[1], types.FunctionType ): expected[1]() #pylint: disable=not-callable else: diff --git a/vasl_templates/webapp/tests/test_snippets.py b/vasl_templates/webapp/tests/test_snippets.py index 5c19476..84305f4 100644 --- a/vasl_templates/webapp/tests/test_snippets.py +++ b/vasl_templates/webapp/tests/test_snippets.py @@ -3,8 +3,8 @@ from selenium.webdriver.common.keys import Keys from vasl_templates.webapp.tests.utils import \ - init_webapp, select_tab, set_template_params, get_clipboard, \ - get_stored_msg, set_stored_msg_marker, find_child, wait_for, adjust_html, \ + init_webapp, select_tab, set_template_params, wait_for_clipboard, \ + get_stored_msg, set_stored_msg_marker, find_child, adjust_html, \ for_each_template, add_simple_note, edit_simple_note, \ get_sortable_entry_count, generate_sortable_entry_snippet, drag_sortable_entry_to_trash @@ -199,7 +199,7 @@ def test_edit_templates( webapp, webdriver ): # check that the new template is being used elem = find_child( "button.generate[data-id='{}']".format( orig_template_id ) ) elem.click() - wait_for( 2, lambda: get_clipboard() == "EDITED TEMPLATE: {}".format( orig_template_id ) ) + wait_for_clipboard( 2, "EDITED TEMPLATE: {}".format( orig_template_id ) ) for_each_template( test_template ) # customize the SCENARIO NOTE template @@ -213,7 +213,7 @@ def test_edit_templates( webapp, webdriver ): add_simple_note( sortable, "scenario note (ignored)", None ) elem = find_child( "li img.snippet", sortable ) elem.click() - assert get_clipboard() == "EDITED TEMPLATE: scenario_note" + wait_for_clipboard( 2, "EDITED TEMPLATE: scenario_note" ) # customize the OB SETUP template select_tab( "ob1" ) @@ -228,7 +228,7 @@ def test_edit_templates( webapp, webdriver ): add_simple_note( sortable, "ob setup (ignored)", None ) elem = find_child( "li img.snippet", sortable ) elem.click() - assert get_clipboard() == "EDITED TEMPLATE: ob_setup" + wait_for_clipboard( 2, "EDITED TEMPLATE: ob_setup" ) # customize the OB NOTE template select_tab( "ob2" ) @@ -243,7 +243,7 @@ def test_edit_templates( webapp, webdriver ): add_simple_note( sortable, "ob note (ignored)", None ) elem = find_child( "li img.snippet", sortable ) elem.click() - assert get_clipboard() == "EDITED TEMPLATE: ob_note" + wait_for_clipboard( 2, "EDITED TEMPLATE: ob_note" ) # --------------------------------------------------------------------- @@ -254,10 +254,10 @@ def _test_snippet( btn, params, expected, expected2 ): set_template_params( params ) marker = set_stored_msg_marker( "_last-warning_" ) btn.click() - snippet = get_clipboard() - lines = [ l.strip() for l in snippet.split("\n") ] - snippet = " | ".join( l for l in lines if l ) - assert snippet == expected + def reformat( clipboard ): #pylint: disable=missing-docstring + lines = [ l.strip() for l in clipboard.split("\n") ] + return " | ".join( l for l in lines if l ) + wait_for_clipboard( 2, expected, transform=reformat ) # check warnings for mandatory parameters last_warning = get_stored_msg( "_last-warning_" ) diff --git a/vasl_templates/webapp/tests/test_ssr.py b/vasl_templates/webapp/tests/test_ssr.py index b0030f1..0a02bde 100644 --- a/vasl_templates/webapp/tests/test_ssr.py +++ b/vasl_templates/webapp/tests/test_ssr.py @@ -3,7 +3,7 @@ import html from vasl_templates.webapp.tests.utils import \ - init_webapp, select_tab, find_child, get_clipboard, adjust_html, \ + init_webapp, select_tab, find_child, wait_for_clipboard, adjust_html, \ add_simple_note, edit_simple_note, drag_sortable_entry_to_trash, get_sortable_entry_count # --------------------------------------------------------------------- @@ -35,7 +35,7 @@ def test_ssr( webapp, webdriver ): val = "\n".join( "(*) [{}]".format(e) for e in expected ) if width: val += "\nwidth = [{}]".format( width ) - assert html.unescape( adjust_html( get_clipboard() ) ) == val + wait_for_clipboard( 2, val, transform=lambda v: html.unescape(adjust_html(v)) ) # add an SSR and generate the SSR snippet add_ssr( "This is my first SSR." ) diff --git a/vasl_templates/webapp/tests/test_template_packs.py b/vasl_templates/webapp/tests/test_template_packs.py index 9d1a402..d92bd04 100644 --- a/vasl_templates/webapp/tests/test_template_packs.py +++ b/vasl_templates/webapp/tests/test_template_packs.py @@ -9,7 +9,7 @@ from selenium.webdriver.support.ui import Select from vasl_templates.webapp import snippets from vasl_templates.webapp.tests.utils import \ - select_tab, select_menu_option, get_clipboard, get_stored_msg, set_stored_msg, set_stored_msg_marker,\ + select_tab, select_menu_option, wait_for_clipboard, get_stored_msg, set_stored_msg, set_stored_msg_marker,\ add_simple_note, for_each_template, find_child, find_children, wait_for, \ select_droplist_val, get_droplist_vals_index, init_webapp @@ -24,14 +24,11 @@ def test_individual_files( webapp, webdriver ): # try uploading a customized version of each template def test_template( template_id, orig_template_id ): """Test uploading a customized version of the template.""" - # make sure generating a snippet returns something - elem, clipboard = _generate_snippet( template_id, orig_template_id ) - assert clipboard != "" # upload a new template _ = _upload_template_pack_file( template_id+".j2", "UPLOADED TEMPLATE", False ) # make sure generating a snippet returns the new version - elem.click() - assert get_clipboard() == "UPLOADED TEMPLATE" + _ = _generate_snippet( template_id, orig_template_id ) + wait_for_clipboard( 2, "UPLOADED TEMPLATE" ) for_each_template( test_template ) # try uploading a template with an incorrect filename extension @@ -159,8 +156,8 @@ def _check_snippets( expected ): def test_template( template_id, orig_template_id ): """Test each template.""" - _, clipboard = _generate_snippet( template_id, orig_template_id ) - assert clipboard == expected( template_id ) + _ = _generate_snippet( template_id, orig_template_id ) + wait_for_clipboard( 2, expected(template_id) ) for_each_template( test_template ) # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -185,7 +182,7 @@ def _generate_snippet( template_id, orig_template_id ): # generate a snippet for the specified template elem = find_child( "button.generate[data-id='{}']".format( orig_template_id ) ) elem.click() - return elem, get_clipboard() + return elem # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/vasl_templates/webapp/tests/test_vehicles_ordnance.py b/vasl_templates/webapp/tests/test_vehicles_ordnance.py index 942b0d3..73b7294 100644 --- a/vasl_templates/webapp/tests/test_vehicles_ordnance.py +++ b/vasl_templates/webapp/tests/test_vehicles_ordnance.py @@ -7,7 +7,7 @@ from selenium.webdriver.common.action_chains import ActionChains from vasl_templates.webapp.tests.utils import \ init_webapp, select_tab, set_template_params, find_child, find_children, \ - get_clipboard, click_dialog_button + wait_for_clipboard, click_dialog_button # --------------------------------------------------------------------- @@ -66,17 +66,17 @@ def test_crud( webapp, webdriver ): select_tab( "ob{}".format( player_no ) ) btn = find_child( "button[data-id='ob_{}_{}']".format( vo_type, player_no ) ) btn.click() - buf = get_clipboard() - names = [ - mo.group(1) - for mo in re.finditer( r"^\[\*\] (.*):" , buf, re.MULTILINE ) - ] - assert names == _expected[ (vo_type,player_no) ] + def reformat( clipboard ): #pylint: disable=missing-docstring + return [ + mo.group(1) + for mo in re.finditer( r"^\[\*\] (.*):" , clipboard, re.MULTILINE ) + ] + clipboard = wait_for_clipboard( 2, _expected[(vo_type,player_no)], transform=reformat ) # check the snippet width expected = _width[ (vo_type,player_no) ] mo = re.search( r"width={}$".format( expected if expected else "" ), - buf, + clipboard, re.MULTILINE ) assert mo @@ -144,7 +144,7 @@ def test_snippets( webapp, webdriver ): ] if vo_type == "vehicles": expected.insert( 3, "- CS 5" ) - assert get_clipboard() == "\n".join(expected) + wait_for_clipboard( 2, "\n".join(expected) ) delete_vo( vo_type, 1, "a german {}".format(vo_type0), webdriver ) # test a partial example @@ -159,16 +159,17 @@ def test_snippets( webapp, webdriver ): ] if vo_type == "vehicles": expected.insert( 2, '- cs 4 (brew up)' ) - assert get_clipboard() == "\n".join(expected) + wait_for_clipboard( 2, "\n".join(expected) ) delete_vo( vo_type, 1, "another german {}".format(vo_type0), webdriver ) # test a minimal example add_vo( vo_type, 1, "name only" ) btn = find_child( "button[data-id='ob_{}_1']".format( vo_type ) ) btn.click() - assert get_clipboard() == \ + wait_for_clipboard( 2, \ '''[German] ; width= [*] name only: #=''' + ) # do the test do_test( "vehicles" ) @@ -193,9 +194,10 @@ def test_variable_capabilities( webapp, webdriver ): set_template_params( { "SCENARIO_DATE": "{:02d}/01/{}".format(month,year) } ) select_tab( "ob2" ) vehicles2.click() - buf = get_clipboard() - mo = re.search( r"^- capabilities: (.*)$", buf, re.MULTILINE ) - assert mo.group(1) == expected + def reformat( clipboard ): #pylint: disable=missing-docstring + mo = re.search( r"^- capabilities: (.*)$", clipboard, re.MULTILINE ) + return mo.group( 1 ) + wait_for_clipboard( 2, expected, transform=reformat ) do_test( 1, 1940, '"sM8\u2020"' ) do_test( 1, 1943, '"sM8\u2020"' ) do_test( 2, 1943, '"HE7" "sM8\u2020"' ) diff --git a/vasl_templates/webapp/tests/utils.py b/vasl_templates/webapp/tests/utils.py index 01edea6..96e7d11 100644 --- a/vasl_templates/webapp/tests/utils.py +++ b/vasl_templates/webapp/tests/utils.py @@ -250,7 +250,7 @@ def generate_sortable_entry_snippet( sortable, entry_no ): """Generate the snippet for a sortable entry.""" elems = find_children( "li img.snippet", sortable ) elems[entry_no].click() - return get_clipboard() + return _get_clipboard() def drag_sortable_entry_to_trash( sortable, entry_no ): """Draw a sortable entry to the trash.""" @@ -374,8 +374,13 @@ def click_dialog_button( caption ): _pyqt_app = None -def get_clipboard() : - """Get the contents of the clipboard.""" +def _get_clipboard() : + """Get the contents of the clipboard. + + NOTE: This used to be public, but there is sometimes a delay between doing something + in the UI (e.g. clicking a button) and the result appearing in the clipboard, so tests + should use wait_for_clipboard() instead. + """ if pytest.config.option.use_clipboard: #pylint: disable=no-member global _pyqt_app if _pyqt_app is None: @@ -405,14 +410,18 @@ def wait_for_elem( timeout, elem_id, parent=None ): wait_for( timeout, check_elem ) return args["elem"] -def wait_for_clipboard( timeout, expected, contains=False ): +def wait_for_clipboard( timeout, expected, contains=False, transform=None ): """Wait for the clipboard to hold an expected value.""" args = { "last-clipboard": "" } def check_clipboard(): #pylint: disable=missing-docstring - args["last-clipboard"] = get_clipboard() - return expected in args["last-clipboard"] if contains else expected == args["last-clipboard"] + clipboard = _get_clipboard() + args["last-clipboard"] = clipboard + if transform: + clipboard = transform( clipboard ) + return expected in clipboard if contains else expected == clipboard try: wait_for( timeout, check_clipboard ) + return args["last-clipboard"] except AssertionError: print( "Timed out waiting for the clipboard:" ) print( "- Expecting:", expected )