Skip to content

Commit 1f036de

Browse files
committed
[tests:fix] Fixed hide_loading_overlay: only wait if element is present
Sometimes the loading overlay may be stale/removed and this method was failing randomly causing flaky tests
1 parent 8b0907a commit 1f036de

2 files changed

Lines changed: 7 additions & 11 deletions

File tree

openwisp_utils/tests/selenium.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,10 @@ def wait_for(self, method, by, value, timeout=2, driver=None):
285285
def hide_loading_overlay(self, html_id="loading-overlay", timeout=2, driver=None):
286286
"""The geckodriver can't figure out the loading overlay is still fading out, so let's just hide it."""
287287
driver = driver or self.web_driver
288-
driver.execute_script(
289-
f'document.getElementById("{html_id}").style.display="none";'
288+
element_exists = driver.execute_script(
289+
f'var el = document.getElementById("{html_id}"); '
290+
f'if (el) {{ el.style.display="none"; return true; }} return false;'
290291
)
291-
self.wait_for_invisibility(By.ID, html_id, timeout, driver)
292+
# Only wait if element exists
293+
if element_exists:
294+
self.wait_for_invisibility(By.ID, html_id, timeout, driver)

tests/test_project/tests/test_selenium.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from channels.testing import ChannelsLiveServerTestCase
44
from django.test import tag
55
from django.urls import reverse
6-
from selenium.common.exceptions import JavascriptException, NoSuchElementException
6+
from selenium.common.exceptions import NoSuchElementException
77
from selenium.webdriver import ActionChains
88
from selenium.webdriver.common.by import By
99

@@ -725,13 +725,6 @@ def setUp(self):
725725
self.login()
726726

727727
def test_hide_loading_overlay(self):
728-
try:
729-
self.hide_loading_overlay()
730-
except JavascriptException:
731-
pass
732-
else:
733-
self.fail("Javascript exception not raised")
734-
735728
self.web_driver.execute_script(
736729
'document.body.insertAdjacentHTML("beforeend", "<div id=\'loading-overlay\'></div>");'
737730
)

0 commit comments

Comments
 (0)