SeleniumHQ / seleniumhq.github.io

Official Selenium website and documentation
https://selenium.dev
Apache License 2.0
1.07k stars 1.27k forks source link

Log examples added for Ruby using Chrome #1730

Closed aguspe closed 3 months ago

aguspe commented 3 months ago

User description

Description

This is a continuation of the previous updates on special features for Chrome, now adding an example of how to get logs using ruby

Motivation and Context

It's important to keep the documentation and examples up to date for the selenium community to have good references

Types of changes

Checklist


PR Type

enhancement, documentation


Description


Changes walkthrough πŸ“

Relevant files
Enhancement
chrome_spec.rb
Add Ruby Test for Retrieving Chrome Browser Logs                 

examples/ruby/spec/browsers/chrome_spec.rb
  • Added a new test case to get browser logs using Ruby with Chrome.
  • The test navigates to a URL and retrieves the browser logs, checking
    for a specific log message.
  • +9/-0     
    Documentation
    chrome.en.md
    Update English Chrome Documentation with Ruby Example Link

    website_and_docs/content/documentation/webdriver/browsers/chrome.en.md
  • Updated the Ruby tab to link to the new browser log example in the
    Ruby code.
  • +1/-1     
    chrome.ja.md
    Update Japanese Chrome Documentation with Ruby Example Link

    website_and_docs/content/documentation/webdriver/browsers/chrome.ja.md
  • Updated the Ruby tab to link to the new browser log example in the
    Ruby code.
  • +1/-1     
    chrome.pt-br.md
    Update Portuguese Chrome Documentation with Ruby Example Link

    website_and_docs/content/documentation/webdriver/browsers/chrome.pt-br.md
  • Updated the Ruby tab to link to the new browser log example in the
    Ruby code.
  • +1/-1     
    chrome.zh-cn.md
    Update Chinese Chrome Documentation with Ruby Example Link

    website_and_docs/content/documentation/webdriver/browsers/chrome.zh-cn.md
  • Updated the Ruby tab to link to the new browser log example in the
    Ruby code.
  • +1/-1     

    πŸ’‘ PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    netlify[bot] commented 3 months ago

    Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    Latest commit b279a987d5ff1013156a2ffd0174d1d22c9d63aa
    codiumai-pr-agent-pro[bot] commented 3 months ago

    PR Description updated to latest commit (https://github.com/SeleniumHQ/seleniumhq.github.io/commit/9c30e7b2364318b962c36a2ac6788eaac79a003e)

    codiumai-pr-agent-pro[bot] commented 3 months ago

    PR Review πŸ”

    ⏱️ Estimated effort to review [1-5] 2, because the PR primarily involves adding a new test example and updating documentation. The changes are straightforward and localized, making it relatively easy to review.
    πŸ§ͺ Relevant tests Yes
    ⚑ Possible issues Possible Flakiness: The use of `sleep 1` in the test code could lead to flakiness in test execution. It's generally better to use explicit waits or other synchronization mechanisms.
    πŸ”’ Security concerns No
    codiumai-pr-agent-pro[bot] commented 3 months ago

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Replace sleep with explicit waits to enhance test reliability ___ **Replace the sleep method with a more reliable waiting mechanism to ensure that the browser
    logs are ready to be fetched. Using sleep can lead to flaky tests due to varying load
    times. Consider using explicit waits provided by Selenium WebDriver.** [examples/ruby/spec/browsers/chrome_spec.rb [141]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1730/files#diff-f3e68237826e1b64c37885dc12602e936e6e4ea29da653aeb659d466a465ffcaR141-R141) ```diff -sleep 1 +Selenium::WebDriver::Wait.new(timeout: 10).until { @driver.execute_script('return document.readyState') == 'complete' } ```
    Suggestion importance[1-10]: 8 Why: Replacing `sleep` with explicit waits is a significant improvement for test reliability and accuracy, addressing potential flakiness.
    8
    Improve the robustness of log message checking ___ **Use a more specific assertion to check for the presence of the error message in the
    browser logs. This ensures that the test is more robust and less likely to fail due to
    unexpected log messages.** [examples/ruby/spec/browsers/chrome_spec.rb [144]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1730/files#diff-f3e68237826e1b64c37885dc12602e936e6e4ea29da653aeb659d466a465ffcaR144-R144) ```diff -expect(logs.first.message).to include 'Failed to load resource' +expect(logs.any? { |log| log.message.include?('Failed to load resource') }).to be true ```
    Suggestion importance[1-10]: 6 Why: Improving the specificity of assertions enhances test robustness, but the impact is moderate as it refines existing functionality rather than addressing a major flaw.
    6
    Best practice
    Add cleanup code to quit WebDriver after tests ___ **Ensure proper cleanup by adding an after block to quit the WebDriver session. This helps
    in preventing resource leakage and ensures that each test is isolated.** [examples/ruby/spec/browsers/chrome_spec.rb [138-145]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1730/files#diff-f3e68237826e1b64c37885dc12602e936e6e4ea29da653aeb659d466a465ffcaR138-R145) ```diff it 'gets the browser logs' do @driver = Selenium::WebDriver.for :chrome @driver.navigate.to 'https://www.selenium.dev/selenium/web/' sleep 1 logs = @driver.logs.get(:browser) expect(logs.first.message).to include 'Failed to load resource' end +after do + @driver.quit +end + ```
    Suggestion importance[1-10]: 7 Why: Adding an `after` block for cleanup is a good practice to prevent resource leakage and ensure test isolation, although it's a common practice rather than a critical fix.
    7
    Maintainability
    Manage WebDriver instances more safely to prevent resource leaks ___ **Initialize the WebDriver instance using a context manager or ensure it's properly handled
    to avoid starting multiple sessions or leaving sessions open, which can affect subsequent
    tests or system resources.** [examples/ruby/spec/browsers/chrome_spec.rb [139]](https://github.com/SeleniumHQ/seleniumhq.github.io/pull/1730/files#diff-f3e68237826e1b64c37885dc12602e936e6e4ea29da653aeb659d466a465ffcaR139-R139) ```diff -@driver = Selenium::WebDriver.for :chrome +begin + @driver = Selenium::WebDriver.for :chrome + # rest of the code +ensure + @driver.quit +end ```
    Suggestion importance[1-10]: 7 Why: Managing WebDriver instances properly is crucial for resource management and test reliability, making this a valuable suggestion for maintainability.
    7