Skip to content
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

Updated Python Examples for Options #2010

Merged

Conversation

shbenzer
Copy link
Contributor

@shbenzer shbenzer commented Oct 22, 2024

User description

Updated Python examples in Browser Options

Description

added examples to test_options.py
made examples more consistent with other language examples
updated all translations of index.mds

Motivation and Context

Issue #1983

Types of changes

  • Change to the site (I have double-checked the Netlify deployment, and my changes look good)
  • Code example added (and I also added the example to all translated languages)
  • Improved translation
  • Added new translation (and I also added a notice to each document missing translation)

Checklist

  • I have read the contributing document.
  • I have used hugo to render the site/docs locally and I am sure it works.

PR Type

Tests, Documentation


Description

  • Added new Python test cases for browser options including browser name, version, and platform name.
  • Updated existing Python test cases to improve consistency and ensure all tests navigate to and quit from the Selenium website.
  • Updated documentation in multiple languages (English, Japanese, Portuguese, Chinese) to reflect new line numbers for Python code examples.
  • Ensured consistency across different language examples in the documentation.

Changes walkthrough 📝

Relevant files
Tests
test_options.py
Enhance and unify Python test examples for browser options

examples/python/tests/drivers/test_options.py

  • Added new test cases for browser name, version, and platform name.
  • Updated existing test cases to improve consistency.
  • Changed FirefoxOptions to ChromeOptions in test_set_window_rect.
  • Ensured all tests navigate to and quit from the Selenium website.
  • +31/-25 
    Documentation
    options.en.md
    Update English documentation with new Python example references

    website_and_docs/content/documentation/webdriver/drivers/options.en.md

  • Updated Python code example references to new line numbers.
  • Ensured consistency with updated Python test examples.
  • +14/-14 
    options.ja.md
    Update Japanese documentation with new Python example references

    website_and_docs/content/documentation/webdriver/drivers/options.ja.md

  • Updated Python code example references to new line numbers.
  • Ensured consistency with updated Python test examples.
  • +14/-14 
    options.pt-br.md
    Update Portuguese documentation with new Python example references

    website_and_docs/content/documentation/webdriver/drivers/options.pt-br.md

  • Updated Python code example references to new line numbers.
  • Ensured consistency with updated Python test examples.
  • +14/-14 
    options.zh-cn.md
    Update Chinese documentation with new Python example references

    website_and_docs/content/documentation/webdriver/drivers/options.zh-cn.md

  • Updated Python code example references to new line numbers.
  • Ensured consistency with updated Python test examples.
  • +14/-14 

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link

    netlify bot commented Oct 22, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 69de78a

    @qodo-merge-pro qodo-merge-pro bot added documentation Improvements or additions to documentation tests Review effort [1-5]: 3 labels Oct 22, 2024
    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis 🔶

    1983 - Partially compliant

    Fully compliant requirements:

    • Update code examples in the Browser Options section to be consistent across programming languages
    • Ensure Java example creates an options object and inspects the browser name attribute
    • Make Python examples consistent with Java example
    • Ensure consistency in the pageLoadStrategy section across all language examples

    Not compliant requirements:

    • Ruby examples are not updated in this PR
    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Consistency Check
    Verify that the new Python examples are consistent with Java examples, especially for browser name, version, and platform name.

    Documentation Update
    Ensure that the updated line numbers in the documentation accurately reflect the changes made in the Python test file.

    Copy link
    Contributor

    qodo-merge-pro bot commented Oct 22, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Use fixtures and parameterization to reduce code duplication and improve test maintainability

    Extract the common setup and teardown code into fixture functions to reduce code
    duplication across test cases.

    examples/python/tests/drivers/test_options.py [6-11]

    -def test_page_load_strategy_normal():
    +import pytest
    +
    +@pytest.fixture
    +def chrome_driver(request):
         options = webdriver.ChromeOptions()
    -    options.page_load_strategy = 'normal'
    +    options.page_load_strategy = request.param
         driver = webdriver.Chrome(options=options)
    -    driver.get("https://www.selenium.dev/")
    +    yield driver
         driver.quit()
     
    +@pytest.mark.parametrize('chrome_driver', ['normal', 'eager', 'none'], indirect=True)
    +def test_page_load_strategy(chrome_driver):
    +    chrome_driver.get("https://www.selenium.dev/")
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: The suggestion significantly reduces code duplication and enhances maintainability by using pytest fixtures and parameterization, which is a best practice in test design.

    9
    Add assertions to verify that options are correctly applied to the driver

    Add assertions to verify that the options are correctly set and applied to the
    driver.

    examples/python/tests/drivers/test_options.py [78-83]

     def test_set_browser_name():
         options = webdriver.ChromeOptions()
         assert options.capabilities['browserName'] == 'chrome'
         driver = webdriver.Chrome(options=options)
    +    assert driver.capabilities['browserName'] == 'chrome'
         driver.get("https://www.selenium.dev/")
         driver.quit()
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Adding assertions to verify that options are correctly applied enhances test reliability by ensuring that the driver is configured as expected, which is crucial for test accuracy.

    7
    Add error handling and logging to improve test robustness and debuggability

    Consider adding error handling and logging to improve the robustness and
    debuggability of the tests.

    examples/python/tests/drivers/test_options.py [71-76]

    +import logging
    +
     def test_proxy():
         options = webdriver.ChromeOptions()
         options.proxy = Proxy({ 'proxyType': ProxyType.MANUAL, 'httpProxy' : 'http.proxy:1234'})
    -    driver = webdriver.Chrome(options=options)
    -    driver.get("https://www.selenium.dev/")
    -    driver.quit()
    +    try:
    +        driver = webdriver.Chrome(options=options)
    +        driver.get("https://www.selenium.dev/")
    +    except Exception as e:
    +        logging.error(f"Error occurred: {e}")
    +        raise
    +    finally:
    +        if 'driver' in locals():
    +            driver.quit()
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: While adding error handling and logging can improve robustness and debuggability, the implementation is somewhat verbose for a test context, which slightly reduces its impact.

    6
    Best practice
    Use a context manager to ensure proper resource management and exception handling

    Consider using a context manager (with statement) to ensure the driver is properly
    closed, even if an exception occurs during the test.

    examples/python/tests/drivers/test_options.py [7-11]

     options = webdriver.ChromeOptions()
     options.page_load_strategy = 'normal'
    -driver = webdriver.Chrome(options=options)
    -driver.get("https://www.selenium.dev/")
    -driver.quit()
    +with webdriver.Chrome(options=options) as driver:
    +    driver.get("https://www.selenium.dev/")
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion improves resource management by ensuring the driver is closed properly even if an exception occurs, enhancing the robustness and reliability of the tests.

    8

    💡 Need additional feedback ? start a PR chat

    Copy link
    Member

    @harsha509 harsha509 left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    Thank you @shbenzer !

    Copy link

    @A1exKH A1exKH left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    LGTM.

    @shbenzer
    Copy link
    Contributor Author

    Failure has nothing to do with submitted code - though I do see it occasionally on other PRs. Do we know why this test fails on Ubuntu? tests/actions_api/test_mouse.py::test_move_by_offset_from_viewport_origin_ab @harsha509 @A1exKH

    @harsha509 harsha509 merged commit 8deef6c into SeleniumHQ:trunk Oct 28, 2024
    9 checks passed
    @harsha509
    Copy link
    Member

    tests/actions_api/test_mouse.py::test_move_by_offset_from_viewport_origin_ab

    Looks like a flaky one..!

    Looking into it!

    selenium-ci added a commit that referenced this pull request Oct 28, 2024
    * Updated Python Examples for Options
    
    * think set_window_rect() is just supported for firefox
    
    ---------
    
    Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com> 8deef6c
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    3 participants