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

Restore Original Scroll Position After Full Page Screenshot #3967

Merged

Conversation

kharithomas
Copy link
Contributor

@kharithomas kharithomas commented Oct 16, 2024

This PR adds functionality to restore the original scroll position after a full page screenshot is taken. This helps avoid the need to add extra actions after screenshots to return the position.

=========================================================

Thank you for your contribution.
Before submitting this PR, please make sure:

  • PR description and commit message should describe the changes done in this PR
  • Verify the PR is pointing to correct branch i.e. Release or Beta branch if the code fix is for specific release , else point it to master
  • Latest Code from master or specific release branch is merged to your branch
  • No unwanted\commented\junk code is included
  • No new warning upon build solution
  • Code Summary\Comments are added to my code which explains what my code is doing
  • Existing unit test cases are passed
  • New Unit tests are added for your development
  • Sanity Tests are successfully executed for New and Existing Functionality
  • Verify that changes are compatible with all relevant browsers and platforms.
  • After creating pull request there should not be any conflicts
  • Resolve all Codacy comments
  • Builds and checks are passed before PR is sent for review
  • Resolve code review comments
  • Update the Help Library document to match any feature changes

Summary by CodeRabbit

  • New Features
    • Enhanced functionality for capturing full-page screenshots, including original scroll position and improved device metrics handling.
  • Bug Fixes
    • Streamlined logic for determining device scale factor, improving reliability in screenshot capturing.
    • Added error handling to ensure consistent restoration of the browser's state after capturing screenshots.

Copy link
Contributor

coderabbitai bot commented Oct 16, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The changes in this pull request focus on the GetFullPageScreenshot method within the ChromeDriverEx class. Key modifications include capturing the original scroll position before taking a screenshot, constructing a metrics dictionary with page dimensions and device scale factors, and simplifying the logic for device metrics. After capturing the screenshot, the method resets the browser's metrics and scroll position to their original values. These enhancements improve the robustness and reliability of the screenshot capturing process.

Changes

File Path Change Summary
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/ChromeDriverEx.cs Updated GetFullPageScreenshot method to enhance full-page screenshot capturing, including original scroll position and device metrics handling.

Possibly related PRs

Poem

🐇 In the land of code where rabbits play,
A screenshot's magic has come to stay.
With scrolls and metrics, we capture the view,
Restoring the state, as good rabbits do!
Hopping through changes, we cheer with delight,
For full-page captures now shine ever bright! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (1)
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Selenium/ChromeDriverEx.cs (1)

35-36: Optimize Dimension Calculations if Possible

If document.documentElement.scrollWidth and document.documentElement.scrollHeight suffice for capturing the full page dimensions, you might simplify the calculations by removing redundant properties.

Consider:

-["width"] = driver.ExecuteScript("return Math.max(window.innerWidth, document.body.scrollWidth, document.documentElement.scrollWidth)"),
-["height"] = driver.ExecuteScript("return Math.max(window.innerHeight, document.body.scrollHeight, document.documentElement.scrollHeight)"),
+["width"] = driver.ExecuteScript("return document.documentElement.scrollWidth"),
+["height"] = driver.ExecuteScript("return document.documentElement.scrollHeight"),
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f0077d4 and 2b2c317.

📒 Files selected for processing (1)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Selenium/ChromeDriverEx.cs (1 hunks)
🧰 Additional context used
🔇 Additional comments (6)
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Selenium/ChromeDriverEx.cs (6)

29-30: Good Practice: Capturing Original Scroll Position

Capturing the original scroll position ensures that the user's view is restored after the screenshot is taken, enhancing user experience.


35-36: Efficient Calculation of Page Dimensions

Using Math.max on window.innerWidth, document.body.scrollWidth, and document.documentElement.scrollWidth accurately captures the maximum width and height of the page, accommodating various document structures.


37-37: Robust Device Scale Factor Retrieval

Retrieving window.devicePixelRatio with a fallback to 1 ensures that the device scale factor is correctly set, even if the script returns null.


41-41: Applying Device Metrics for Full Page Screenshot

