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

BugFix - Playwright Driver not working with Shadow DOM #4008

Merged

Conversation

IamRanjeetSingh
Copy link
Contributor

@IamRanjeetSingh IamRanjeetSingh commented Nov 27, 2024

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

    • Introduced a method to find parent elements in the Page Object Model (POM), enhancing element location capabilities.
    • Added a new method for converting POM and element IDs into a single string format.
  • Improvements

    • Enhanced tracking of parent-child relationships during element learning processes, improving organization and identification of elements.
  • Bug Fixes

    • Adjusted internal logic for better handling of shadow DOM and frame elements.

Copy link
Contributor

coderabbitai bot commented Nov 27, 2024

Walkthrough

The pull request introduces significant updates to the BrowserElementLocator, POMLearner, and POMLocatorParser classes. A new method FindParentElementInPOMAsync is added to BrowserElementLocator for locating parent elements in the Page Object Model (POM). The POMLearner class is enhanced to track parent-child relationships by adding a Guid parentElementId parameter to multiple methods. Additionally, a new static method Convert is introduced in POMLocatorParser to format POM and element IDs into a single string. These changes improve element handling in shadow DOMs and the overall structure of learned elements.

Changes

File Path Change Summary
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/BrowserElementLocator.cs Added method FindParentElementInPOMAsync for retrieving parent elements; modified FindAllMatchingElementsFromPOMAsync to handle shadow hosts and update locators.
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/POM/POMLearner.cs Updated methods to include Guid parentElementId for tracking parent-child relationships; modified internal logic to ensure correct parent ID references in HTMLElementInfo.
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/POM/POMLocatorParser.cs Added public static method Convert to format POM and element IDs into a single string.

Possibly related PRs

Suggested reviewers

  • Maheshkale447

Poem

🐰 In the land of code where elements play,
A parent is found in a brand new way.
With shadows and paths, we learn and explore,
Each element's journey opens a door.
So hop along, dear friends, let's cheer,
For changes in POM bring us near! 🎉


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: 3

🧹 Outside diff range and nitpick comments (3)
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/POM/POMLearner.cs (3)

81-81: Ensure proper handling of root elements with parentElementId.

In the call to LearnDocumentElementsAsync, passing Guid.Empty as parentElementId might lead to confusion or incorrect parent-child relationships for root elements. Consider defining a constant or a specific GUID to represent root elements for better clarity and consistency.


98-98: Update documentation for methods with new parameters.

The method LearnHtmlNodeChildElements now includes parentElementId. Ensure that any corresponding documentation or comments are updated to reflect this change for better maintainability.


Line range hint 231-254: Optimize CreateHTMLElementInfoAsync for clarity and error handling.

  • Clarity: The method signature now includes parentElementId. Ensure parameter ordering enhances readability, possibly grouping related parameters together.
  • Error Handling: Validate parentElementId before usage to prevent potential exceptions when calling ToString() on a default or invalid GUID.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7cb950c and f7c65b0.

📒 Files selected for processing (3)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/BrowserElementLocator.cs (3 hunks)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/POM/POMLearner.cs (8 hunks)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/POM/POMLocatorParser.cs (1 hunks)
🔇 Additional comments (8)
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/POM/POMLocatorParser.cs (1)

102-105: Verify consistent usage of the Convert method

Let's ensure this new method is being used consistently across the codebase and matches existing string format patterns.

✅ Verification successful

Based on the search results, I can now generate the final response:

Convert method usage is consistent with existing POM locator patterns

The new Convert method implementation matches the existing POM locator pattern usage across the codebase:

  • The method is being used correctly in BrowserElementLocator.cs: POMLocatorParser.Convert(pomId, parentElementId)
  • The format matches existing POM element locator patterns that use underscore separator:
    • Split operations consistently use Split('_') to parse POM locators
    • POM element locators are consistently constructed as {pomGuid}_{elementGuid}
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check usage patterns of POM locator string formatting

