GispoCoding / pytest-qgis

A pytest plugin for testing QGIS python plugins
GNU General Public License v2.0
29 stars 8 forks source link

On another note, I was recently using the pytest-qt module to simulate mouse clicks on a QComboBox in my plugin. I was trying to use their qtbot’s keyClicks to try and change the combo box text. #36

Closed kaiguy23 closed 1 year ago

kaiguy23 commented 1 year ago
          On another note, I was recently using the pytest-qt module to simulate mouse clicks on a QComboBox in my plugin. I was trying to use their qtbot’s keyClicks to try and change the combo box text.

For some reason, this seems to work 50% of the time. I have 4 items in my combo box (aspen, bot, bot_train, bot_test) and for some reason the combo box text changes for aspen and bot but not for bot_train and bot_test. I have never seen the combo box fail to switch when I use the plugin myself; this seems to only occur in the test environment.

I saw that you guys have a qgis_bot fixture, but it doesn’t seem to have this keyClicks attribute. Should I be using this module instead, and if so, how should I be using it?

Any advice would be appreciated! This might be a pytest-qt problem, but I figured I should ask here since I am testing a qgis plugin. Thanks!

Originally posted by @kaiguy23 in https://github.com/GispoCoding/pytest-qgis/issues/35#issuecomment-1582007224

Joonalai commented 1 year ago

For some reason, this seems to work 50% of the time. I have 4 items in my combo box (aspen, bot, bot_train, bot_test) and for some reason the combo box text changes for aspen and bot but not for bot_train and bot_test. I have never seen the combo box fail to switch when I use the plugin myself; this seems to only occur in the test environment.

At least in pytest-qt 3.3.0 choosing items from combo box is not trivial with qtbot if all the starting letters of the items are not unique.

In one project we have this kind of (hacky) helper function to choose item in combo box:

def set_combo_box_value(
    qtbot: QtBot, c_box: QComboBox, value: str, strip_values: bool = True, occurence_index: int = 0
) -> None:
    """Set combo box value to be the exact string. This handles also the null values"""
    c_box_values = [
        c_box.itemText(i).strip() if strip_values else c_box.itemText(i)
        for i in range(c_box.count())
    ]
    unique_staring_letters = {
        c_box_value[0] if c_box_value else "" for c_box_value in c_box_values
    }
    if value and len(unique_staring_letters) == len(c_box_values):
        # By typing the first letter, it changes
        qtbot.keyClicks(c_box, value[0])
    elif value in c_box_values:
        indexes = [
            i for i, c_box_value in enumerate(c_box_values) if c_box_value == value
        ]
        c_box.setCurrentIndex(indexes[occurence_index])
    else:
        # Map layer combo boxes add projection info
        closest_values = [
            c_box_value for c_box_value in c_box_values if c_box_value.startswith(value)
        ]
        if not closest_values:
            raise ValueError(
                f"Combobox {c_box} does not contain value {value}. It contains values: {closest_values}"
            )
        c_box.setCurrentText(closest_values[0])
    assert c_box.currentText().startswith(value)

I saw that you guys have a qgis_bot fixture, but it doesn’t seem to have this keyClicks attribute. Should I be using this module instead, and if so, how should I be using it?

The naming of the fixture has been inspired by pytest-qt's qtbot, but it has no intention to replace it. In fact I use pytest-qt and qtbot in most of the QGIS plugin projects in addition to pytest-qgis.