Setting the device metrics override with the calculated dimensions ensures that the browser emulates the full page size, allowing for a complete screenshot.


44-44: Capturing the Full Page Screenshot

Taking the screenshot after adjusting the device metrics ensures that the entire page content is captured as intended.


48-49: Restoring Original Device Metrics and Scroll Position

Clearing the device metrics override and resetting the scroll position maintains the original state of the browser, preventing side effects from the screenshot process.

Comment on lines 29 to 49
// Capture the original scroll position
Dictionary<string, object> originalScrollPosition = (Dictionary<string, object>)driver.ExecuteScript("return { x: window.pageXOffset, y: window.pageYOffset };");

// Capture page dimensions and device metrics
Dictionary<string, Object> metrics = new Dictionary<string, Object>
{
["width"] = driver.ExecuteScript("return Math.max(window.innerWidth,document.body.scrollWidth,document.documentElement.scrollWidth)"),
["height"] = driver.ExecuteScript("return Math.max(window.innerHeight,document.body.scrollHeight,document.documentElement.scrollHeight)")
["width"] = driver.ExecuteScript("return Math.max(window.innerWidth, document.body.scrollWidth, document.documentElement.scrollWidth)"),
["height"] = driver.ExecuteScript("return Math.max(window.innerHeight, document.body.scrollHeight, document.documentElement.scrollHeight)"),
["deviceScaleFactor"] = Convert.ToDouble(driver.ExecuteScript("return window.devicePixelRatio") ?? 1),
["mobile"] = driver.ExecuteScript("return typeof window.orientation !== 'undefined'")
};
object devicePixelRatio = driver.ExecuteScript("return window.devicePixelRatio");
if (devicePixelRatio != null)
{
double doubleValue = 0;
if (double.TryParse(devicePixelRatio.ToString(), out doubleValue))
{
metrics["deviceScaleFactor"] = doubleValue;
}
else
{
long longValue = 0;
if (long.TryParse(devicePixelRatio.ToString(), out longValue))
{
metrics["deviceScaleFactor"] = longValue;
}
}
}
metrics["mobile"] = driver.ExecuteScript("return typeof window.orientation !== 'undefined'");
//Execute the emulation Chrome Command to change browser to a custom device that is the size of the entire page

// Execute the emulation Chrome command to change browser to a custom device that is the size of the entire page
driver.ExecuteCdpCommand("Emulation.setDeviceMetricsOverride", metrics);
//You can then just screenshot it as it thinks everything is visible

// Take screenshot as everything is now visible
Screenshot screenshot = driver.GetScreenshot();
//This command will return your browser back to a normal, usable form if you need to do anything else with it.
driver.ExecuteCdpCommand("Emulation.clearDeviceMetricsOverride", []);

// Reset the device metrics and scroll position to original state
driver.ExecuteCdpCommand("Emulation.clearDeviceMetricsOverride", new Dictionary<string, object>());
driver.ExecuteScript($"window.scrollTo({{ top: {originalScrollPosition["y"]}, left: {originalScrollPosition["x"]}, behavior: 'instant' }});");
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider Adding Error Handling for Script Executions

To enhance robustness, add error handling around script executions to manage potential exceptions or unexpected return values.

For example:

try
{
    // Script execution code
}
catch (WebDriverException e)
{
    // Handle exception
}

["width"] = driver.ExecuteScript("return Math.max(window.innerWidth, document.body.scrollWidth, document.documentElement.scrollWidth)"),
["height"] = driver.ExecuteScript("return Math.max(window.innerHeight, document.body.scrollHeight, document.documentElement.scrollHeight)"),
["deviceScaleFactor"] = Convert.ToDouble(driver.ExecuteScript("return window.devicePixelRatio") ?? 1),
["mobile"] = driver.ExecuteScript("return typeof window.orientation !== 'undefined'")
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve Mobile Detection Logic

The current mobile detection checks if window.orientation is defined. However, this method may not be reliable across all devices and browsers.

Consider using navigator.userAgent or navigator.userAgentData for a more robust mobile detection:

