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

[rb] Add PrintOptions Implementation for Ruby WebDriver #15158

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

Conversation

yvsvarma
Copy link
Contributor

@yvsvarma yvsvarma commented Jan 25, 2025

User description

Description
This PR introduces the PrintOptions implementation for Ruby WebDriver, aligning it with similar functionality available in Java, .NET, and Python bindings. The PrintOptions class (print_options.rb) provides an interface to define printing configurations, such as page size, orientation, scale, and margins, and converts these configurations into a hash suitable for WebDriver commands.

Key Changes

1. PrintOptions Class (lib/selenium/webdriver/print_options.rb):
Added a class to manage print configurations with default values:

  • Orientation: portrait
  • Scale: 1.0
  • Background printing: false
  • Page size: Default to Letter (21.59 cm x 27.94 cm).
  • Margins: Default to 1.0 cm on all sides.

2. Supports predefined page sizes:

  • :letter (21.59 cm x 27.94 cm)
  • :legal (21.59 cm x 35.56 cm)
  • :a4 (21.0 cm x 29.7 cm)
  • :tabloid (27.94 cm x 43.18 cm)
  1. Custom page sizes can also be set.
  2. The to_h method generates a hash representation of the options for WebDriver.

Unit Tests (spec/unit/selenium/print_options_spec.rb):
Added tests to ensure:

  • Default values are set correctly.
  • Custom and predefined page sizes can be applied.
  • Invalid page size raises an error.
  • Conversion to hash works as expected.

Testing
Verified functionality through the rspec test suite:

  • Command: rspec spec/unit/selenium/print_options_spec.rb - Result: All tests pass successfully.

Reference Implementations

This implementation follows the structure and functionality of the PrintOptions feature in other languages:


PR Type

Enhancement, Tests


Description

  • Introduced PrintOptions class for Ruby WebDriver to manage print configurations.

  • Added support for default, predefined, and custom page sizes.

  • Implemented to_h method to convert configurations into WebDriver-compatible hash.

  • Added comprehensive unit tests for PrintOptions functionality.


Changes walkthrough 📝

Relevant files
Enhancement
print_options.rb
Introduced `PrintOptions` class for print configurations 

rb/lib/selenium/webdriver/print_options.rb

  • Added PrintOptions class to manage print configurations.
  • Defined default values for orientation, scale, margins, and page size.
  • Implemented to_h method for hash conversion.
  • Added method to set predefined page sizes with validation.
  • +78/-0   
    Tests
    spec_helper.rb
    Added RSpec configuration for tests                                           

    rb/spec/spec_helper.rb

  • Configured RSpec for testing.
  • Included necessary dependencies for PrintOptions testing.
  • +15/-0   
    print_options_spec.rb
    Added unit tests for `PrintOptions` functionality               

    rb/spec/unit/selenium/print_options_spec.rb

  • Added unit tests for PrintOptions class.
  • Tested default values, custom and predefined page sizes.
  • Verified hash conversion and error handling for invalid page sizes.
  • +85/-0   

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Input Validation

    The scale and margins values are not validated. Should add checks to ensure scale is positive and margins are non-negative.

    #
    # @return [Hash]
    def to_h
      options = {
        orientation: @orientation,
        scale: @scale,
        background: @background,
        pageRanges: @page_ranges,
        paperWidth: @page_size[:width],
        paperHeight: @page_size[:height],
        marginTop: @margins[:top],
        marginBottom: @margins[:bottom],
        marginLeft: @margins[:left],
        marginRight: @margins[:right]
      }
    
      options.compact
    end
    Documentation

    The page_ranges parameter lacks documentation about its expected format and validation. Should document the expected string format.

    attr_accessor :orientation, :scale, :background, :page_ranges, :page_size, :margins

    Copy link
    Contributor

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add scale value validation

    Add validation for scale value to ensure it's within a reasonable range (e.g., 0.1
    to 2.0) to prevent printing issues.

    rb/lib/selenium/webdriver/print_options.rb [31-38]

     def initialize
       @orientation = DEFAULT_ORIENTATION
       @scale = DEFAULT_SCALE
       @background = false
       @page_ranges = nil
       @page_size = DEFAULT_PAGE_SIZE
       @margins = DEFAULT_MARGINS
     end
     
    +def scale=(value)
    +  raise ArgumentError, "Scale must be between 0.1 and 2.0" unless (0.1..2.0).include?(value.to_f)
    +  @scale = value.to_f
    +end
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding scale validation is crucial for preventing printing issues and ensuring proper output. The suggestion provides a clear range check and proper error handling.

    8
    Validate orientation values

    Add validation for orientation to ensure only valid values ('portrait' or
    'landscape') are accepted.

    rb/lib/selenium/webdriver/print_options.rb [29]

    -attr_accessor :orientation, :scale, :background, :page_ranges, :page_size, :margins
    +attr_reader :orientation, :scale, :background, :page_ranges, :page_size, :margins
     
    +def orientation=(value)
    +  raise ArgumentError, "Orientation must be 'portrait' or 'landscape'" unless ['portrait', 'landscape'].include?(value)
    +  @orientation = value
    +end
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Restricting orientation to valid values ('portrait' or 'landscape') is essential for preventing runtime errors and ensuring correct print configuration.

    8
    Validate margin values

    Add validation for margins to prevent negative values which could cause printing
    errors.

    rb/lib/selenium/webdriver/print_options.rb [27]

     DEFAULT_MARGINS = { top: 1.0, bottom: 1.0, left: 1.0, right: 1.0 }.freeze
     
    +def margins=(margins_hash)
    +  margins_hash.each do |key, value|
    +    raise ArgumentError, "Margin values must be non-negative" if value.to_f < 0
    +  end
    +  @margins = margins_hash
    +end
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Preventing negative margin values is important for proper page layout and printing functionality. The suggestion adds necessary validation with clear error messaging.

    7

    @yvsvarma yvsvarma changed the title Add PrintOptions Implementation for Ruby WebDriver [rb] Add PrintOptions Implementation for Ruby WebDriver Jan 26, 2025
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    1 participant