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

[java] Enhance PageSize class to support for predefined and custom Paper Sizes #15052

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

Conversation

yvsvarma
Copy link

@yvsvarma yvsvarma commented Jan 9, 2025

User description

PR Description

Current Behavior
The PageSize class in Selenium provides basic functionality to manage page dimensions but lacks the ability to:
Set predefined paper sizes like "A4", "A6", "Legal", "Tabloid," etc.
This is also a discrepancy between the documented functionality in the examples Print Page which says user can set the paper size as - “A0”, “A6”, “Legal”, “Tabloid”, etc.

The updated PageSize class introduces:
Predefined Sizes:
Added an internal PaperSize enum that includes common paper sizes:
A4: 27.94 cm x 21.59 cm
A6: 14.8 cm x 10.5 cm
Legal: 35.56 cm x 21.59 cm
Tabloid: 43.18 cm x 27.94 cm

Convenient Factory Methods:
New static methods for creating predefined sizes:
PageSize.A4(), PageSize.A6(), PageSize.LEGAL(), and PageSize.TABLOID().

Custom Sizes:
Added support for users to create PageSize objects with custom dimensions:
Example: new PageSize(30.0, 20.0).

Enhanced Usability:
Users can now:

Set a predefined paper size directly:
printOptions.setPageSize(PageSize.TABLOID());

Retrieve height and width using getHeight() and getWidth():

double height = printOptions.getPageSize().getHeight();

Define custom paper sizes for flexibility:

PageSize customSize = new PageSize(30.0, 20.0);
printOptions.setPageSize(customSize);

How to Use:
1. Predefined Sizes:

PrintOptions printOptions = new PrintOptions();
printOptions.setPageSize(PageSize.LEGAL());

2. Custom Sizes:

PageSize customPage = new PageSize(25.0, 18.0);
printOptions.setPageSize(customPage);

3. Retrieving Dimensions:

double height = printOptions.getPageSize().getHeight();
double width = printOptions.getPageSize().getWidth();

Note: Examples will be updated on this Print Page once this PR is approved and merged.


PR Type

Enhancement


Description

  • Added predefined paper sizes using an internal PaperSize enum.

  • Introduced factory methods for common paper sizes (e.g., PageSize.A4()).

  • Enabled custom page size creation with height and width parameters.

  • Enhanced usability with toMap serialization and toString representation.


Changes walkthrough 📝

Relevant files
Enhancement
PageSize.java
Added predefined and custom paper size support                     

