コード例 #1
0
def _generate_snippet(webdriver, template_id, orig_template_id):
    """Generate a snippet for the specified template."""

    if template_id == "scenario_note":
        # create a scenario note and generate a snippet for it
        sortable = find_child("#scenario_notes-sortable")
        add_simple_note(sortable, "test scenario note", None)
        elems = find_children("li img.snippet", sortable)
        elem = elems[0]
    elif template_id in ("ob_setup", "ob_note"):
        # create a OB setup/note and generate a snippet for it
        select_tab("ob1")
        sortable = find_child("#{}s-sortable_1".format(template_id))
        add_simple_note(sortable, "test {}".format(template_id), None)
        elems = find_children(
            "#{}s-sortable_1 li img.snippet".format(template_id))
        elem = elems[0]
    elif template_id in ("ob_vehicle_note", "ob_ordnance_note"):
        # create a vehicle/ordnance and generate a snippet for its note
        mo = re.search(r"^ob_([a-z]+)_note_(\d)$", orig_template_id)
        vo_type, player_no = mo.group(1), int(mo.group(2))
        vo_type0 = "vehicles" if vo_type == "vehicle" else vo_type
        player_nat = "german" if player_no == 1 else "russian"
        sortable = find_child("#ob_{}-sortable_{}".format(vo_type0, player_no))
        add_vo(webdriver, vo_type0, player_no,
               "a {} {}".format(player_nat, vo_type))
        elems = find_children("li img.snippet", sortable)
        elem = elems[0]
    else:
        # generate a snippet for the specified template
        elem = find_child(
            "button.generate[data-id='{}']".format(orig_template_id))
    elem.click()
    return elem
コード例 #2
0
 def change_field(param):
     """Make a change to a field."""
     # make a change to the specified field
     if param in SIMPLE_NOTES:
         target = find_child(SIMPLE_NOTES[param])
         add_simple_note(target, "changed value", None)
         return target
     if param in VEHICLE_ORDNANCE:
         info = VEHICLE_ORDNANCE[param]
         target = find_child(info[0])
         mo = re.search(r"([a-z]+)-", info[0])
         add_vo(webdriver, mo.group(1), info[1], info[2])
         return target
     target = next(e for e in [
         find_child("{}[name='{}']".format(ctype, param))
         for ctype in ["input", "select", "textarea"]
     ] if e)
     if target.tag_name in ("input", "textarea"):
         prev_val = target.get_attribute("value")
         new_val = "01/01/2000" if param == "SCENARIO_DATE" else "changed value"
         if target.is_displayed():
             target.clear()
             target.send_keys(new_val)
         else:
             webdriver.execute_script("arguments[0].value = arguments[1]",
                                      target, new_val)
         return target, prev_val, new_val
     elif target.tag_name == "select":
         sel = Select(target)
         prev_val = sel.first_selected_option.get_attribute("value")
         select_droplist_index(sel, 2)
         new_val = sel.first_selected_option.get_attribute("value")
         return target, prev_val, new_val
     assert False
     return None
コード例 #3
0
 def do_test(vehicles, expected):  #pylint: disable=missing-docstring
     # add the specified vehicles
     new_scenario()
     set_player(1, "japanese")
     for veh in vehicles:
         add_vo(webdriver, "vehicles", 1, veh)
     # get the multi-applicable notes
     btn = find_child("button.generate[data-id='ob_vehicles_ma_notes_1']")
     btn.click()
     wait_for_clipboard(2, expected, transform=_extract_extn_ma_notes)
