Skip to content

[🚀 Feature]: [py] Reduce pytest output of Selenium-internals on exception #15664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ShaheedHaque opened this issue Apr 24, 2025 · 3 comments
Closed
Labels
C-py Python Bindings I-enhancement Something could be better J-awaiting answer Question asked of user; a reply moves it to triage again

Comments

@ShaheedHaque
Copy link

ShaheedHaque commented Apr 24, 2025

Description

I would like pytest to show less Selenium internal code and variables in it's output when there is an exception. For example, let's say one has a test like this:

class TestFlow(SeleniumTest):
    def test_10_ae_pay_run________(self):
        self.driver.find_element(By.ID, "no such element")  <<<< exception raised here

Then the output from pytest -slvv has something like these stack frames:

  1. The user code (as above)
  2. selenium/webdriver/remote/webdriver.py in find_element()
  3. selenium/webdriver/remote/webdriver.py in execute()
  4. selenium/webdriver/remote/errorhandler.py in check_response()

Of these, the first and last are shown in verbose form including (IIUC) all the source code of the function, the variables and their values and so on. (The middle ones are shown in a compact form, with only one line of source code). The net result is that there are about 140 lines of output for check_response(), including a stack trace.

I propose to make check_response() into a minimal wrapper around a _check_response_real() and so remove most of this. The core of a possible patch looks something like this:

 class ErrorHandler:
     """Handles errors returned by the WebDriver server."""
 
     def check_response(self, response: Dict[str, Any]) -> None:
+        exception_class, args = self._check_response_real(response)
+        if exception_class:
+            raise exception_class(*args)
+
+    def _check_response_real(self, response: Dict[str, Any]) -> Tuple[Optional[Type[Exception]], Optional[Tuple[Any]]]:
         """Checks that a JSON response from the WebDriver does not have an
         error.
 
         :Args:
          - response - The JSON response from the WebDriver server as a dictionary

The 140 lines of Selenium internal is reduced to about 40 lines if there a stack trace, or just 20 lines if not. IMHO, this makes reading the output and thence debugging much nicer. I will propose a patch in due course.

Have you considered any alternatives or workarounds?

No response

@ShaheedHaque ShaheedHaque added A-needs-triaging A Selenium member will evaluate this soon! I-enhancement Something could be better labels Apr 24, 2025
@selenium-ci
Copy link
Member

@ShaheedHaque, thank you for creating this issue. We will troubleshoot it as soon as we can.

Selenium Triage Team: remember to follow the Triage Guide

@github-actions github-actions bot added the C-py Python Bindings label Apr 24, 2025
@shbenzer
Copy link
Contributor

shbenzer commented Apr 24, 2025

I would like pytest to show less Selenium internal code and variables in it's output when there is an exception.

Pytest has args that control output like -v and -tb https://docs.pytest.org/en/stable/how-to/output.html

As a side note, the pytest output typically lists parameters, so if you have fixtures/parameters in your tests that include passwords, usernames, etc. I would also suggest using a simple obfuscation class like this:

class Secret:
    def __init__(self, value):
        self.value = value

    def __repr__(self):
        return "Secret(********)"

    def __str___(self):
        return "*******"

@shbenzer shbenzer removed the A-needs-triaging A Selenium member will evaluate this soon! label Apr 24, 2025
@shbenzer shbenzer added the J-awaiting answer Question asked of user; a reply moves it to triage again label May 3, 2025
Copy link

This issue was closed because we did not receive any additional information after 14 days.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-py Python Bindings I-enhancement Something could be better J-awaiting answer Question asked of user; a reply moves it to triage again
Projects
None yet
Development

No branches or pull requests

3 participants