Skip to content

Commit 98540a6

Browse files
authored
[chores] Minor improvements found while reviewing SeleniumTestMixin
1 parent 6b306a9 commit 98540a6

2 files changed

Lines changed: 89 additions & 33 deletions

File tree

docs/developer/test-utilities.rst

Lines changed: 88 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -230,40 +230,96 @@ should be automatically installed when setting up the development
230230
environment. All the OpenWISP modules using ``SeleniumTestMixin`` are
231231
already depending on ``openwisp-utils[selenium]``.
232232

233-
Methods
234-
~~~~~~~
233+
Key Methods of ``SeleniumTestMixin``
234+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235235

236-
- ``setUpClass()`` (``@classmethod``): Initializes the Selenium WebDriver
237-
with Firefox and applies custom settings to improve test reliability.
236+
``setUpClass()`` (``@classmethod``)
237+
+++++++++++++++++++++++++++++++++++
238+
239+
Initializes the Selenium WebDriver with either Firefox (default) or
240+
Chrome.
241+
242+
Applies a number of settings by default to improve test reliability.
243+
244+
- Uses the ``SELENIUM_HEADLESS`` environment variable to determine whether
245+
to run in headless mode.
246+
- Firefox-specific options:
238247

239-
- Uses the ``SELENIUM_HEADLESS`` environment variable to determine
240-
whether to run in headless mode.
241248
- Uses the ``GECKO_BIN`` environment variable to specify a custom
242249
Firefox binary location.
243250
- Uses the ``GECKO_LOG`` environment variable to enable GeckoDriver
244-
logging to ``geckodriver.log``. - Configures preferences to disable
245-
hardware acceleration and increase timeouts.
246-
247-
- ``tearDownClass()`` (``@classmethod``): Quits the Selenium WebDriver to
248-
clean up resources after the test class has finished executing.
249-
- ``open(url, driver=None, timeout=5)``: Opens a URL in the browser. -
250-
Waits for the page to fully load before returning. - Ensures the
251-
``#main-content`` element is present before proceeding.
252-
- ``login(username=None, password=None, driver=None)``: Logs into the
253-
Django admin dashboard. - Defaults to using ``admin`` / ``password``
254-
credentials. - Navigates to ``/admin/login/`` and fills in the login
255-
form.
256-
- ``find_element(by, value, timeout=2, wait_for='visibility')``: Finds an
257-
element using Selenium's ``find_element`` method. - Waits for the
258-
element based on the specified ``wait_for`` condition (``visibility``,
259-
``presence``).
260-
- ``wait_for_visibility(by, value, timeout=2)``: Waits until an element is
261-
visible.
262-
- ``wait_for_invisibility(by, value, timeout=2)``: Waits until an element
263-
is no longer visible.
264-
- ``wait_for_presence(by, value, timeout=2)``: Waits until an element is
265-
present in the DOM.
266-
- ``wait_for(method, by, value, timeout=2)``: General method for waiting
267-
for an element based on a given condition. - Uses Selenium's
268-
``WebDriverWait`` and Expected Conditions (``EC``). - If the timeout is
269-
reached, the test fails with a descriptive error message.
251+
logging to ``geckodriver.log``.
252+
- Uses ``page_load_strategy = "eager"`` to start interacting with the
253+
page before it's fully loaded.
254+
- Disables hardware acceleration for improved stability
255+
(``gfx.webrender.force-disabled``, ``layers.acceleration.disabled``).
256+
- Increases script timeout (``dom.max_script_run_time = 30``).
257+
- Uses a free port for Marionette to support parallel test execution.
258+
- Injects a Firefox extension to capture browser console logs, since
259+
Firefox does not support the WebDriver ``get_log`` API.
260+
261+
- Chrome-specific options:
262+
263+
- Uses the ``CHROME_BIN`` environment variable to specify a custom
264+
Chrome binary location.
265+
- Uses ``page_load_strategy = "eager"`` to start interacting with the
266+
page before it's fully loaded.
267+
- Adds flags: ``--ignore-certificate-errors``, ``--no-sandbox``,
268+
``--disable-gpu``, ``--disable-dev-shm-usage``,
269+
``--disable-features=VizDisplayCompositor``.
270+
- Uses a free remote debugging port to support parallel test execution.
271+
- Enables browser logging with ``goog:loggingPrefs`` to retrieve console
272+
logs via ``get_browser_logs()``.
273+
274+
``tearDownClass()`` (``@classmethod``)
275+
++++++++++++++++++++++++++++++++++++++
276+
277+
Quits the Selenium WebDriver to clean up resources after the test class
278+
has finished executing.
279+
280+
``open(url, html_container="#main-content", driver=None, timeout=5)``
281+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
282+
283+
- Opens a URL in the browser.
284+
- Waits for the page to fully load before returning.
285+
- Waits for the ``html_container`` element to be visible before
286+
proceeding.
287+
288+
``login(username=None, password=None, driver=None)``
289+
++++++++++++++++++++++++++++++++++++++++++++++++++++
290+
291+
Logs into the Django admin dashboard.
292+
293+
- Defaults to using ``admin`` / ``password`` credentials.
294+
- Navigates to ``/admin/login/`` and fills in the login form.
295+
296+
``find_element(by, value, timeout=2, driver=None, wait_for='visibility')``
297+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
298+
299+
Finds an element using Selenium's ``find_element`` method. Waits for the
300+
element based on the specified ``wait_for`` condition (``visibility``,
301+
``presence``).
302+
303+
``wait_for_visibility(by, value, timeout=2, driver=None)``
304+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
305+
306+
Waits until an element is visible.
307+
308+
``wait_for_invisibility(by, value, timeout=2, driver=None)``
309+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
310+
311+
Waits until an element is no longer visible.
312+
313+
``wait_for_presence(by, value, timeout=2, driver=None)``
314+
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
315+
316+
Waits until an element is present in the DOM.
317+
318+
``wait_for(method, by, value, timeout=2, driver=None)``
319+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
320+
321+
General method for waiting for an element based on a given condition. Uses
322+
Selenium's ``WebDriverWait`` and Expected Conditions (``EC``).
323+
324+
If the timeout is reached, the test fails with a descriptive error
325+
message.

openwisp_utils/tests/selenium.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def wait_for(self, method, by, value, timeout=2, driver=None):
281281
driver = driver or self.web_driver
282282
try:
283283
return WebDriverWait(driver, timeout).until(
284-
getattr(EC, method)(((by, value)))
284+
getattr(EC, method)((by, value))
285285
)
286286
except TimeoutException as e:
287287
print(self.get_browser_logs(driver))

0 commit comments

Comments
 (0)