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

added csharp alert code #2089

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

Conversation

pallavigitwork
Copy link
Member

@pallavigitwork pallavigitwork commented Dec 2, 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

added csharp alert code

Motivation and Context

code was missing

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

enhancement, documentation


Description

  • Added a new C# test class AlertsTest to handle different types of alerts including simple, confirm, and prompt alerts.
  • Updated the documentation across multiple languages (English, Japanese, Portuguese, Chinese) to reference the new C# test file for alert handling.
  • Replaced inline C# code examples in the documentation with links to the new test file to ensure consistency and maintainability.

Changes walkthrough 📝

Relevant files
Enhancement
AlertsTest.cs
Add C# test for handling alert interactions                           

examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs

  • Added a new C# test class AlertsTest for handling alerts.
  • Implemented methods to test simple, confirm, and prompt alerts.
  • Utilized IJavaScriptExecutor and WebDriverWait for alert interactions.
  • Ensured proper driver management with driver.Quit().
  • +101/-1 
    Documentation
    alerts.en.md
    Update C# alert documentation with new examples                   

    website_and_docs/content/documentation/webdriver/interactions/alerts.en.md

  • Updated C# code examples to reference new test file.
  • Replaced inline C# code with links to the new test file.
  • +9/-40   
    alerts.ja.md
    Update Japanese C# alert documentation with new examples 

    website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md

  • Updated C# code examples to reference new test file.
  • Replaced inline C# code with links to the new test file.
  • +9/-40   
    alerts.pt-br.md
    Update Portuguese C# alert documentation with new examples

    website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md

  • Updated C# code examples to reference new test file.
  • Replaced inline C# code with links to the new test file.
  • +9/-28   
    alerts.zh-cn.md
    Update Chinese C# alert documentation with new examples   

    website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md

  • Updated C# code examples to reference new test file.
  • Replaced inline C# code with links to the new test file.
  • +9/-39   

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

    Copy link

    netlify bot commented Dec 2, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit 79f3f5f

    Copy link
    Contributor

    qodo-merge-pro bot commented Dec 2, 2024

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

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

    Code Duplication
    The alert wait logic is duplicated three times. Consider extracting this into a reusable helper method.

    Resource Management
    The WebDriver instance should be properly disposed using 'using' statement or try-finally block to ensure cleanup.

    Copy link
    Contributor

    qodo-merge-pro bot commented Dec 2, 2024

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    General
    Ensure proper resource cleanup by using a disposal pattern for driver management

    Use a using statement for the WebDriver to ensure proper disposal of resources, even
    if an exception occurs during test execution.

    examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs [30-106]

    -WebDriver driver = new ChromeDriver();
    -driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
    -// ... test code ...
    -driver.Quit(); //close all windows
    +using (WebDriver driver = new ChromeDriver())
    +{
    +    driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500);
    +    // ... test code ...
    +} // WebDriver automatically disposed
    Suggestion importance[1-10]: 9

    Why: Using a 'using' statement ensures proper resource cleanup even in case of exceptions, preventing potential memory leaks and resource issues. This is a critical best practice for managing IDisposable resources in C#.

    9
    Handle specific exceptions rather than catching and silently ignoring all exceptions

    Avoid catching and swallowing generic Exception without proper error handling in the
    alert wait condition.

    examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs [40-49]

     wait.Until((driver) => {
         try
         {
             return driver.SwitchTo().Alert();
         }
    -    catch (Exception ex)
    +    catch (NoAlertPresentException)
         {
             return null;
         }
     });
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Catching specific NoAlertPresentException instead of generic Exception improves error handling and debugging by avoiding unintended exception suppression. This makes the code more maintainable and safer.

    7
    Eliminate code duplication by extracting repeated logic into a reusable method

    Extract the duplicated alert wait logic into a reusable helper method to improve
    code maintainability and reduce duplication.

    examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs [40-49]

    -wait.Until((driver) => {
    -    try
    -    {
    -        return driver.SwitchTo().Alert();
    -    }
    -    catch (Exception ex)
    -    {
    -        return null;
    -    }
    -});
    +private IAlert WaitForAlert(WebDriverWait wait)
    +{
    +    return wait.Until(driver => {
    +        try
    +        {
    +            return driver.SwitchTo().Alert();
    +        }
    +        catch (NoAlertPresentException)
    +        {
    +            return null;
    +        }
    +    });
    +}
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: The alert wait logic is duplicated three times in the code. Extracting it into a helper method would improve maintainability and reduce the chance of inconsistencies.

    6

    💡 Need additional feedback ? start a PR chat

    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