seleniumhq.github.io icon indicating copy to clipboard operation
seleniumhq.github.io copied to clipboard

Adding WaitForDownload test for .NET

Open halex2005 opened this issue 1 year ago • 8 comments

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

    halex2005 avatar Aug 22 '24 17:08 halex2005

    Deploy Preview for selenium-dev ready!

    Name Link
    Latest commit d2847e8b8bd38df00be4130a49c7e7f7510a3f83
    Latest deploy log https://app.netlify.com/projects/selenium-dev/deploys/68a0e03e5a25ef0008d22782
    Deploy Preview https://deploy-preview-1885--selenium-dev.netlify.app
    Preview on mobile
    Toggle QR Code...

    QR Code

    Use your smartphone camera to open QR code link.

    To edit notification comments on pull requests, go to your Netlify project configuration.

    netlify[bot] avatar Aug 22 '24 17:08 netlify[bot]

    CLA assistant check
    All committers have signed the CLA.

    CLAassistant avatar Aug 22 '24 17:08 CLAassistant

    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.

    qodo-code-review[bot] avatar Aug 22 '24 17:08 qodo-code-review[bot]

    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

    qodo-code-review[bot] avatar Aug 22 '24 17:08 qodo-code-review[bot]

    @halex2005 can you please sign the CLA?

    diemol avatar Aug 23 '24 08:08 diemol

    @diemol, signed

    halex2005 avatar Aug 23 '24 18:08 halex2005

    looks like the failing check is unrelated @diemol

    shbenzer avatar Oct 22 '24 15:10 shbenzer

    @halex2005, there are compilation issues.

    cannot convert from 'string' to 'int'

    diemol avatar Aug 16 '25 19:08 diemol