-["mobile"] = driver.ExecuteScript("return typeof window.orientation !== 'undefined'")
+["mobile"] = driver.ExecuteScript("return /Mobi|Android/i.test(navigator.userAgent)")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
["mobile"] = driver.ExecuteScript("return typeof window.orientation !== 'undefined'")
["mobile"] = driver.ExecuteScript("return /Mobi|Android/i.test(navigator.userAgent)")

Copy link
Contributor

@Maheshkale447 Maheshkale447 left a comment

Choose a reason for hiding this comment

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

Please check unit test cases failure, maybe latest code is not available on source branch

};
object devicePixelRatio = driver.ExecuteScript("return window.devicePixelRatio");
if (devicePixelRatio != null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's keep the original code, it was added after JS returned inconsistent values in long or sometimes in double. and long fails to case to double.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made changes - see latest commit.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (1)
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Selenium/ChromeDriverEx.cs (1)

Line range hint 40-56: Consider simplifying device pixel ratio parsing

While the current implementation is thorough in handling different numeric types, it could be simplified.

Consider this more concise alternative:

-            object devicePixelRatio = driver.ExecuteScript("return window.devicePixelRatio");
-            if (devicePixelRatio != null)
-            {
-                double doubleValue = 0;
-                if (double.TryParse(devicePixelRatio.ToString(), out doubleValue))
-                {
-                    metrics["deviceScaleFactor"] = doubleValue;
-                }
-                else
-                {
-                    long longValue = 0;
-                    if (long.TryParse(devicePixelRatio.ToString(), out longValue))
-                    {
-                        metrics["deviceScaleFactor"] = longValue;
-                    }
-                }
-            }
+            object devicePixelRatio = driver.ExecuteScript("return window.devicePixelRatio");
+            if (devicePixelRatio != null)
+            {
+                try {
+                    metrics["deviceScaleFactor"] = Convert.ToDouble(devicePixelRatio);
+                } catch (InvalidCastException) {
+                    // Fallback to default if conversion fails
+                    metrics["deviceScaleFactor"] = 1.0;
+                }
+            }

However, if you prefer the current explicit type handling for better debugging, that's also valid.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2b2c317 and cbccd8c.

📒 Files selected for processing (1)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Selenium/ChromeDriverEx.cs (2 hunks)
🔇 Additional comments (1)
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Selenium/ChromeDriverEx.cs (1)

32-38: LGTM: Metrics collection implementation is solid

The implementation correctly captures page dimensions and mobile status. The window dimensions calculation properly considers various scenarios (innerWidth/Height, body, and documentElement).

Comment on lines 29 to 31
// Capture the original scroll position
Dictionary<string, object> originalScrollPosition = (Dictionary<string, object>)driver.ExecuteScript("return { x: window.pageXOffset, y: window.pageYOffset };");

Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add error handling and type safety for scroll position capture

While the scroll position capture works, it needs better error handling and type safety.

Apply this diff to improve robustness:

-            Dictionary<string, object> originalScrollPosition = (Dictionary<string, object>)driver.ExecuteScript("return { x: window.pageXOffset, y: window.pageYOffset };");
+            Dictionary<string, object> originalScrollPosition;
+            try {
+                originalScrollPosition = (Dictionary<string, object>)driver.ExecuteScript("return { x: window.pageXOffset, y: window.pageYOffset };") 
+                    ?? new Dictionary<string, object> { ["x"] = 0, ["y"] = 0 };
+            } catch (WebDriverException ex) {
+                // Fallback to default values if script execution fails
+                originalScrollPosition = new Dictionary<string, object> { ["x"] = 0, ["y"] = 0 };
+            }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Capture the original scroll position
Dictionary<string, object> originalScrollPosition = (Dictionary<string, object>)driver.ExecuteScript("return { x: window.pageXOffset, y: window.pageYOffset };");
Dictionary<string, object> originalScrollPosition;
try {
originalScrollPosition = (Dictionary<string, object>)driver.ExecuteScript("return { x: window.pageXOffset, y: window.pageYOffset };")
?? new Dictionary<string, object> { ["x"] = 0, ["y"] = 0 };
} catch (WebDriverException ex) {
// Fallback to default values if script execution fails
originalScrollPosition = new Dictionary<string, object> { ["x"] = 0, ["y"] = 0 };
}

Comment on lines 58 to 66
// Execute the emulation Chrome command to change browser to a custom device that is the size of the entire page
driver.ExecuteCdpCommand("Emulation.setDeviceMetricsOverride", metrics);
//You can then just screenshot it as it thinks everything is visible

// Take screenshot as everything is now visible
Screenshot screenshot = driver.GetScreenshot();
//This command will return your browser back to a normal, usable form if you need to do anything else with it.
driver.ExecuteCdpCommand("Emulation.clearDeviceMetricsOverride", []);

// Reset the device metrics and scroll position to original state
driver.ExecuteCdpCommand("Emulation.clearDeviceMetricsOverride", new Dictionary<string, object>());
driver.ExecuteScript($"window.scrollTo({{ top: {originalScrollPosition["y"]}, left: {originalScrollPosition["x"]}, behavior: 'instant' }});");
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add error handling for CDP commands and scroll restoration

The screenshot capture and restoration logic needs error handling for CDP commands and scroll operations.

Apply this diff to improve robustness:

-            driver.ExecuteCdpCommand("Emulation.setDeviceMetricsOverride", metrics);
-
-            Screenshot screenshot = driver.GetScreenshot();
-
-            driver.ExecuteCdpCommand("Emulation.clearDeviceMetricsOverride", new Dictionary<string, object>());
-            driver.ExecuteScript($"window.scrollTo({{ top: {originalScrollPosition["y"]}, left: {originalScrollPosition["x"]}, behavior: 'instant' }});");
+            Screenshot screenshot = null;
+            try {
+                driver.ExecuteCdpCommand("Emulation.setDeviceMetricsOverride", metrics);
+                screenshot = driver.GetScreenshot();
+            } finally {
+                try {
+                    // Always attempt to restore original state
+                    driver.ExecuteCdpCommand("Emulation.clearDeviceMetricsOverride", new Dictionary<string, object>());
+                    driver.ExecuteScript($"window.scrollTo({{ top: {originalScrollPosition["y"]}, left: {originalScrollPosition["x"]}, behavior: 'instant' }});");
+                } catch (WebDriverException) {
+                    // Log warning if restoration fails
+                }
+            }
+            
+            if (screenshot == null) {
+                throw new WebDriverException("Failed to capture screenshot");
+            }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Execute the emulation Chrome command to change browser to a custom device that is the size of the entire page
driver.ExecuteCdpCommand("Emulation.setDeviceMetricsOverride", metrics);
//You can then just screenshot it as it thinks everything is visible
// Take screenshot as everything is now visible
Screenshot screenshot = driver.GetScreenshot();
//This command will return your browser back to a normal, usable form if you need to do anything else with it.
driver.ExecuteCdpCommand("Emulation.clearDeviceMetricsOverride", []);
// Reset the device metrics and scroll position to original state
driver.ExecuteCdpCommand("Emulation.clearDeviceMetricsOverride", new Dictionary<string, object>());
driver.ExecuteScript($"window.scrollTo({{ top: {originalScrollPosition["y"]}, left: {originalScrollPosition["x"]}, behavior: 'instant' }});");
// Execute the emulation Chrome command to change browser to a custom device that is the size of the entire page
Screenshot screenshot = null;
try {
driver.ExecuteCdpCommand("Emulation.setDeviceMetricsOverride", metrics);
screenshot = driver.GetScreenshot();
} finally {
try {
// Always attempt to restore original state
driver.ExecuteCdpCommand("Emulation.clearDeviceMetricsOverride", new Dictionary<string, object>());
driver.ExecuteScript($"window.scrollTo({{ top: {originalScrollPosition["y"]}, left: {originalScrollPosition["x"]}, behavior: 'instant' }});");
} catch (WebDriverException) {
// Log warning if restoration fails
}
}
if (screenshot == null) {
throw new WebDriverException("Failed to capture screenshot");
}

@Maheshkale447 Maheshkale447 merged commit 29f93d0 into Ginger-Automation:master Oct 28, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants