Skip to content

Add Python Examples for PrintOptions #1994

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

Conversation

shbenzer
Copy link
Contributor

@shbenzer shbenzer commented Oct 13, 2024

User description

Added python examples for PrintOptions

Description

added test_print_options.py
Added python examples
updated badge codes for other languages (missing example to implementation missing)

Motivation and Context

code examples
Issue #1941

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 Python test cases for Selenium's PrintOptions class, covering orientation, page range, size, margin, scale, background, and shrink-to-fit settings.
  • Updated documentation across multiple languages to include actual Python code examples for PrintOptions.
  • Replaced placeholder badges with real code snippets in the documentation.

Changes walkthrough 📝

Relevant files
Tests
test_print_options.py
Add Python tests for Selenium PrintOptions                             

examples/python/tests/interactions/test_print_options.py

  • Added pytest fixture for WebDriver setup and teardown.
  • Implemented tests for various PrintOptions settings.
  • Verified orientation, page range, size, margin, scale, background, and
    shrink-to-fit options.
  • +63/-0   
    Documentation
    print_page.en.md
    Update Python documentation for PrintOptions examples       

    website_and_docs/content/documentation/webdriver/interactions/print_page.en.md

  • Updated Python code examples with actual implementation.
  • Replaced badge-code with gh-codeblock for Python.
  • Minor formatting changes.
  • +29/-38 
    print_page.ja.md
    Update Japanese documentation for PrintOptions examples   

    website_and_docs/content/documentation/webdriver/interactions/print_page.ja.md

  • Updated Python code examples with actual implementation.
  • Replaced badge-code with gh-codeblock for Python.
  • Minor formatting changes.
  • +29/-38 
    print_page.pt-br.md
    Update Portuguese documentation for PrintOptions examples

    website_and_docs/content/documentation/webdriver/interactions/print_page.pt-br.md

  • Updated Python code examples with actual implementation.
  • Replaced badge-code with gh-codeblock for Python.
  • Minor formatting changes.
  • +29/-38 
    print_page.zh-cn.md
    Update Chinese documentation for PrintOptions examples     

    website_and_docs/content/documentation/webdriver/interactions/print_page.zh-cn.md

  • Updated Python code examples with actual implementation.
  • Replaced badge-code with gh-codeblock for Python.
  • Minor formatting changes.
  • +29/-38 

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

    Copy link

    netlify bot commented Oct 13, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 7b8b226

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

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    🎫 Ticket compliance analysis 🔶

    1941 - Partially compliant

    Fully compliant requirements:

    • Expand documentation for Selenium's PrintOptions class

    Not compliant requirements:

    • Provide use cases for PrintOptions
    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Test Coverage
    Verify if all possible values for orientation, page size, and scale are tested

    Documentation Completeness
    Check if the documentation covers all PrintOptions features and provides clear usage instructions

    Copy link
    Contributor

    qodo-merge-pro bot commented Oct 13, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Parameterize tests to reduce code duplication and improve maintainability

    Consider parameterizing the tests for different print options to reduce code
    duplication and make it easier to add new test cases in the future. This can be
    achieved using pytest's parameterize decorator.

    examples/python/tests/interactions/test_print_options.py [11-16]

    -def test_orientation(driver):
    +@pytest.mark.parametrize("option,value,getter", [
    +    ("orientation", PrintOptions.Orientation.LANDSCAPE, "get_orientation"),
    +    ("page_ranges", "1-3", "get_page_ranges"),
    +    ("page_size", "A4", "get_size"),
    +    # Add more options here
    +])
    +def test_print_options(driver, option, value, getter):
         driver.get("https://www.selenium.dev/")
         print_options = PrintOptions()
    -    print_options.set_orientation(PrintOptions.Orientation.LANDSCAPE)
    -    current_orientation = print_options.get_orientation()
    -    assert current_orientation == PrintOptions.Orientation.LANDSCAPE
    +    getattr(print_options, f"set_{option}")(value)
    +    current_value = getattr(print_options, getter)()
    +    assert current_value == value
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: Parameterizing tests significantly reduces code duplication and improves maintainability. It allows for easier addition of new test cases and enhances the overall structure of the test suite.

    9
    Simplify multiple assertions into a single, more concise statement

    Instead of using separate assertions for each margin, consider using assert all()
    with a list comprehension to check all margins in a single statement. This makes the
    test more concise and easier to maintain.

    examples/python/tests/interactions/test_print_options.py [39-42]

    -assert current_margin.get_top() == 10
    -assert current_margin.get_bottom() == 10
    -assert current_margin.get_left() == 10
    -assert current_margin.get_right() == 10
    +assert all(getattr(current_margin, f'get_{side}')() == 10 for side in ['top', 'bottom', 'left', 'right'])
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion to use assert all() with a list comprehension simplifies the code, making it more concise and easier to maintain. This enhances readability without altering the test logic.

    7
    Add input validation and more informative error messages in test cases

    Consider adding error handling and validation for the input values in the test
    cases. This can help catch potential issues early and provide more informative error
    messages.

    examples/python/tests/interactions/test_print_options.py [47-49]

    -print_options.set_scale(0.5) ## 0.1 to 2.0
    +scale_value = 0.5
    +if not 0.1 <= scale_value <= 2.0:
    +    raise ValueError("Scale must be between 0.1 and 2.0")
    +print_options.set_scale(scale_value)
     current_scale = print_options.get_scale()
    -assert current_scale == 0.5
    +assert current_scale == scale_value, f"Expected scale {scale_value}, but got {current_scale}"
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Adding input validation and informative error messages can help catch potential issues early and improve debugging. However, this suggestion is less critical for test cases where inputs are controlled.

    6
    Best practice
    Use a context manager for WebDriver to ensure proper resource management

    Consider using a context manager for the WebDriver to ensure proper cleanup. This
    can be achieved by using Python's with statement or implementing enter and
    exit methods in a custom class.

    examples/python/tests/interactions/test_print_options.py [5-9]

     @pytest.fixture()
     def driver():
    -    driver = webdriver.Chrome()
    -    yield driver
    -    driver.quit()
    +    with webdriver.Chrome() as driver:
    +        yield driver
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Using a context manager for WebDriver ensures that resources are properly managed and cleaned up, reducing the risk of resource leaks. This is a best practice that enhances the reliability and maintainability of the test setup.

    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
    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.

    HI @shbenzer,

    Tests are failing in the CI. Could you please take a look when you get a chance?

    Thanks,
    Sri

    @shbenzer
    Copy link
    Contributor Author

    @harsha509 I accidentally created a conflict - use #2000 instead.

    @shbenzer shbenzer closed this Oct 15, 2024
    @shbenzer shbenzer deleted the Add-Python-Examples-for-printOptions branch October 15, 2024 17:02
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    documentation Improvements or additions to documentation Review effort [1-5]: 2 tests
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants