Closed changchiyou closed 7 months ago
After I manually insert a time.sleep()
and rerun the pytest, it works.
Edit | Result |
---|---|
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
the correct way is to wait for expected elements to appear, random sleeps are fragile
(in an more complex scenario dynamic element appearance is potentially load/performance dependent, random sleeps entually fail unless you make them painfully long)
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
Use all instead of sum
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
I except that
Screenshot
will display the innerText ofrecognized-texts
inHTML
:However, it is not behaving as expected:
Should I manually insert a
time.sleep()
in the code section found athttps://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?