Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 opened this issue Jun 8, 2023 · 1 comment

Comments

@kaiguy23
Copy link

kaiguy23 commented Jun 8, 2023

          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 #35 (comment)

@kaiguy23 kaiguy23 closed this as completed Jun 8, 2023
@Joonalai
Copy link
Contributor

Joonalai commented Jun 8, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants