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

Adding WaitForDownload test for .NET #1885

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

Conversation

halex2005
Copy link

@halex2005 halex2005 commented Aug 22, 2024

User description

Hi!

Description

I've added WaitForDownload test case for .NET.
Review, please.

Note, that I've also added --no-sandbox switch to ChromeOptions because chrome crashes without it.
It's to make sure that tests are green.

Types of changes

  • Change to the site (I have double-checked the Netlify deployment, and my changes look good)
  • [x ] 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

  • [x ] I have read the contributing document.
  • [x ] I have used hugo to render the site/docs locally and I am sure it works.

PR Type

Tests, Enhancement


Description

  • Added a new WaitForDownload test method in NetworkTest.cs to handle and verify file downloads.
  • Enhanced BaseTest.cs by adding a --no-sandbox argument to ChromeOptions to prevent Chrome crashes during tests.
  • Updated documentation files to include CSharp code block references for better clarity and guidance.

Changes walkthrough 📝

Relevant files
Enhancement
BaseTest.cs
Add no-sandbox argument to ChromeOptions                                 

examples/dotnet/SeleniumDocs/BaseTest.cs

  • Added --no-sandbox argument to ChromeOptions.
  • Ensures Chrome does not crash during tests.
  • +1/-0     
    Tests
    NetworkTest.cs
    Add WaitForDownload test method for file downloads             

    examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs

  • Added WaitForDownload test method.
  • Implemented download behavior settings and event handling.
  • Verified file download completion and existence.
  • +47/-3   
    Documentation
    network.en.md
    Update CSharp tab with code block reference                           

    website_and_docs/content/documentation/webdriver/bidi/cdp/network.en.md

    • Updated CSharp tab with code block reference.
    +1/-1     
    network.ja.md
    Update CSharp tab with code block reference                           

    website_and_docs/content/documentation/webdriver/bidi/cdp/network.ja.md

    • Updated CSharp tab with code block reference.
    +1/-1     
    network.pt-br.md
    Update CSharp tab with code block reference                           

    website_and_docs/content/documentation/webdriver/bidi/cdp/network.pt-br.md

    • Updated CSharp tab with code block reference.
    +1/-1     
    network.zh-cn.md
    Update CSharp tab with code block reference                           

    website_and_docs/content/documentation/webdriver/bidi/cdp/network.zh-cn.md

    • Updated CSharp tab with code block reference.
    +1/-1     

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    Copy link

    netlify bot commented Aug 22, 2024

    👷 Deploy request for selenium-dev pending review.

    Visit the deploys page to approve it

    Name Link
    🔨 Latest commit e211eba

    @CLAassistant
    Copy link

    CLAassistant commented Aug 22, 2024

    CLA assistant check
    All committers have signed the CLA.

    Copy link
    Contributor

    PR Reviewer Guide 🔍

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 Security concerns

    Security concern:
    Adding '--no-sandbox' to ChromeOptions (examples/dotnet/SeleniumDocs/BaseTest.cs, line 42) disables the Chrome sandbox, which is a security feature. This can potentially expose the system to security risks if malicious code is executed. While it may be necessary for testing environments, it should be used with caution and not in production environments.

    ⚡ Key issues to review

    Security Concern
    Adding '--no-sandbox' argument to ChromeOptions may introduce security risks in production environments.

    Error Handling
    The WaitForDownload test method lacks proper error handling for potential exceptions during file operations.

    Copy link
    Contributor

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Error handling
    Add error handling when deleting files to manage potential exceptions

    Consider using File.Delete() within a try-catch block to handle potential exceptions
    that may occur during file deletion, such as access denied or file not found errors.

    examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs [192]

    -File.Delete(downloadedFilePath);
    +try
    +{
    +    File.Delete(downloadedFilePath);
    +}
    +catch (IOException ex)
    +{
    +    Console.WriteLine($"Error deleting file: {ex.Message}");
    +}
     
    • Apply this suggestion
    Suggestion importance[1-10]: 9

    Why: This suggestion significantly enhances the code by adding error handling for file deletion, which is crucial for managing exceptions like access denied or file not found, thereby improving reliability.

    9
    Add exception handling to the event handler to catch and log specific errors

    Consider using a more specific exception type instead of Exception when catching
    exceptions. This helps in better error handling and provides more information about
    the specific error that occurred.

    examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs [173-183]

     session.DevToolsEventReceived += (sender, args) =>
     {
    -    var downloadState = args.EventData["state"]?.ToString();
    -    if (args.EventName == "downloadProgress" &&
    -        (string.Equals(downloadState, "completed") ||
    -         string.Equals(downloadState, "canceled")))
    +    try
         {
    -        downloadId = args.EventData["guid"].ToString();
    -        downloaded = downloadState.Equals("completed");
    -        downloadCompleted.Set();
    +        var downloadState = args.EventData["state"]?.ToString();
    +        if (args.EventName == "downloadProgress" &&
    +            (string.Equals(downloadState, "completed") ||
    +             string.Equals(downloadState, "canceled")))
    +        {
    +            downloadId = args.EventData["guid"].ToString();
    +            downloaded = downloadState.Equals("completed");
    +            downloadCompleted.Set();
    +        }
    +    }
    +    catch (KeyNotFoundException ex)
    +    {
    +        Console.WriteLine($"Error accessing EventData: {ex.Message}");
         }
     };
     
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: The suggestion correctly adds exception handling to the event handler, which enhances error management by catching specific exceptions and logging them, improving robustness and debugging capabilities.

    8
    Best practice
    Use null-coalescing operator when combining paths to handle potential null values

    Consider using Path.Combine() to construct the file path instead of string
    concatenation. This ensures that the correct path separator is used for the current
    operating system.

    examples/dotnet/SeleniumDocs/BiDi/CDP/NetworkTest.cs [190]

    -var downloadedFilePath = Path.Combine(downloadPath, downloadId);
    +var downloadedFilePath = Path.Combine(downloadPath, downloadId ?? string.Empty);
     
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: The suggestion improves the code by using a null-coalescing operator to handle potential null values when combining paths, ensuring that the path construction is robust and error-free.

    7
    Documentation
    Add a comment to explain the usage of a potentially security-impacting browser argument

    Consider adding a comment explaining why the "--no-sandbox" argument is being used.
    This argument can have security implications, so it's important to document the
    reason for its usage.

    examples/dotnet/SeleniumDocs/BaseTest.cs [35]

    +// Adding --no-sandbox for CI/CD environments or when running with limited privileges
     options.AddArgument("--no-sandbox");
     
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Adding a comment to explain the use of "--no-sandbox" is a good practice for documentation, especially given its security implications, but it is not critical to the functionality of the code.

    6

    @diemol
    Copy link
    Member

    diemol commented Aug 23, 2024

    @halex2005 can you please sign the CLA?

    @halex2005
    Copy link
    Author

    @diemol, signed

    @shbenzer
    Copy link
    Contributor

    looks like the failing check is unrelated @diemol

    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.

    5 participants