Skip to content

[java]: docs example for full page screenshot in firefox #2066

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

Merged
merged 5 commits into from
Nov 18, 2024

Conversation

navin772
Copy link
Member

@navin772 navin772 commented Nov 18, 2024

User description

Thanks for contributing to the Selenium site and documentation!
A PR well described will help maintainers to review and merge it quickly

Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, and help reviewers by making them as simple and short as possible.

Description

Full page firefox screenshot example in Java.

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

Documentation, Enhancement


Description

  • Added a new Java test example for capturing full-page screenshots using FirefoxDriver.
  • Updated documentation to include the new Java code example for full-page screenshots in multiple languages (English, Japanese, Portuguese, Chinese).
  • Ensured that the Java code block references in the documentation point to the correct line in the source file.

Changes walkthrough 📝

Relevant files
Enhancement
FirefoxTest.java
Add full-page screenshot example in Firefox test                 

examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java

  • Added a test method for taking a full-page screenshot in Firefox.
  • Utilized getFullPageScreenshotAs method to capture the screenshot.
  • Verified the existence of the screenshot file.
  • +18/-1   
    Documentation
    firefox.en.md
    Update Java code block reference in documentation               

    website_and_docs/content/documentation/webdriver/browsers/firefox.en.md

    • Updated code block reference for Java example.
    +1/-1     
    firefox.ja.md
    Update Java code block reference in Japanese documentation

    website_and_docs/content/documentation/webdriver/browsers/firefox.ja.md

    • Updated code block reference for Java example.
    +1/-1     
    firefox.pt-br.md
    Update Java code block reference in Portuguese documentation

    website_and_docs/content/documentation/webdriver/browsers/firefox.pt-br.md

    • Updated code block reference for Java example.
    +1/-1     
    firefox.zh-cn.md
    Update Java code block reference in Chinese documentation

    website_and_docs/content/documentation/webdriver/browsers/firefox.zh-cn.md

    • Updated code block reference for Java example.
    +1/-1     

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

    Copy link

    netlify bot commented Nov 18, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 01c41d6

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Resource Cleanup
    The screenshot file is not cleaned up after the test. Consider adding cleanup code in the @AfterEach method to delete any generated screenshot files.

    File Path Handling
    Using hardcoded file paths could cause issues across different environments. Consider using a temporary directory or configurable output path.

    Copy link
    Contributor

    qodo-merge-pro bot commented Nov 18, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Best practice
    Ensure proper resource cleanup by using try-finally block for driver management

    Add a try-finally block to ensure the driver is properly closed even if an assertion
    or exception occurs during the test.

    examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java [175-190]

     @Test
     public void fullPageScreenshot() throws Exception {
         driver = startFirefoxDriver();
    -    driver.get("https://www.selenium.dev");
    -    File screenshot = driver.getFullPageScreenshotAs(OutputType.FILE);
    -    File targetFile = new File("full_page_screenshot.png");
    -    Files.move(screenshot.toPath(), targetFile.toPath());
    -    Assertions.assertTrue(targetFile.exists(), "The full page screenshot file should exist");
    -    driver.quit();
    +    try {
    +        driver.get("https://www.selenium.dev");
    +        File screenshot = driver.getFullPageScreenshotAs(OutputType.FILE);
    +        File targetFile = new File("full_page_screenshot.png");
    +        Files.move(screenshot.toPath(), targetFile.toPath());
    +        Assertions.assertTrue(targetFile.exists(), "The full page screenshot file should exist");
    +    } finally {
    +        driver.quit();
    +    }
     }
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: Adding a try-finally block is crucial for proper resource management, ensuring the WebDriver is closed even if exceptions occur during test execution. This prevents resource leaks and improves test reliability.

    8
    ✅ Clean up test artifacts by removing generated files after test completion
    Suggestion Impact:The commit implemented the suggestion to delete the screenshot file after the test, but used Files.deleteIfExists instead of targetFile.deleteOnExit.

    code diff:

    +    Files.deleteIfExists(targetFile.toPath());

    Delete the screenshot file after the test to clean up test artifacts and prevent
    accumulation of files.

    examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java [183-187]

     File targetFile = new File("full_page_screenshot.png");
     Files.move(screenshot.toPath(), targetFile.toPath());
     Assertions.assertTrue(targetFile.exists(), "The full page screenshot file should exist");
    +targetFile.deleteOnExit();
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Cleaning up test artifacts is a good practice to prevent disk space issues and maintain a clean test environment, though the impact is moderate since temporary files are typically manageable.

    6
    Enhancement
    Generate unique filenames to support parallel test execution

    Use a unique filename for each test run to prevent conflicts in parallel test
    execution.

    examples/java/src/test/java/dev/selenium/browsers/FirefoxTest.java [183]

    -File targetFile = new File("full_page_screenshot.png");
    +File targetFile = new File("full_page_screenshot_" + System.currentTimeMillis() + ".png");
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using unique filenames is important for preventing file conflicts during parallel test execution, which could lead to test failures or inconsistent results in CI/CD environments.

    7

    💡 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 @navin772 !

    @harsha509 harsha509 merged commit 55a8ef0 into SeleniumHQ:trunk Nov 18, 2024
    9 checks passed
    @navin772 navin772 deleted the java-full-page-screenshot branch November 19, 2024 12:04
    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.

    2 participants