SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
29.77k stars 8.02k forks source link

[py] Implement High Level Logging API with BiDi #14107

Closed p0deje closed 2 weeks ago

p0deje commented 3 weeks ago

Partial Python implementation of https://github.com/SeleniumHQ/selenium/issues/13992, closely mimics Ruby part from https://github.com/SeleniumHQ/selenium/pull/14073

titusfortner commented 3 weeks ago

Yeah, this isn't really testing the code, just showing how to use it, so maybe we don't need it in specs, just in our docs. What does this look like in the python?

        driver.navigate.to url_for('bidi/logEntryAdded.html')
        log_entries = []
        driver.script.add_console_message_handler { |log| log_entries << log  if log.level == 'error'}
        driver.find_element(id: 'consoleLog').click
        driver.find_element(id: 'consoleError').click
        wait.until { log_entries.any? }
        expect(log_entries[0].level).to eq('error')
p0deje commented 3 weeks ago

Yeah, this isn't really testing the code, just showing how to use it, so maybe we don't need it in specs, just in our docs. What does this look like in the python?

        driver.navigate.to url_for('bidi/logEntryAdded.html')
        log_entries = []
        driver.script.add_console_message_handler { |log| log_entries << log  if log.level == 'error'}
        driver.find_element(id: 'consoleLog').click
        driver.find_element(id: 'consoleError').click
        wait.until { log_entries.any? }
        expect(log_entries[0].level).to eq('error')

The equivalent would one of:

# simple lambda
errors = []
driver.script.add_console_message_handler(lambda log_entry: if log.level == 'error': errors.append(log_entry) }
# function
self.errors = []

def collect_error(log_entry)
    if log_entry.level == 'error':
        self.errors.append(log_entry)

driver.script.add_console_message_handler(collect_error}
# aggregator object
class ErrorsHandler:
    def __init__():
      self.errors = []

   def collect_error(log_entry)
      if log_entry.level == 'error':
          self.errors.append(log_entry)

errors_handler = ErrorsHandler()
driver.script.add_console_message_handler(errors_handler.collect_error}
titusfortner commented 2 weeks ago

@p0deje I didn't realize you added the options in here. I implemented enable_bidi() and web_socket_url parameters in trunk. You want me to rebase this?

codiumai-pr-agent-pro[bot] commented 2 weeks ago

CI Failure Feedback 🧐

(Checks updated until commit https://github.com/SeleniumHQ/selenium/commit/16e2e766f4e1ef2e5e417446fb163d19e6213c42)

**Action:** Python / Lint
**Failed stage:** [Test with tox](https://github.com/SeleniumHQ/selenium/actions/runs/9571783856/job/26389959727) [❌]
**Failure summary:** The action failed because the code formatting check detected that the file conftest.py is not
properly formatted according to the black code style.
  • 1 file would be reformatted: This indicates that conftest.py does not comply with the required code
    style.
  • The linting process exited with code 1, indicating a failure.
  • Relevant error logs: ```yaml 1: ##[group]Operating System 2: Ubuntu ... 223: + 224: request.addfinalizer(fin) 225: if request.node.get_closest_marker("no_driver_after_test"): 226: driver_instance = None 227: would reformat conftest.py 228: Oh no! 💥 💔 💥 229: 1 file would be reformatted, 219 files would be left unchanged. 230: linting-ci: exit 1 (1.83 seconds) /home/runner/work/selenium/selenium/py> black --check --diff selenium/ test/ conftest.py -l 120 pid=1949 231: linting-ci: FAIL code 1 (5.66=setup[3.37]+cmd[0.46,1.83] seconds) 232: evaluation failed :( (5.82 seconds) 233: ##[error]Process completed with exit code 1. ```

    ✨ CI feedback usage guide:
    The CI feedback tool (`/checks)` automatically triggers when a PR has a failed check. The tool analyzes the failed checks and provides several feedbacks: - Failed stage - Failed test name - Failure summary - Relevant error logs In addition to being automatically triggered, the tool can also be invoked manually by commenting on a PR: ``` /checks "https://github.com/{repo_name}/actions/runs/{run_number}/job/{job_number}" ``` where `{repo_name}` is the name of the repository, `{run_number}` is the run number of the failed check, and `{job_number}` is the job number of the failed check. #### Configuration options - `enable_auto_checks_feedback` - if set to true, the tool will automatically provide feedback when a check is failed. Default is true. - `excluded_checks_list` - a list of checks to exclude from the feedback, for example: ["check1", "check2"]. Default is an empty list. - `enable_help_text` - if set to true, the tool will provide a help message with the feedback. Default is true. - `persistent_comment` - if set to true, the tool will overwrite a previous checks comment with the new feedback. Default is true. - `final_update_message` - if `persistent_comment` is true and updating a previous checks message, the tool will also create a new message: "Persistent checks updated to latest commit". Default is true. See more information about the `checks` tool in the [docs](https://pr-agent-docs.codium.ai/tools/ci_feedback/).
    codecov[bot] commented 2 weeks ago

    Codecov Report

    Attention: Patch coverage is 18.89764% with 103 lines in your changes missing coverage. Please review.

    Project coverage is 56.98%. Comparing base (5c5487f) to head (d07cbe3). Report is 3 commits behind head on trunk.

    :exclamation: Current head d07cbe3 differs from pull request most recent head 43c5fc9

    Please upload reports for the commit 43c5fc9 to get more accurate results.

    Files Patch % Lines
    py/selenium/webdriver/common/bidi/script.py 28.98% 45 Missing and 4 partials :warning:
    .../selenium/webdriver/remote/websocket_connection.py 7.69% 24 Missing :warning:
    py/selenium/webdriver/common/bidi/session.py 0.00% 18 Missing :warning:
    py/selenium/webdriver/remote/webdriver.py 15.38% 11 Missing :warning:
    py/selenium/webdriver/common/options.py 0.00% 1 Missing :warning:
    Additional details and impacted files ```diff @@ Coverage Diff @@ ## trunk #14107 +/- ## ========================================== - Coverage 57.76% 56.98% -0.79% ========================================== Files 87 89 +2 Lines 5413 5528 +115 Branches 228 232 +4 ========================================== + Hits 3127 3150 +23 - Misses 2058 2146 +88 - Partials 228 232 +4 ```

    :umbrella: View full report in Codecov by Sentry.
    :loudspeaker: Have feedback on the report? Share it here.