pytest-dev / pytest-selenium

Plugin for running Selenium with pytest
Other
333 stars 120 forks source link

`Screenshot` doesn't match with `HTML` #327

Closed changchiyou closed 7 months ago

changchiyou commented 7 months ago

I except that Screenshot will display the innerText of recognized-texts in HTML:

<p id="recognized-texts">2024-02-18 00:17:20 [00059baiden]<br>2024-02-18 00:18:22 [005657obama]<br></p>

However, it is not behaving as expected:

Screenshot HTML
drawing drawing

Should I manually insert a time.sleep() in the code section found at

https://github.com/pytest-dev/pytest-selenium/blob/c5be64bc8fffef5f4639a375619b614472f561ab/src/pytest_selenium/pytest_selenium.py#L297-L306

, or is there any existing argument that I may have overlooked?

changchiyou commented 7 months ago

After I manually insert a time.sleep() and rerun the pytest, it works.

Edit Result
drawing drawing
changchiyou commented 7 months ago

NVM, adding time.sleep() right after selenium.get() can easily solve the problem:

@pytest.mark.webtest
@pytest.mark.usefixtures("live_server")
@pytest.mark.parametrize(*p_args(args={"RECOGNIZE": True, "TEMP_FEATURES": True}), **p_kwargs()) # type: ignore
def test_RealtimeRecognition_create_flask(create_tmp_folders_files, ini_config_setting_v2, selenium):
    selenium.get(url_for("index", _external=True))
    sleep(1) # here
RonnyPfannschmidt commented 7 months ago

the correct way is to wait for expected elements to appear, random sleeps are fragile

RonnyPfannschmidt commented 7 months ago

(in an more complex scenario dynamic element appearance is potentially load/performance dependent, random sleeps entually fail unless you make them painfully long)

changchiyou commented 7 months ago

According to https://github.com/pytest-dev/pytest-selenium/issues/327#issuecomment-1951046257, I replace time.sleep(1) with:

recognized_texts = [
  "2024-02-18 00:17:20 [00059]",
  "2024-02-18 00:18:22 [05657]",
],
    while sum([1 for recognized_text in recognized_texts if recognized_text in selenium.page_source]) \
          != len(recognized_texts):
        ... # complete full logics here or use for loop instead for time limitation
RonnyPfannschmidt commented 7 months ago

Use all instead of sum

changchiyou commented 7 months ago

According to https://github.com/pytest-dev/pytest-selenium/issues/327#issuecomment-1951317590, I adjust the code from:

    while sum([1 for recognized_text in recognized_texts if recognized_text in selenium.page_source]) \
          != len(recognized_texts):
        ... # complete full logics here or use for loop instead for time limitation

to:

    while not all([recognized_text in selenium.page_source for recognized_text in recognized_texts]):
        ... # complete full logics here or use for loop instead for time limitation