Skip to content

BugFix - 39434 - Support for Locator checking in analyzer.#3768

Merged
Maheshkale447 merged 2 commits into
Releases/Betafrom
BugFix/39434-AnalyzeForUnsupportedLocators
Jun 14, 2024
Merged

BugFix - 39434 - Support for Locator checking in analyzer.#3768
Maheshkale447 merged 2 commits into
Releases/Betafrom
BugFix/39434-AnalyzeForUnsupportedLocators

Conversation

@IamHarsimranjeet

@IamHarsimranjeet IamHarsimranjeet commented Jun 14, 2024

Copy link
Copy Markdown
Contributor

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 custom exception LocatorNotSupportedException for unsupported locator types in web operations.
    • Added GetElementsAsync method to IBrowserTab for locating elements asynchronously.
  • Improvements

    • Enhanced error handling and messaging for unsupported operations in PlaywrightDriver.
    • Added support checks for element and frame locators in PlaywrightBrowserTab.
  • Bug Fixes

    • Refined logic for supported operations and actions in PlaywrightDriver and handler classes.
  • Code Cleanup

    • Removed deprecated methods and lists related to supported operations in action handlers.

@coderabbitai

coderabbitai Bot commented Jun 14, 2024

Copy link
Copy Markdown
Contributor

Walkthrough

The updates focus on improving the GingerCoreNET project by introducing a new LocatorNotSupportedException custom exception, extending the IBrowserTab interface with an asynchronous method for locating elements, and refining the element locator and action support logic across several classes in the Playwright driver. These changes enhance error handling, streamline supported operations checking, and ensure robustness in element location procedures.

Changes

Files/Paths Change Summaries
.../Exceptions/LocatorNotSupportedException.cs Introduced a custom exception class LocatorNotSupportedException with a constructor for custom messages.
.../IBrowserTab.cs Added the GetElementsAsync method to the IBrowserTab interface, throwing LocatorNotSupportedException for unsupported locators.
.../Web/Playwright/ActionHandlers/ActBrowserElementHandler.cs Included System.Diagnostics.CodeAnalysis namespace, removed SupportedOperations list and method, added Equals and GetHashCode methods to Context struct.
.../Web/Playwright/ActionHandlers/ActUIElementHandler.cs Removed the SupportedOperations list and IsOperationSupported method, and adjusted exception handling.
.../Web/Playwright/PlaywrightBrowserTab.cs Added SupportedElementLocators and SupportedFrameLocators lists, introduced methods to check supported locators, renamed LocateElementAsync to GetElementLocator, enhanced exception handling in relevant methods.
.../Web/Playwright/PlaywrightDriver.cs Refined logic for supported actions, updated checks for supported operations, improved error messaging, and introduced predefined sets of supported operations for ActUIElement and ActBrowserElement.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant IBrowserTab
    participant PlaywrightDriver

    User->>IBrowserTab: GetElementsAsync(locateBy, locateValue)
    IBrowserTab->>PlaywrightDriver: IsElementLocatorSupported(locateBy)
    alt Locator Supported
        PlaywrightDriver-->>IBrowserTab: Supported
        IBrowserTab-->>User: Elements Collection
    else Locator Not Supported
        PlaywrightDriver-->>IBrowserTab: Not Supported
        IBrowserTab-->>User: throws LocatorNotSupportedException
    end
Loading

Poem

In Ginger’s world of code so grand,
A new exception now shall stand.
Locate elements with async cheer,
But unsupported ones must steer clear.
With logic refined and errors so bright,
We code through the day and into the night.
Hippity-hop, the browser obeys, a coder's delight! 🐇✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 5

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

47-53: Ensure the XML documentation for GetElementsAsync is clear and complete.

Consider adding more details in the <returns> tag to explain what is returned by the method.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6a96256 and dc6b1b6.

Files selected for processing (6)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Exceptions/LocatorNotSupportedException.cs (1 hunks)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/IBrowserTab.cs (2 hunks)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/ActionHandlers/ActBrowserElementHandler.cs (4 hunks)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/ActionHandlers/ActUIElementHandler.cs (3 hunks)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightBrowserTab.cs (7 hunks)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightDriver.cs (1 hunks)
Files skipped from review due to trivial changes (1)
  • Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Exceptions/LocatorNotSupportedException.cs
