From 246ca572957039f900cdeef1a014d3af86e258ab Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Mon, 15 Sep 2025 13:35:26 +0530 Subject: [PATCH 1/2] add test for `downloadEnd` event --- .../common/bidi_browsing_context_tests.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/py/test/selenium/webdriver/common/bidi_browsing_context_tests.py b/py/test/selenium/webdriver/common/bidi_browsing_context_tests.py index 254d2aace3c08..d0c7e3613ced3 100644 --- a/py/test/selenium/webdriver/common/bidi_browsing_context_tests.py +++ b/py/test/selenium/webdriver/common/bidi_browsing_context_tests.py @@ -807,6 +807,41 @@ def on_download_will_begin(info): driver.browsing_context.remove_event_handler("download_will_begin", callback_id) +@pytest.mark.xfail_firefox +def test_add_event_handler_download_end(driver, pages): + """Test adding event handler for download_end event.""" + events_received = [] + + def on_download_end(info): + events_received.append(info) + + callback_id = driver.browsing_context.add_event_handler("download_end", on_download_end) + assert callback_id is not None + + context_id = driver.current_window_handle + url = pages.url("downloads/download.html") + driver.browsing_context.navigate(context=context_id, url=url, wait=ReadinessState.COMPLETE) + + driver.find_element(By.ID, "file-1").click() + + driver.find_element(By.ID, "file-2").click() + WebDriverWait(driver, 5).until(lambda d: len(events_received) > 1) + + assert len(events_received) == 2 + + download_event = events_received[0] + assert download_event.download_params is not None + assert download_event.download_params.status == "complete" + assert download_event.download_params.context == context_id + assert download_event.download_params.timestamp is not None + assert "downloads/file_1.txt" in download_event.download_params.url + # we assert that atleast the str "file_1" is present in the downloaded file since multiple downloads + # will have numbered suffix like file_1 (1) + assert "file_1" in download_event.download_params.filepath + + driver.browsing_context.remove_event_handler("download_end", callback_id) + + def test_add_event_handler_with_specific_contexts(driver): """Test adding event handler with specific browsing contexts.""" events_received = [] From 9c0c5d35472cc6306e63bf698a6a7c6bc2a1c425 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Mon, 15 Sep 2025 19:49:14 +0530 Subject: [PATCH 2/2] fix in case of event ordering --- .../common/bidi_browsing_context_tests.py | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/py/test/selenium/webdriver/common/bidi_browsing_context_tests.py b/py/test/selenium/webdriver/common/bidi_browsing_context_tests.py index d0c7e3613ced3..87850a4de29e5 100644 --- a/py/test/selenium/webdriver/common/bidi_browsing_context_tests.py +++ b/py/test/selenium/webdriver/common/bidi_browsing_context_tests.py @@ -825,19 +825,27 @@ def on_download_end(info): driver.find_element(By.ID, "file-1").click() driver.find_element(By.ID, "file-2").click() - WebDriverWait(driver, 5).until(lambda d: len(events_received) > 1) + WebDriverWait(driver, 5).until(lambda d: len(events_received) == 2) assert len(events_received) == 2 - download_event = events_received[0] - assert download_event.download_params is not None - assert download_event.download_params.status == "complete" - assert download_event.download_params.context == context_id - assert download_event.download_params.timestamp is not None - assert "downloads/file_1.txt" in download_event.download_params.url - # we assert that atleast the str "file_1" is present in the downloaded file since multiple downloads + for ev in events_received: + assert ev.download_params is not None + assert ev.download_params.status == "complete" + assert ev.download_params.context == context_id + assert ev.download_params.timestamp is not None + + # we assert that atleast "file_1" is present in the downloaded file since multiple downloads # will have numbered suffix like file_1 (1) - assert "file_1" in download_event.download_params.filepath + assert any( + "downloads/file_1.txt" in ev.download_params.url and "file_1" in ev.download_params.filepath + for ev in events_received + ) + + assert any( + "downloads/file_2.jpg" in ev.download_params.url and "file_2" in ev.download_params.filepath + for ev in events_received + ) driver.browsing_context.remove_event_handler("download_end", callback_id)