# Find all string concatenations or interpolations with underscore that might be related to POM
rg -g '*.cs' '["\'].*_.*["\']' --type-add 'cs:*.cs'

# Find direct calls to the new Convert method
rg -g '*.cs' 'Convert\s*\(\s*\w+\s*,\s*\w+\s*\)' --type-add 'cs:*.cs'

# Find existing POM locator string patterns
rg -g '*.cs' 'Split\s*\(\s*["\']_["\']' --type-add 'cs:*.cs'

Length of output: 117


Script:

#!/bin/bash
# Search for string patterns with underscore
rg -g "*.cs" "_" -A 2 -B 2

# Find direct calls to Convert method in POMLocatorParser
rg -g "*.cs" "POMLocatorParser\.Convert" -A 2 -B 2

# Find string split operations with underscore
rg -g "*.cs" "\.Split\(['\"]_['\"]" -A 2 -B 2

Length of output: 131867


Script:

#!/bin/bash
# Search for POMLocatorParser.Convert usage
rg -g "*.cs" "POMLocatorParser\.Convert\s*\(\s*\w+\s*,\s*\w+\s*\)" -A 2 -B 2

# Search for string splitting with underscore specifically in POM related files
rg -g "*.cs" -g "*POM*.cs" "\.Split\s*\(\s*['\"]_['\"]" -A 2 -B 2

# Search for string concatenation with underscore in POM related files
rg -g "*.cs" -g "*POM*.cs" "\+\s*['\"]_['\"]|\+\s*['\"]_['\"]" -A 2 -B 2

Length of output: 42672

Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/BrowserElementLocator.cs (4)

27-27: Addition of missing namespace for Drivers.Common

The added using GingerCore.Drivers.Common; ensures that all necessary types from the Drivers.Common namespace are available.


102-118: Verify the accuracy of XPath to CSS selector conversion

When the parent element is a shadow host, the code converts XPath locators to CSS selectors to correctly locate elements within the shadow DOM. It's important to ensure that the ConvertXPathToCssSelector method handles all XPath expressions accurately, as some complex XPath selectors may not have a direct CSS equivalent.

Consider adding unit tests to validate the conversion of various XPath expressions to CSS selectors to ensure reliability.

Would you like assistance in creating these unit tests or in verifying the conversion logic?


124-124: Assignment of modified element to ElementInfo

Updating ElementInfo with the potentially modified element ensures that any changes made to locators, such as converting XPath to CSS selectors, are reflected during element location.


134-151: Implementation of FindParentElementInPOMAsync method

The new method FindParentElementInPOMAsync correctly retrieves the parent element by navigating the POM hierarchy and handles cases where the parent element does not exist or is not applicable.

Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/POM/POMLearner.cs (3)

84-86: Verify the propagation of parentElementId in recursive calls.

As parentElementId is passed down to LearnHtmlNodeChildElements, ensure that it accurately represents the parent element at each level. This is crucial for maintaining correct parent-child relationships when learning elements.


132-136: Confirm consistent use of parentElementId in recursive element learning.

In recursive calls to LearnHtmlNodeChildElements, check that parentElementId correctly references the immediate parent. This consistency is vital for accurately reconstructing the DOM hierarchy.


863-863: Passing shadowHostElement.Guid as parentElementId improves hierarchy tracking.

Good job on using shadowHostElement.Guid when learning shadow DOM elements. This ensures that elements within the shadow DOM are correctly associated with their shadow host.

Comment on lines +102 to +105
public static string Convert(Guid pomId, Guid elementId)
{
return $"{pomId}_{elementId}";
}
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

Access modifier inconsistency and missing validation

The new Convert method has several issues:

  1. The method is marked as public while the containing class is internal. This exposes implementation details and breaks encapsulation.
  2. Unlike the Create method, there's no validation for the input parameters.
  3. XML documentation is missing, breaking the documentation pattern established in the class.

Consider applying these changes:

-        public static string Convert(Guid pomId, Guid elementId)
-        {
-            return $"{pomId}_{elementId}";
-        }
+        /// <summary>
+        /// Converts POM and element IDs into a locator string.
+        /// </summary>
+        /// <param name="pomId">The ID of the POM.</param>
+        /// <param name="elementId">The ID of the element within the POM.</param>
+        /// <returns>A formatted string combining both IDs.</returns>
+        /// <exception cref="ArgumentException">Thrown when either ID is empty.</exception>
+        internal static string Convert(Guid pomId, Guid elementId)
+        {
+            if (pomId == Guid.Empty)
+            {
+                throw new ArgumentException($"{nameof(pomId)} cannot be empty");
+            }
+            if (elementId == Guid.Empty)
+            {
+                throw new ArgumentException($"{nameof(elementId)} cannot be empty");
+            }
+            return $"{pomId}_{elementId}";
+        }
📝 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
public static string Convert(Guid pomId, Guid elementId)
{
return $"{pomId}_{elementId}";
}
/// <summary>
/// Converts POM and element IDs into a locator string.
/// </summary>
/// <param name="pomId">The ID of the POM.</param>
/// <param name="elementId">The ID of the element within the POM.</param>
/// <returns>A formatted string combining both IDs.</returns>
/// <exception cref="ArgumentException">Thrown when either ID is empty.</exception>
internal static string Convert(Guid pomId, Guid elementId)
{
if (pomId == Guid.Empty)
{
throw new ArgumentException($"{nameof(pomId)} cannot be empty");
}
if (elementId == Guid.Empty)
{
throw new ArgumentException($"{nameof(elementId)} cannot be empty");
}
return $"{pomId}_{elementId}";
}

@@ -113,7 +113,7 @@ private async Task LearnHtmlNodeChildElements(HtmlNode htmlNode, string parentPa
browserElement = await _browserElementProvider.GetElementAsync(eLocateBy.ByXPath, childNode.XPath);
if (browserElement != null && await IsBrowserElementVisibleAsync(browserElement))
{
childElement = await CreateHTMLElementInfoAsync(childNode, parentPath, browserElement, captureScreenshot: shouldLearnThisNode);
childElement = await CreateHTMLElementInfoAsync(childNode, parentPath, parentElementId, browserElement, captureScreenshot: shouldLearnThisNode);
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

Handle cases where parentElementId might not be set.

When creating an HTMLElementInfo, ensure that parentElementId is valid. If Guid.Empty is passed unintentionally, it might affect the integrity of the element hierarchy.

@@ -899,7 +899,7 @@ private async Task LearnFrameElementsAsync(HTMLElementInfo frameElement, string

string newParentPath = string.IsNullOrEmpty(parentPath) ? frameElement.XPath : $"{parentPath},{frameElement.XPath}";

await LearnDocumentElementsAsync(frameHtmlDocument, newParentPath, learnedElements, cancellationToken);
await LearnDocumentElementsAsync(frameHtmlDocument, newParentPath, Guid.Empty, learnedElements, cancellationToken);
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

Re-evaluate usage of Guid.Empty for frame elements.

When learning frame elements, using Guid.Empty as parentElementId might not accurately represent the parent-child relationship. Consider passing the actual frameElement.Guid to maintain consistency.

Apply this diff to pass the correct parentElementId:

- await LearnDocumentElementsAsync(frameHtmlDocument, newParentPath, Guid.Empty, learnedElements, cancellationToken);
+ await LearnDocumentElementsAsync(frameHtmlDocument, newParentPath, frameElement.Guid, learnedElements, cancellationToken);

Committable suggestion skipped: line range outside the PR's diff.

@Maheshkale447 Maheshkale447 merged commit a23ffc4 into master Nov 27, 2024
8 checks passed
@Maheshkale447 Maheshkale447 deleted the BugFix/PlaywrightDriverNotWorkingWithShadowDOM branch November 27, 2024 10:23
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