java/src/org/openqa/selenium/print/PageSize.java

  • Added PaperSize enum for predefined sizes (A4, A6, Legal, Tabloid).
  • Introduced constructors for predefined and custom sizes.
  • Implemented factory methods for predefined sizes.
  • Enhanced serialization with toMap and added toString method.
  • +79/-26 

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

    Copy link
    Contributor

    qodo-merge-pro bot commented Jan 9, 2025

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Immutability

    The class holds state (height/width) but doesn't enforce immutability. Consider making fields final and ensuring defensive copies in getters if needed.

    private final double height;
    private final double width;
    Input Validation

    The custom size constructor accepts any height/width values without validation. Consider adding checks for negative or zero values.

    public PageSize(double height, double width) {
        this.height = height;
        this.width = width;
    }

    Copy link
    Contributor

    qodo-merge-pro bot commented Jan 9, 2025

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Possible issue
    Add validation to prevent invalid page dimensions that could cause rendering problems

    Add input validation in the custom size constructor to prevent negative or zero
    dimensions, which could cause rendering issues.

    java/src/org/openqa/selenium/print/PageSize.java [34-37]

     public PageSize(double height, double width) {
    +    if (height <= 0 || width <= 0) {
    +        throw new IllegalArgumentException("Page dimensions must be positive values");
    +    }
         this.height = height;
         this.width = width;
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding input validation for page dimensions is crucial to prevent runtime errors and ensure proper rendering. This is a significant improvement for code robustness and user experience.

    8
    Security
    Enhance enum immutability to prevent potential thread-safety issues

    Make the PaperSize enum immutable by making its fields final and marking the enum as
    public static final.

    java/src/org/openqa/selenium/print/PageSize.java [79-83]

    -public enum PaperSize {
    +public static final enum PaperSize {
         A4(27.94, 21.59),
         A6(14.8, 10.5),
         LEGAL(35.56, 21.59),
         TABLOID(43.18, 27.94);
    • Apply this suggestion
    Suggestion importance[1-10]: 2

    Why: The suggestion to add 'static final' to the enum is unnecessary as Java enums are inherently static and final. The suggestion addresses a non-existent issue.

    2

    @shbenzer
    Copy link
    Contributor

    shbenzer commented Jan 9, 2025

    Thanks for the PR @yvsvarma, this is a great suggestion!

    Could you please add a test to ensure this works as intended?

    @yvsvarma
    Copy link
    Author

    Hi @shbenzer,

    Thank you for your review and feedback!

    To address your request, I’d like to point out that we already have an example to test PageSize functionality in the seleniumhq.github.io repository. Specifically, the example is in the PrintOptionsTest class.

    I’ve verified the enhanced functionality locally by updating the existing example, printed the sizes and compared, everything is working as intended with the changes in this PR. For your reference, I’ve attached the example, I intend to update after this PR - PrintOptionsTest.txt

    Once this PR is merged, I’d be happy to enhance the PrintOptionsTest example in the seleniumhq.github.io repository to reflect the updated PageSize implementation.

    Please note that the existing example remains valid with this update.

    Let me know if there’s anything else you’d like me to address.

    Thank you!

    @shbenzer
    Copy link
    Contributor

    shbenzer commented Jan 10, 2025

    To address your request, I’d like to point out that we already have an example to test PageSize functionality in the seleniumhq.github.io repository. Specifically, the example is in the PrintOptionsTest class.

    We need a test inside this repo, and can’t rely on the examples in the docs to test our code. Since this is new functionality it needs a new test.

    Nothing complex, just something simple like:

    1. Set size (PageSize.setPageSize(PageSize.LEGAL);)
    2. Assert PageSize.getHeight() == y : “failed”
    3. Assert PageSize.getWidth() == x : “failed”

    @shbenzer
    Copy link
    Contributor

    shbenzer commented Jan 10, 2025

    PrintOptions printOptions = new PrintOptions();
    printOptions.setPageSize(PageSize.LEGAL());

    Let’s make this more Java standard, and like the Keys class, so instead it’ll be setPageSize(PageSize.LEGAL). We want to reference a constant instead of calling a method inside setPageSize()

    @yvsvarma yvsvarma force-pushed the print-page-size-options branch 2 times, most recently from f584082 to b251728 Compare January 11, 2025 20:02
    @yvsvarma
    Copy link
    Author

    PrintOptions printOptions = new PrintOptions();
    printOptions.setPageSize(PageSize.LEGAL());

    Let’s make this more Java standard, and like the Keys class, so instead it’ll be setPageSize(PageSize.LEGAL). We want to reference a constant instead of calling a method inside setPageSize()

    @shbenzer , Fixed as per your review comments.

    @yvsvarma yvsvarma requested a review from Delta456 January 11, 2025 20:21
    @yvsvarma yvsvarma force-pushed the print-page-size-options branch from 62d9369 to f24cf74 Compare January 11, 2025 23:14
    @yvsvarma
    Copy link
    Author

    To address your request, I’d like to point out that we already have an example to test PageSize functionality in the seleniumhq.github.io repository. Specifically, the example is in the PrintOptionsTest class.

    We need a test inside this repo, and can’t rely on the examples in the docs to test our code. Since this is new functionality it needs a new test.

    Nothing complex, just something simple like:

    1. Set size (PageSize.setPageSize(PageSize.LEGAL);)
    2. Assert PageSize.getHeight() == y : “failed”
    3. Assert PageSize.getWidth() == x : “failed”

    Updated test cases also in this repo. Please review.

    yvsvarma added a commit to yvsvarma/selenium that referenced this pull request Jan 12, 2025
    yvsvarma added a commit to yvsvarma/selenium that referenced this pull request Jan 12, 2025
    yvsvarma added a commit to yvsvarma/selenium that referenced this pull request Jan 12, 2025
    yvsvarma added a commit to yvsvarma/selenium that referenced this pull request Jan 12, 2025
    yvsvarma added a commit to yvsvarma/selenium that referenced this pull request Jan 12, 2025
    @yvsvarma
    Copy link
    Author

    yvsvarma commented Jan 15, 2025

    @Delta456 , I am avoiding any new format changes (like line addition / deletion) in this PR as suggested by Puja earlier on this PR. Confining changes to the intended changes for the feature only, so that will be clear to reviewers. Will create a fresh PR with that subject on the suggestions we may have. Thanks for reviewing.

    @yvsvarma
    Copy link
    Author

    In my mind a prefix should be added to the names e.g. ISO_A4, US_LETTER, ANSI_A, ... There are overlapping definitions in the different standards, e.g. ISO_B4 (250mm × 353mm) and JIS_B4 (257 × 364)

    Thanks for the review @joerg1985 . I have updated the names to - ISO_A4, US_LEGAL, ANSI_TABLOID, US_LETTER. Kindly review.

    @yvsvarma
    Copy link
    Author

    @yvsvarma we'll need to make sure all bindings implement this feature the same way. Here's the current difference in each PR per your tests:

    Python:

    print_options.set_page_size(PrintOptions.LEGAL)

    Java:

    PageSize pageSize = PageSize.A4;

    I prefer the set_page_size method implementation - as that allows for an easy and logical way for users to set custom sizes.

    Hi @shbenzer , Thank you again for reviewing. Updated java implementation accordingly. Please take a look.

    Copy link
    Contributor

    @pujagani pujagani 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! @yvsvarma

    @shbenzer
    Copy link
    Contributor

    Hi @shbenzer , Thank you again for reviewing. Updated java implementation accordingly. Please take a look.

    Excellent, thank you.

    @shbenzer
    Copy link
    Contributor

    @yvsvarma please run scripts/format.sh to pass CI - RBE / Format / Check format script run (pull_request)

    @yvsvarma
    Copy link
    Author

    @pujagani , Thank you for reviewing and approving the changes.

    @yvsvarma
    Copy link
    Author

    @yvsvarma please run scripts/format.sh to pass CI - RBE / Format / Check format script run (pull_request)

    Done. Ran format.sh successfully and pushed those changes in the last commit. Please take a look @shbenzer

    @VietND96 VietND96 changed the title Enhance PageSize class to support for predefined and custom Paper Sizes [java] Enhance PageSize class to support for predefined and custom Paper Sizes Jan 19, 2025
    @shbenzer
    Copy link
    Contributor

    shbenzer commented Jan 19, 2025

    This is the current integration with PrintOptions class, correct?

    printOptions.setPageSize(PageSize.setPageSize(PageSize.ISO_A4));

    because if so, it should be:

    printOptions.setPageSize(PageSize.ISO_A4);

    Let’s remove the redundant function call - once that’s done I think it’ll be good to go.

    @yvsvarma
    Copy link
    Author

    @shbenzer , Thanks again for reviewing. Updated as per review comments. Also, ran scripts/format.sh. All good.
    Please review further.

    @yvsvarma
    Copy link
    Author

    @VietND96 , can you please review.

    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.

    7 participants