コード例 #4
0
def test_include_vasl_images_in_snippets(webapp, webdriver):
    """Test including VASL counter images in snippets."""

    # initialize
    webapp.control_tests.set_data_dir("{REAL}")
    init_webapp(webapp, webdriver)
    set_user_settings(
        {"scenario-images-source": SCENARIO_IMAGES_SOURCE_THIS_PROGRAM})

    # add a vehicle
    set_player(1, "german")
    add_vo(webdriver, "vehicles", 1, "PzKpfw IB")

    # enable "show VASL images in snippets"
    select_menu_option("user_settings")
    elem = find_child(
        ".ui-dialog.user-settings input[name='include-vasl-images-in-snippets']"
    )
    assert not elem.is_selected()
    elem.click()
    click_dialog_button("OK")
    _check_cookies(webdriver, "include-vasl-images-in-snippets", True)

    # make sure that it took effect
    snippet_btn = find_child("button[data-id='ob_vehicles_1']")
    snippet_btn.click()
    wait_for_clipboard(2, "/counter/2524/front", contains=True)

    # disable "show VASL images in snippets"
    select_menu_option("user_settings")
    elem = find_child(
        ".ui-dialog.user-settings input[name='include-vasl-images-in-snippets']"
    )
    assert elem.is_selected()
    elem.click()
    click_dialog_button("OK")
    _check_cookies(webdriver, "include-vasl-images-in-snippets", False)

    # make sure that it took effect
    snippet_btn.click()
    wait_for_clipboard(2, "/counter/2524/front", contains=False)
コード例 #5
0
    def check_counter_images(veh_name, expected):
        """Check the counter images available for the specified vehicle."""

        # add the specified vehicle
        add_vo(webdriver, "vehicles", 2, veh_name)

        # edit the vehicle
        vehicles_sortable = find_child("#ob_vehicles-sortable_2")
        elems = find_children("li", vehicles_sortable)
        ActionChains(webdriver).double_click(elems[-1]).perform()
        dlg = find_child(".ui-dialog.edit-vo")

        # check the currently-selected counter
        image_url = find_child("img.vasl-image", dlg).get_attribute("src")
        if expected:
            assert image_url.endswith("/counter/{}/front".format(expected[0]))
        else:
            assert image_url.endswith("/missing-image.png")

        # check the available counters
        btn = find_child("input.select-vo-image", dlg)
        if expected and len(expected) > 1:
            btn.click()
            dlg2 = find_child(".ui-dialog.select-vo-image")
            image_urls = [
                elem.get_attribute("src")
                for elem in find_children(".vo-images img", dlg2)
            ]
            assert len(image_urls) == len(expected)
            for image_url, piece_id in zip(image_urls, expected):
                assert image_url.endswith(
                    "/counter/{}/front/0".format(piece_id))
            dlg2.send_keys(Keys.ESCAPE)
        else:
            assert btn is None
        dlg.send_keys(Keys.ESCAPE)