Additional context used
Learnings (1)
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightBrowserTab.cs (2)
User: IamRanjeetSingh
PR: Ginger-Automation/Ginger#3738
File: Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightBrowserTab.cs:72-77
Timestamp: 2024-06-07T20:51:31.445Z
Learning: Error handling for the `GoToURLAsync` method in the `PlaywrightBrowserTab` class is managed in the parent method or surrounding context.
User: IamRanjeetSingh
PR: Ginger-Automation/Ginger#3753
File: Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/PlaywrightBrowserTab.cs:99-99
Timestamp: 2024-06-12T12:54:44.221Z
Learning: User IamRanjeetSingh prefers exceptions to propagate rather than being caught and handled locally within methods in `PlaywrightBrowserTab.cs`.
GitHub Check: Codacy Static Code Analysis
Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/ActionHandlers/ActUIElementHandler.cs

[warning] 137-137: Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/ActionHandlers/ActUIElementHandler.cs#L137
Either remove or fill this block of code.

Comment on lines +156 to +160
if (!IsFrameLocatorSupported(locateBy))
{
throw new LocatorNotSupportedException($"Frame locator '{locateBy}' is not supported.");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Refactor to simplify the SwitchFrameAsync method.

Consider using a dictionary to map eLocateBy values to their corresponding frame locator strings to reduce the switch-case complexity.

Also applies to: 177-177

Comment on lines +136 to +138
catch (LocatorNotSupportedException ex)
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Address the empty catch block for LocatorNotSupportedException.

The catch block for LocatorNotSupportedException is empty. This can lead to swallowed exceptions which makes debugging difficult. It's important to handle exceptions explicitly, even if it's just logging them. Consider adding at least a log statement or rethrowing the exception with additional context.

- catch (LocatorNotSupportedException ex)
- {
- }
+ catch (LocatorNotSupportedException ex)
+ {
+     _act.Error = "Locator not supported: " + ex.Message;
+     // Optionally, log the error or handle it accordingly.
+ }
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
catch (LocatorNotSupportedException ex)
{
catch (LocatorNotSupportedException ex)
{
_act.Error = "Locator not supported: " + ex.Message;
// Optionally, log the error or handle it accordingly.
}
Tools
GitHub Check: Codacy Static Code Analysis

[warning] 137-137: Ginger/GingerCoreNET/Drivers/CoreDrivers/Web/Playwright/ActionHandlers/ActUIElementHandler.cs#L137
Either remove or fill this block of code.

Comment on lines +22 to +35
private static readonly IEnumerable<eLocateBy> SupportedElementLocators = new List<eLocateBy>()
{
eLocateBy.ByID,
eLocateBy.ByCSS,
eLocateBy.ByXPath
};

private static readonly IEnumerable<eLocateBy> SupportedFrameLocators = new List<eLocateBy>()
{
eLocateBy.ByID,
eLocateBy.ByTitle,
eLocateBy.ByUrl,
eLocateBy.ByXPath
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The review comment suggests verifying the completeness of the supported locators lists. Based on the results from the shell script, the following locators are used across the codebase:

  • eLocateBy.ByID
  • eLocateBy.ByCSS
  • eLocateBy.ByXPath
  • eLocateBy.ByTitle
  • eLocateBy.ByUrl
  • eLocateBy.ByName
  • eLocateBy.ByRelXPath
  • eLocateBy.ByValue
  • eLocateBy.ByLinkText
  • eLocateBy.ByClassName
  • eLocateBy.ByAutomationID
  • eLocateBy.ByXY
  • eLocateBy.ByHref
  • eLocateBy.ByIndex
  • eLocateBy.ByMulitpleProperties
  • eLocateBy.ByTagName
  • eLocateBy.ByContentDescription
  • eLocateBy.ByText
  • eLocateBy.ByBoundingRectangle
  • eLocateBy.IsEnabled
  • eLocateBy.IsOffscreen
  • eLocateBy.POMElement
  • eLocateBy.iOSPredicateString
  • eLocateBy.iOSClassChain
  • eLocateBy.ByCaretPosition
  • eLocateBy.ByngModel
  • eLocateBy.ByngRepeat
  • eLocateBy.ByngBind
  • eLocateBy.ByngSelectedOption
  • eLocateBy.ByResourceID
  • eLocateBy.ByElementsRepository
  • eLocateBy.Unknown
  • eLocateBy.NA

The SupportedElementLocators list includes:

  • eLocateBy.ByID
  • eLocateBy.ByCSS
  • eLocateBy.ByXPath

The SupportedFrameLocators list includes:

  • eLocateBy.ByID
  • eLocateBy.ByTitle
  • eLocateBy.ByUrl
  • eLocateBy.ByXPath

Issues Found:

  1. The SupportedElementLocators list is missing several locators such as ByName, ByRelXPath, ByValue, ByLinkText, and others.
  2. The SupportedFrameLocators list is also missing locators like ByName, ByRelXPath, and others.

Actionable Follow-up:

  • Consider expanding the SupportedElementLocators and SupportedFrameLocators lists to include all relevant locators used in the codebase.

Analysis chain

Validate the completeness of supported locators.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Ensure all necessary locators are supported.

# Test: List all locators used in the project.
rg --type cs "eLocateBy"

Length of output: 166266

Comment on lines +238 to +285
message = string.Empty;

if (act is ActWithoutDriver)
{
message = string.Empty;
return true;
}
if (act is ActUIElement actUIElement)
{
bool isSupported = ActUIElementHandler.IsOperationSupported(actUIElement.ElementAction);
if (!isSupported)
bool isLocatorSupported = PlaywrightBrowserTab.IsElementLocatorSupported(actUIElement.ElementLocateBy);
if (!isLocatorSupported)
{
string operationName = Common.GeneralLib.General.GetEnumValueDescription(typeof(ActBrowserElement.eControlAction), actUIElement.ElementAction);
message = $"'{act.ActionType} - {operationName}' is not supported by Playwright driver, use Selenium driver instead.";
message = $"Element Locator '{actUIElement.ElementLocateBy}' is not supported by Playwright driver, use Selenium driver instead.";
}
else

bool isOperationSupported = ActUIElementSupportedOperations.Contains(actUIElement.ElementAction);
if (!isOperationSupported)
{
message = string.Empty;
string operationName = Common.GeneralLib.General.GetEnumValueDescription(typeof(ActBrowserElement.eControlAction), actUIElement.ElementAction);
if (!string.IsNullOrEmpty(message))
{
message += '\n';
}
message += $"'{act.ActionType} - {operationName}' is not supported by Playwright driver, use Selenium driver instead.";
}
return isSupported;

return isLocatorSupported && isOperationSupported;
}
else if (act is ActBrowserElement actBrowserElement)
{
bool isSupported = ActBrowserElementHandler.IsOperationSupported(actBrowserElement.ControlAction);
if (!isSupported)
bool isLocatorSupported =
actBrowserElement.ControlAction == ActBrowserElement.eControlAction.SwitchFrame &&
PlaywrightBrowserTab.IsFrameLocatorSupported(act.LocateBy);
if (!isLocatorSupported)
{
string operationName = Common.GeneralLib.General.GetEnumValueDescription(typeof(ActBrowserElement.eControlAction), actBrowserElement.ControlAction);
message = $"'{act.ActionType} - {operationName}' is not supported by Playwright driver, use Selenium driver instead.";
message = $"Frame Locator '{act.LocateBy}' is not supported by Playwright driver, use Selenium driver instead.";
}
else

bool isOperationSupported = ActBrowserElementSupportedOperations.Contains(actBrowserElement.ControlAction);
if (!isOperationSupported)
{
message = string.Empty;
string operationName = Common.GeneralLib.General.GetEnumValueDescription(typeof(ActBrowserElement.eControlAction), actBrowserElement.ControlAction);
if (!string.IsNullOrEmpty(message))
{
message += '\n';
}
message += $"'{act.ActionType} - {operationName}' is not supported by Playwright driver, use Selenium driver instead.";
}
return isSupported;
return isLocatorSupported && isOperationSupported;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ensure consistency in error handling.

Consider abstracting the logic for checking supported locators and operations into separate methods to reduce duplication and improve maintainability.

Comment on lines +35 to +51
public override bool Equals(object? obj)
{
if (obj is not Context other)
{
return false;
}

return Equals(other);
}

public override int GetHashCode()
{
HashCode hashCode = new();
hashCode.Add(Environment.GetHashCode());
hashCode.Add(BusinessFlow.GetHashCode());
return hashCode.ToHashCode();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Enhance the Context struct's hash code generation.

Consider utilizing HashCode.Combine(Environment, BusinessFlow) for a more concise and potentially more efficient hash code computation.

@Maheshkale447
Maheshkale447 merged commit 93b7df9 into Releases/Beta Jun 14, 2024
@Maheshkale447
Maheshkale447 deleted the BugFix/39434-AnalyzeForUnsupportedLocators branch June 14, 2024 11:25
@coderabbitai coderabbitai Bot mentioned this pull request Sep 9, 2024
15 tasks
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