Skip to content

Commit 5f23fc6

Browse files
committed
Add refactored test 1756722
1 parent 9b7ee07 commit 5f23fc6

File tree

3 files changed

+63
-60
lines changed

3 files changed

+63
-60
lines changed

modules/browser_object_navigation.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import logging
2+
import re
23
from typing import Literal
34

4-
from selenium.common.exceptions import TimeoutException
5+
from selenium.common.exceptions import StaleElementReferenceException, TimeoutException
56
from selenium.webdriver import ActionChains, Firefox
67
from selenium.webdriver.common.by import By
78
from selenium.webdriver.common.keys import Keys
@@ -290,27 +291,40 @@ def click_file_download_warning_panel(self) -> BasePage:
290291
self.click_on("file-download-warning-button")
291292
return self
292293

293-
def wait_for_item_to_download(self, filename: str) -> BasePage:
294+
@BasePage.context_chrome
295+
def wait_for_download_elements(self) -> BasePage:
294296
"""
295-
Check the downloads tool in the toolbar to wait for a given file to download
297+
Wait for download elements to be present.
296298
"""
297-
original_timeout = self.driver.timeouts.implicit_wait
298-
try:
299-
# Whatever our timeout, we want to lengthen it because downloads
300-
self.driver.implicitly_wait(original_timeout * 2)
301-
self.element_visible("downloads-item-by-file", labels=[filename])
302-
self.expect_not(
303-
EC.element_attribute_to_include(
304-
self.get_selector("downloads-button"), "animate"
305-
)
306-
)
307-
with self.driver.context(self.context_id):
308-
self.driver.execute_script(
309-
"arguments[0].setAttribute('hidden', true)",
310-
self.get_element("downloads-button"),
311-
)
312-
finally:
313-
self.driver.implicitly_wait(original_timeout)
299+
self.element_visible("download-target-element")
300+
return self
301+
302+
@BasePage.context_chrome
303+
def verify_download_name(self, expected_pattern: str) -> BasePage:
304+
"""
305+
Verify download name matches expected pattern.
306+
Argument:
307+
expected_pattern: Regex pattern to match against download name
308+
"""
309+
download_name = self.get_element("download-target-element")
310+
download_value = download_name.get_attribute("value")
311+
assert re.match(expected_pattern, download_value), (
312+
f"The download name is incorrect: {download_value}"
313+
)
314+
return self
315+
316+
@BasePage.context_chrome
317+
def wait_for_download_completion(self) -> BasePage:
318+
"""Wait until the most recent download reaches 100% progress."""
319+
320+
def _download_complete(_):
321+
try:
322+
element = self.get_element("download-progress-element")
323+
return element.get_attribute("value") == "100"
324+
except StaleElementReferenceException:
325+
return False
326+
327+
self.wait.until(_download_complete)
314328
return self
315329

316330
@BasePage.context_chrome

modules/data/navigation.components.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,24 @@
405405
"groups": []
406406
},
407407

408+
"download-target-element": {
409+
"selectorData": "downloadTarget",
410+
"strategy": "class",
411+
"groups": []
412+
},
413+
414+
"download-progress-element": {
415+
"selectorData": "downloadProgress",
416+
"strategy": "class",
417+
"groups": []
418+
},
419+
420+
"download-details-element": {
421+
"selectorData": "downloadDetailsNormal",
422+
"strategy": "class",
423+
"groups": []
424+
},
425+
408426
"bookmark-in-bar": {
409427
"selectorData": "toolbarbutton.bookmark-item",
410428
"strategy": "css",
Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import re
2-
from time import sleep
3-
41
import pytest
5-
from selenium.common.exceptions import StaleElementReferenceException
62
from selenium.webdriver import Firefox
7-
from selenium.webdriver.common.by import By
8-
from selenium.webdriver.support import expected_conditions as EC
9-
from selenium.webdriver.support.wait import WebDriverWait
103

4+
from modules.browser_object import Navigation
115
from modules.page_object import GenericPage
126

137

@@ -31,42 +25,19 @@ def test_mixed_content_download_via_https(driver: Firefox, delete_files):
3125
"""
3226
C1756722: Verify that the user can download mixed content via HTTPS
3327
"""
34-
28+
# Initialize objects
3529
web_page = GenericPage(driver, url=MIXED_CONTENT_DOWNLOAD_URL)
30+
nav = Navigation(driver)
3631

37-
# Wait up to 30 seconds for test website to wake up and download the content
32+
# Wait for the test website to wake up and download the content
3833
web_page.open()
39-
with driver.context(driver.CONTEXT_CHROME):
40-
WebDriverWait(driver, 30).until(EC.title_contains("File Examples"))
41-
42-
with driver.context(driver.CONTEXT_CHROME):
43-
download_name = WebDriverWait(driver, 10).until(
44-
EC.presence_of_element_located((By.CLASS_NAME, "downloadTarget"))
45-
)
46-
47-
download_status = WebDriverWait(driver, 10).until(
48-
EC.presence_of_element_located((By.CLASS_NAME, "downloadProgress"))
49-
)
34+
web_page.wait.until(lambda _: web_page.title_contains("File Examples"))
5035

51-
# Verify that the desired download target element is present directly, no extra steps needed.
52-
download_value = download_name.get_attribute("value")
53-
assert re.match(r"file-sample_100kB(\(\d+\)).odt$", download_value), (
54-
f"The download name is incorrect: {download_value}"
55-
)
36+
# Wait for download elements to appear
37+
nav.wait_for_download_elements()
5638

57-
# Verify that the download progress has reached 100%, which indicates that the download is complete.
58-
i = 1
59-
while True:
60-
try:
61-
download_value = download_status.get_attribute("value")
62-
if download_value == "100":
63-
break
64-
except StaleElementReferenceException:
65-
pass
39+
# Verify download name matches expected pattern
40+
nav.verify_download_name(r"file-sample_100kB(\(\d+\))?.odt$")
6641

67-
if i > MAX_CHECKS:
68-
raise TimeoutError(
69-
"Download progress did not reach 100% within reasonable time."
70-
)
71-
sleep(1)
72-
i = +1
42+
# Wait for download completion
43+
nav.wait_for_download_completion()

0 commit comments

Comments
 (0)