コード例 #6
0
def test_custom_comments( webapp, webdriver ): #pylint: disable=too-many-statements
    """Test custom comments."""

    # NOTE: Vehicle/ordnance comments are not capabilities, but they are managed in the same place
    # and the code is virtually identical, so it makes sense to put the test code here.

    # initialize
    init_webapp( webapp, webdriver, scenario_persistence=1 )

    # add a vehicle
    add_vo( webdriver, "vehicles", 1, "a commented german vehicle" )

    snippet_btn = find_child( "button[data-id='ob_vehicles_1']" )
    def extract_comments( clipboard ):
        """Extract the comments."""
        mo = re.search( r"^- comments: (.*)$", clipboard, re.MULTILINE )
        return mo.group(1) if mo else ""
    def check_snippet( expected ):
        """Check the vehicle's snippet."""
        snippet_btn.click()
        wait_for_clipboard( 2, expected, transform=extract_comments )
    def check_comments_in_dialog( expected ):
        """Check the vehicle's comments."""
        elems = find_children( "#vo_comments-sortable li" )
        elems2 = [ find_child("input[type='text']",c) for c in elems ]
        assert [ e.get_attribute("value") for e in elems2 ] == expected
        return elems

    # check the vehicle's snippet
    check_snippet( '"a comment" "another comment"' )

    # edit the vehicle's comments
    vehicles_sortable = find_child( "#ob_vehicles-sortable_1" )
    elems = find_children( "li", vehicles_sortable )
    assert len(elems) == 1
    ActionChains( webdriver ).double_click( elems[0] ).perform()
    elems = check_comments_in_dialog( [ "a comment", "another comment" ] )

    # edit one of the comments
    elem = find_child( "input[type='text']", elems[0] )
    elem.clear()
    elem.send_keys( "a comment (modified)" )

    # delete a comment
    ActionChains( webdriver ).key_down( Keys.CONTROL ).click( elems[1] ).perform()
    ActionChains( webdriver ).key_up( Keys.CONTROL ).perform()

    # add a new comment
    elem = find_child( "#vo_comments-add" )
    elem.click()
    elems = find_children( "#vo_comments-sortable input[type='text']" )
    assert len(elems) == 2
    elems[1].send_keys( "a <i>new</i> comment" )

    # save the changes and check the vehicle's snippet
    click_dialog_button( "OK" )
    check_snippet( '"a comment (modified)" "a <i>new</i> comment"' )

    # save the scenario
    saved_scenario = save_scenario()
    assert len(saved_scenario["OB_VEHICLES_1"]) == 1
    assert saved_scenario["OB_VEHICLES_1"][0]["custom_comments"] == [ "a comment (modified)", "a <i>new</i> comment" ]

    # reload the scenario, and check the vehicle's snippet
    select_menu_option( "new_scenario" )
    load_scenario( saved_scenario )
    select_tab( "ob1" )
    check_snippet( '"a comment (modified)" "a <i>new</i> comment"' )

    # make sure the comments are loaded correcly when editing the vehicle
    elems = find_children( "li", vehicles_sortable )
    assert len(elems) == 1
    ActionChains( webdriver ).double_click( elems[0] ).perform()
    elems = check_comments_in_dialog( [ "a comment (modified)", "a <i>new</i> comment" ] )

    # delete all comments
    for elem in elems:
        ActionChains( webdriver ).key_down( Keys.CONTROL ).click( elem ).perform()
        ActionChains( webdriver ).key_up( Keys.CONTROL ).perform()
    click_dialog_button( "OK" )
    check_snippet( "" )

    # save the scenario
    saved_scenario2 = save_scenario()
    assert len(saved_scenario2["OB_VEHICLES_1"]) == 1
    assert saved_scenario2["OB_VEHICLES_1"][0]["custom_comments"] == []

    # reload the scenario, and reset the vehicle's comments back to the default
    load_scenario( saved_scenario )
    select_tab( "ob1" )
    elems = find_children( "li", vehicles_sortable )
    assert len(elems) == 1
    ActionChains( webdriver ).double_click( elems[0] ).perform()
    btn = find_child( "#vo_comments-reset" )
    btn.click()
    click_dialog_button( "OK" )
    check_snippet( '"a comment" "another comment"' )

    # make sure the custom comments are no longer saved in the scenario
    saved_scenario2 = save_scenario()
    assert len(saved_scenario2["OB_VEHICLES_1"]) == 1
    assert "custom_comments" not in saved_scenario2["OB_VEHICLES_1"][0]

    # reload the scenario, and manually set the vehicle's comments to be the same as the default
    load_scenario( saved_scenario )
    select_tab( "ob1" )
    elems = find_children( "li", vehicles_sortable )
    assert len(elems) == 1
    ActionChains( webdriver ).double_click( elems[0] ).perform()
    elems = find_children( "#vo_comments-sortable input[type='text']" )
    assert len(elems) == 2
    elems[0].clear()
    elems[0].send_keys( "a comment" )
    elems[1].clear()
    elems[1].send_keys( "another comment" )
    click_dialog_button( "OK" )

    # make sure the custom comments are no longer saved in the scenario
    saved_scenario = save_scenario()
    assert len(saved_scenario["OB_VEHICLES_1"]) == 1
    assert "custom_comments" not in saved_scenario["OB_VEHICLES_1"][0]
