zauberzeug / nicegui

Create web-based user interfaces with Python. The nice way.
https://nicegui.io
MIT License
10.18k stars 606 forks source link

Testing: Recognition of Autocomplete Input #3886

Open GrazingScientist opened 1 month ago

GrazingScientist commented 1 month ago

Description

Hi!

I found another non-working feature in the testing framework, when I want to test that the autocomplete is working.

The test below is failing, although it should not.

from nicegui import ui

def setup() -> None:
    @ui.page("/")
    def main() -> None:
        options = ['AutoComplete', 'NiceGUI', 'Awesome']
        ui.input(label='Text', placeholder='start typing', autocomplete=options)

async def test_validation(user):
    setup()

    await user.open("/")

    await user.should_not_see("AutoComplete")
    user.find("Text").type("Auto")
    await user.should_see("AutoComplete")

I hope, you can fix it. ❤️

rodja commented 1 month ago

Thanks for bringing up this example @GrazingScientist. I think it is a little bit more complicated. If you start your page with NiceGUI and type "Auto" in the browser, you must also press the "TAB" key to confirm the auto completion. So we would need to use user.find("Text").type("Auto").trigger("keydown.tab").

GrazingScientist commented 1 month ago

Hi @rodja ,

thanks for coming back so quickly. I eagerly tried your tip, but the result is the same. The test fails with:

AssertionError: expected to see at least one element with marker=AutoComplete or content=AutoComplete on the page

This is the code I used:

from nicegui import ui

def setup() -> None:
    @ui.page("/")
    def main() -> None:
        options = ["AutoComplete", "NiceGUI", "Awesome"]
        ui.input(label="Text", placeholder="start typing", autocomplete=options)

async def test_validation(user):
    setup()

    await user.open("/")

    await user.should_not_see("AutoComplete")
    user.find("Text").type("Auto").trigger("keydown.tab")
    await user.should_see("AutoComplete")
rodja commented 1 month ago

I'm sorry. This is not implemented yet. I just wanted to clarify the API. We are pretty busy at the moment. Would you like to try creating a pull request.

GrazingScientist commented 1 month ago

I will try to dig into the code, if I have the time. Can you provide me a link to the code section, where I need to focus on?

rodja commented 1 month ago

I think you could try adding a special case for ui.input in the trigger loop over all elements:

https://github.com/zauberzeug/nicegui/blob/809290e3037a837fd231330c096da6409143f6ab/nicegui/testing/user_interaction.py#L38

Similar to what we do for ui.link and ui.select in the click method:

https://github.com/zauberzeug/nicegui/blob/809290e3037a837fd231330c096da6409143f6ab/nicegui/testing/user_interaction.py#L63C17-L70C55

An workaround until we get this done might be to just get the element and check for the possible autocompletes by accessing the .props dict.