コード例 #7
0
def set_template_params(params):  #pylint: disable=too-many-branches
    """Set template parameters."""
    def add_sortable_entries(sortable, entries):
        """Add simple notes to a sortable."""
        for entry in entries:
            add_simple_note(sortable, entry.get("caption", ""),
                            entry.get("width", ""))

    for key, val in params.items():

        # check for scenario notes (these require special handling)
        if key == "SCENARIO_NOTES":
            # add them in (nb: we don't consider any existing scenario notes)
            add_sortable_entries(find_child("#scenario_notes-sortable"), val)
            continue

        # check for SSR's (these require special handling)
        if key == "SSR":
            # add them in (nb: we don't consider any existing SSR's)
            sortable = find_child("#ssr-sortable")
            for ssr in val:
                add_simple_note(sortable, ssr, None)
            continue

        # check for OB setups/notes (these require special handling)
        if key in ("OB_SETUPS_1", "OB_SETUPS_2", "OB_NOTES_1", "OB_NOTES_2"):
            # add them in (nb: we don't consider any existing OB setup/note's)
            mo = re.search(r"^(.*)_(\d)$", key)
            sortable = find_child("#{}-sortable_{}".format(
                mo.group(1).lower(), mo.group(2)))
            add_sortable_entries(sortable, val)
            continue

        # check for vehicles/ordnance (these require special handling)
        if key in ("OB_VEHICLES_1", "OB_ORDNANCE_1", "OB_VEHICLES_2",
                   "OB_ORDNANCE_2"):
            # add them in (nb: we don't consider any existing vehicles/ordnance)
            from vasl_templates.webapp.tests.test_vehicles_ordnance import add_vo  #pylint: disable=cyclic-import
            mo = re.search(r"^OB_(VEHICLES|ORDNANCE)_\d$", key)
            vo_type = mo.group(1).lower()
            for vo_name in val:
                add_vo(_webdriver, vo_type, int(key[-1]), vo_name)
            continue

        # locate the next parameter
        elem = next( c for c in ( \
            find_child( "{}[name='{}']".format(elem_type,key) ) \
            for elem_type in ["input","textarea","select"]
        ) if c )

        # set the parameter value
        if elem.tag_name == "select":
            select_droplist_val(Select(elem), val)
        else:
            if elem.is_displayed():
                elem.clear()
                if val:
                    elem.send_keys(val)
                    if key == "SCENARIO_DATE":
                        elem.send_keys(
                            Keys.TAB
                        )  # nb: force the calendar popup to close :-/
                        wait_for(
                            5, lambda: find_child("#ui-datepicker-div").
                            value_of_css_property("display") == "none")
                        time.sleep(0.25)
            else:
                # FUDGE! Selenium can't interact with hidden elements, so we do it like this.
                # However, we don't do this for everything since it doesn't always triggers events.
                _webdriver.execute_script("arguments[0].value = arguments[1]",
                                          elem, val)
コード例 #8
0
def test_seq_ids(webapp, webdriver):
    """Test handling of vehicle/ordnance sequence ID's."""

    # initialize
    init_webapp(webapp, webdriver, scenario_persistence=1)

    # load the test scenario
    load_scenario({
        "PLAYER_1":
        "german",
        "OB_VEHICLES_1": [{
            "name": "a german vehicle"
        }, {
            "name": "another german vehicle"
        }]
    })
    select_tab("ob1")
    sortable = find_child("#ob_vehicles-sortable_1")

    def check_seq_ids(expected):  #pylint: disable=missing-docstring
        entries = find_children("li", sortable)
        assert len(entries) == len(expected)
        for i, entry in enumerate(entries):
            data = webdriver.execute_script(
                "return $(arguments[0]).data('sortable2-data')", entry)
            assert expected[i] == (data["caption"], data["id"])

    # check the initial seq ID's (nb: they weren't in the loaded scenario, so they should have been auto-assigned)
    check_seq_ids([
        ("a german vehicle", 1),
        ("another german vehicle", 2),
    ])

    # add another vehicle
    add_vo(webdriver, "vehicles", 1, "one more german vehicle")
    check_seq_ids([
        ("a german vehicle", 1),
        ("another german vehicle", 2),
        ("one more german vehicle", 3),
    ])

    # delete the 2nd vehicle
    delete_vo("vehicles", 1, "another german vehicle", webdriver)
    check_seq_ids([
        ("a german vehicle", 1),
        ("one more german vehicle", 3),
    ])

    # add another vehicle
    add_vo(webdriver, "vehicles", 1, "name only")
    check_seq_ids([
        ("a german vehicle", 1),
        ("one more german vehicle", 3),
        ("name only", 2),  # nb: this seq ID gets re-used
    ])

    # make sure the seq ID's are saved out
    saved_scenario = save_scenario()
    entries = [(veh["name"], veh.get("seq_id"))
               for veh in saved_scenario["OB_VEHICLES_1"]]
    assert entries == [
        ("a german vehicle", 1),
        ("one more german vehicle", 3),
        ("name only", 2),
    ]